3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
12 public class WvDelayedString
16 public WvDelayedString(Func
<string> a
)
21 public override string ToString()
27 public partial class wv
29 static string hexbyte(WvBytes b
, int ofs
)
31 if (ofs
>= b
.start
&& ofs
< b
.start
+b
.len
)
32 return b
.bytes
[ofs
].ToString("x2");
37 static char printable(WvBytes b
, int ofs
)
39 if (ofs
>= b
.start
&& ofs
< b
.start
+b
.len
)
41 byte n
= b
.bytes
[ofs
];
42 if (31 < n
&& n
< 127)
51 public static string _hexdump(WvBytes b
)
56 var sb
= new StringBuilder();
58 // This is overly complicated so that the body and header of
59 // the same buffer can be printed separately yet still show the
62 int rowoffset
= b
.start
& (~
0xf);
64 // Note: it's important to set the right capacity when dealing
65 // with large quantities of data. Assume about 80 chars per line.
66 sb
.EnsureCapacity((b
.len
/ 16 + 2) * 80);
68 for (int i
= rowoffset
; i
< b
.len
; i
+= 16)
70 sb
.Append('[').Append(i
.ToString("x4")).Append("]");
72 for (int j
= 0; j
< 16; j
++)
76 sb
.Append(hexbyte(b
, i
+j
));
81 for (int j
= 0; j
< 16; j
++)
85 sb
.Append(printable(b
, i
+j
));
94 public static object hexdump(WvBytes b
)
96 return new WvDelayedString(() => _hexdump(b
));