v
Зеркало из https://github.com/vlang/v
1module hex
2
3import strings
4
5// decode converts a hex string into an array of bytes. The expected
6// input format is 2 ASCII characters for each output byte. If the provided
7// string length is not a multiple of 2, an implicit `0` is prepended to it.
8pub fn decode(s string) ![]u8 {
9mut hex_str := s
10if hex_str.len >= 2 {
11if s[0] == `0` && (s[1] == `x` || s[1] == `X`) {
12hex_str = s[2..]
13}
14}
15if hex_str.len == 0 {
16return []u8{}
17} else if hex_str.len == 1 {
18return [char2nibble(hex_str[0])!]
19} else if hex_str.len == 2 {
20n1 := char2nibble(hex_str[0])!
21n0 := char2nibble(hex_str[1])!
22return [(n1 << 4) | n0]
23}
24// calculate the first byte depending on if hex_str.len is odd
25mut val := char2nibble(hex_str[0])!
26if hex_str.len & 1 == 0 {
27val = (val << 4) | char2nibble(hex_str[1])!
28}
29// set cap to hex_str.len/2 rounded up
30mut bytes := []u8{len: 1, cap: (hex_str.len + 1) >> 1, init: val}
31// iterate over every 2 bytes
32// the start index depends on if hex_str.len is odd
33for i := 2 - (hex_str.len & 1); i < hex_str.len; i += 2 {
34n1 := char2nibble(hex_str[i])!
35n0 := char2nibble(hex_str[i + 1])!
36bytes << (n1 << 4) | n0
37}
38return bytes
39}
40
41// encode converts an array of bytes into a string of ASCII hex bytes. The
42// output will always be a string with length a multiple of 2.
43@[manualfree]
44pub fn encode(bytes []u8) string {
45mut sb := strings.new_builder(bytes.len * 2)
46for b in bytes {
47sb.write_string(b.hex())
48}
49res := sb.str()
50unsafe { sb.free() }
51return res
52}
53
54// char2nibble converts an ASCII hex character to it's hex value
55fn char2nibble(b u8) !u8 {
56match b {
57`0`...`9` { return b - u8(`0`) }
58`A`...`F` { return b - u8(`A`) + 10 }
59`a`...`f` { return b - u8(`a`) + 10 }
60else { return error('invalid hex char ${b.ascii_str()}') }
61}
62}
63