aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-09-02 19:23:30 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-09-02 19:23:30 -0400
commit3eb89ee4db31eef9540273b25689c0515da6bf57 (patch)
tree5ed3117bae6a4f67d067fa2a36888275363228b5 /std
parent0d8412d9f0afa399fbb9011c9f2f8f13a00a41a2 (diff)
downloadzig-3eb89ee4db31eef9540273b25689c0515da6bf57.tar.gz
zig-3eb89ee4db31eef9540273b25689c0515da6bf57.zip
fixups
* zig fmt * use canonical parameter order. memcpy has dest first and the base64 code follows the pattern. * pass correct radix to charToDigit
Diffstat (limited to 'std')
-rw-r--r--std/fmt/index.zig29
1 files changed, 14 insertions, 15 deletions
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index 13a0b525d7..80af750f3d 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -350,7 +350,7 @@ pub fn formatText(
comptime var width = 0;
if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
return formatBuf(bytes, width, context, Errors, output);
- } else if ((fmt[0] == 'x') or (fmt[0] == 'X') ) {
+ } else if ((fmt[0] == 'x') or (fmt[0] == 'X')) {
for (bytes) |c| {
try formatInt(c, 16, fmt[0] == 'X', 2, context, Errors, output);
}
@@ -1332,22 +1332,21 @@ pub fn isWhiteSpace(byte: u8) bool {
};
}
-// depends on https://github.com/ziglang/zig/pull/1454
-pub fn hexToBytes(input: []const u8, out: []u8) !void {
- if (out.len * 2 < input.len)
- return error.InvalidLength;
+pub fn hexToBytes(out: []u8, input: []const u8) !void {
+ if (out.len * 2 < input.len)
+ return error.InvalidLength;
- var i: usize = 0;
- while (i < input.len) : (i += 2) {
- out[i/2] = (try charToDigit(input[i], 36)) << 4;
- out[i/2] += try charToDigit(input[i+1], 36);
- }
+ var in_i: usize = 0;
+ while (in_i != input.len) : (in_i += 2) {
+ const hi = try charToDigit(input[in_i], 16);
+ const lo = try charToDigit(input[in_i + 1], 16);
+ out[in_i / 2] = (hi << 4) | lo;
+ }
}
test "fmt.hexToBytes" {
- const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706";
- var pb: [32]u8 = undefined;
- try hexToBytes(test_hex_str, pb[0..]);
- try testFmt(test_hex_str, "{X}", pb);
+ const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706";
+ var pb: [32]u8 = undefined;
+ try hexToBytes(pb[0..], test_hex_str);
+ try testFmt(test_hex_str, "{X}", pb);
}
-