Skip to content

Commit 51a92d1

Browse files
encoding.base32: vfmt code
1 parent 5415c4f commit 51a92d1

File tree

2 files changed

+45
-52
lines changed

2 files changed

+45
-52
lines changed

vlib/encoding/base32/base32.v

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
module base32
1212

13-
pub const(
14-
std_padding = `=` // Standard padding character
15-
no_padding = u8(-1) // No padding
16-
std_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.bytes()
17-
hex_alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUV'.bytes()
13+
pub const (
14+
std_padding = `=` // Standard padding character
15+
no_padding = u8(-1) // No padding
16+
std_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.bytes()
17+
hex_alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUV'.bytes()
1818
)
1919

2020
struct Encoding {
2121
padding_char u8
22-
alphabet []u8
22+
alphabet []u8
2323
mut:
24-
decode_map [256]u8
24+
decode_map [256]u8
2525
}
2626

2727
pub fn decode_string_to_string(src string) ?string {
@@ -34,7 +34,7 @@ pub fn decode_to_string(src []u8) ?string {
3434
}
3535

3636
pub fn decode(src []u8) ?[]u8 {
37-
mut e := new_encoding(std_alphabet)
37+
mut e := new_encoding(base32.std_alphabet)
3838
return e.decode(src)
3939
}
4040

@@ -47,8 +47,8 @@ pub fn encode_to_string(src []u8) string {
4747
}
4848

4949
pub fn encode(src []u8) []u8 {
50-
e := new_encoding(std_alphabet)
51-
return e.encode(src)
50+
e := new_encoding(base32.std_alphabet)
51+
return e.encode(src)
5252
}
5353

5454
pub fn (enc &Encoding) encode_to_string(src []u8) string {
@@ -60,15 +60,15 @@ pub fn (enc &Encoding) encode_string_to_string(src string) string {
6060
}
6161

6262
pub fn new_std_encoding() Encoding {
63-
return new_encoding_with_padding(std_alphabet, std_padding)
63+
return new_encoding_with_padding(base32.std_alphabet, base32.std_padding)
6464
}
6565

6666
pub fn new_std_encoding_with_padding(padding u8) Encoding {
67-
return new_encoding_with_padding(std_alphabet, padding)
67+
return new_encoding_with_padding(base32.std_alphabet, padding)
6868
}
6969

7070
pub fn new_encoding(alphabet []u8) Encoding {
71-
return new_encoding_with_padding(alphabet, std_padding)
71+
return new_encoding_with_padding(alphabet, base32.std_padding)
7272
}
7373

7474
pub fn new_encoding_with_padding(alphabet []u8, padding_char u8) Encoding {
@@ -83,7 +83,7 @@ pub fn new_encoding_with_padding(alphabet []u8, padding_char u8) Encoding {
8383
}
8484

8585
mut decode_map := [256]u8{}
86-
for i in 0..alphabet.len {
86+
for i in 0 .. alphabet.len {
8787
decode_map[alphabet[i]] = u8(i)
8888
}
8989

@@ -95,8 +95,8 @@ pub fn new_encoding_with_padding(alphabet []u8, padding_char u8) Encoding {
9595
}
9696

9797
fn (enc &Encoding) encode(src []u8) []u8 {
98-
mut buf := []u8{len: enc.encoded_len(src.len)}
99-
mut dst := unsafe { buf }
98+
mut buf := []u8{len: enc.encoded_len(src.len)}
99+
mut dst := unsafe { buf }
100100
enc.encode_(src, mut dst)
101101
return buf
102102
}
@@ -140,23 +140,23 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
140140
// Encode 5-bit blocks using the base32 alphabet
141141
if dst.len >= 8 {
142142
// Common case, unrolled for extra performance
143-
dst[0] = enc.alphabet[b[0]&31]
144-
dst[1] = enc.alphabet[b[1]&31]
145-
dst[2] = enc.alphabet[b[2]&31]
146-
dst[3] = enc.alphabet[b[3]&31]
147-
dst[4] = enc.alphabet[b[4]&31]
148-
dst[5] = enc.alphabet[b[5]&31]
149-
dst[6] = enc.alphabet[b[6]&31]
150-
dst[7] = enc.alphabet[b[7]&31]
143+
dst[0] = enc.alphabet[b[0] & 31]
144+
dst[1] = enc.alphabet[b[1] & 31]
145+
dst[2] = enc.alphabet[b[2] & 31]
146+
dst[3] = enc.alphabet[b[3] & 31]
147+
dst[4] = enc.alphabet[b[4] & 31]
148+
dst[5] = enc.alphabet[b[5] & 31]
149+
dst[6] = enc.alphabet[b[6] & 31]
150+
dst[7] = enc.alphabet[b[7] & 31]
151151
} else {
152152
for i := 0; i < dst.len; i++ {
153-
dst[i] = enc.alphabet[b[i]&31]
153+
dst[i] = enc.alphabet[b[i] & 31]
154154
}
155155
}
156156

157157
// Pad the final quantum
158158
if src.len < 5 {
159-
if enc.padding_char == no_padding {
159+
if enc.padding_char == base32.no_padding {
160160
break
161161
}
162162

@@ -180,14 +180,14 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
180180
}
181181
}
182182

183-
fn (enc &Encoding) encoded_len(n int) int {
184-
if enc.padding_char == no_padding {
185-
return (n*8 + 4) / 5
183+
fn (enc &Encoding) encoded_len(n int) int {
184+
if enc.padding_char == base32.no_padding {
185+
return (n * 8 + 4) / 5
186186
}
187187
return (n + 4) / 5 * 8
188188
}
189189

190-
pub fn (enc &Encoding) decode_string(src string) ?[]u8 {
190+
pub fn (enc &Encoding) decode_string(src string) ?[]u8 {
191191
return enc.decode(src.bytes())
192192
// mut buf := strip_newlines(src.bytes())
193193
// mut dst := unsafe { buf }
@@ -196,20 +196,18 @@ pub fn (enc &Encoding) decode_string(src string) ?[]u8 {
196196
// return buf[..n]
197197
}
198198

199-
pub fn (enc &Encoding) decode_string_to_string(src string) ?string {
199+
pub fn (enc &Encoding) decode_string_to_string(src string) ?string {
200200
decoded := enc.decode_string(src)?
201201
return decoded.bytestr()
202202
}
203203

204-
pub fn (enc &Encoding) decode(src []u8) ?[]u8 {
204+
pub fn (enc &Encoding) decode(src []u8) ?[]u8 {
205205
mut buf := []u8{len: src.len}
206-
// mut dst := unsafe { buf }
206+
// mut dst := unsafe { buf }
207207
// l := strip_newlines(mut dst, src)
208208
// n, _ := enc.decode_(src[..l], mut dst) or {
209209
// src := strip_newlines(src_)
210-
n, _ := enc.decode_(src, mut buf) or {
211-
return err
212-
}
210+
n, _ := enc.decode_(src, mut buf) or { return err }
213211
return buf[..n]
214212
}
215213

@@ -233,9 +231,8 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
233231
mut dlen := 8
234232

235233
for j := 0; j < 8; {
236-
237234
if src.len == 0 {
238-
if enc.padding_char != no_padding {
235+
if enc.padding_char != base32.no_padding {
239236
// We have reached the end and are missing padding
240237
// return n, false, corrupt_input_error(olen - src.len - j)
241238
return error(corrupt_input_error_msg(olen - src.len - j))
@@ -248,12 +245,12 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
248245
src = src[1..]
249246
if in0 == enc.padding_char && j >= 2 && src.len < 8 {
250247
// We`ve reached the end and there`s padding
251-
if src.len+j < 8-1 {
248+
if src.len + j < 8 - 1 {
252249
// not enough padding
253250
// return n, false, corrupt_input_error(olen)
254251
return error(corrupt_input_error_msg(olen))
255252
}
256-
for k := 0; k < 8-1-j; k++ {
253+
for k := 0; k < 8 - 1 - j; k++ {
257254
if src.len > k && src[k] != enc.padding_char {
258255
// incorrect padding
259256
// return n, false, corrupt_input_error(olen - src.len + k - 1)
@@ -272,7 +269,7 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
272269
}
273270
break
274271
}
275-
272+
276273
dbuf[j] = enc.decode_map[in0]
277274
if dbuf[j] == 0xFF {
278275
// return n, false, corrupt_input_error(olen - src.len - 1)
@@ -284,23 +281,23 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
284281
// Pack 8x 5-bit source blocks into 5 u8 destination
285282
// quantum
286283
if dlen == 8 {
287-
dst[dsti+4] = dbuf[6]<<5 | dbuf[7]
284+
dst[dsti + 4] = dbuf[6] << 5 | dbuf[7]
288285
n++
289286
}
290287
if dlen >= 7 {
291-
dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
288+
dst[dsti + 3] = dbuf[4] << 7 | dbuf[5] << 2 | dbuf[6] >> 3
292289
n++
293290
}
294291
if dlen >= 5 {
295-
dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1
292+
dst[dsti + 2] = dbuf[3] << 4 | dbuf[4] >> 1
296293
n++
297294
}
298295
if dlen >= 4 {
299-
dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
296+
dst[dsti + 1] = dbuf[1] << 6 | dbuf[2] << 1 | dbuf[3] >> 4
300297
n++
301298
}
302299
if dlen >= 2 {
303-
dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2
300+
dst[dsti + 0] = dbuf[0] << 3 | dbuf[1] >> 2
304301
n++
305302
}
306303
dsti += 5
@@ -332,7 +329,6 @@ fn strip_newlines(src []u8) []u8 {
332329
return dst
333330
}
334331

335-
336332
fn corrupt_input_error_msg(e int) string {
337333
// return error('illegal base32 data at input byte ' + strconv.FormatInt(int64(e), 10)
338334
return 'illegal base32 data at input byte $e'

vlib/encoding/base32/base32_test.v

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import encoding.base32
22

3-
43
// TODO: add more tests
54

65
fn test_encode_and_decode() {
76
input := 'hello v'
87

98
encoded := base32.encode_string_to_string(input)
109
assert encoded == 'NBSWY3DPEB3A===='
11-
12-
decoded := base32.decode_string_to_string(encoded) or {
13-
panic('error decoding: $err')
14-
}
10+
11+
decoded := base32.decode_string_to_string(encoded) or { panic('error decoding: $err') }
1512
assert decoded == input
1613

1714
encoder_no_padding := base32.new_std_encoding_with_padding(base32.no_padding)

0 commit comments

Comments
 (0)