1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

encoding.base32: vfmt code

This commit is contained in:
Joe Conigliaro 2022-09-27 16:29:44 +10:00
parent 5415c4f75e
commit 51a92d170f
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 45 additions and 52 deletions

View File

@ -34,7 +34,7 @@ pub fn decode_to_string(src []u8) ?string {
}
pub fn decode(src []u8) ?[]u8 {
mut e := new_encoding(std_alphabet)
mut e := new_encoding(base32.std_alphabet)
return e.decode(src)
}
@ -47,7 +47,7 @@ pub fn encode_to_string(src []u8) string {
}
pub fn encode(src []u8) []u8 {
e := new_encoding(std_alphabet)
e := new_encoding(base32.std_alphabet)
return e.encode(src)
}
@ -60,15 +60,15 @@ pub fn (enc &Encoding) encode_string_to_string(src string) string {
}
pub fn new_std_encoding() Encoding {
return new_encoding_with_padding(std_alphabet, std_padding)
return new_encoding_with_padding(base32.std_alphabet, base32.std_padding)
}
pub fn new_std_encoding_with_padding(padding u8) Encoding {
return new_encoding_with_padding(std_alphabet, padding)
return new_encoding_with_padding(base32.std_alphabet, padding)
}
pub fn new_encoding(alphabet []u8) Encoding {
return new_encoding_with_padding(alphabet, std_padding)
return new_encoding_with_padding(alphabet, base32.std_padding)
}
pub fn new_encoding_with_padding(alphabet []u8, padding_char u8) Encoding {
@ -156,7 +156,7 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
// Pad the final quantum
if src.len < 5 {
if enc.padding_char == no_padding {
if enc.padding_char == base32.no_padding {
break
}
@ -181,7 +181,7 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
}
fn (enc &Encoding) encoded_len(n int) int {
if enc.padding_char == no_padding {
if enc.padding_char == base32.no_padding {
return (n * 8 + 4) / 5
}
return (n + 4) / 5 * 8
@ -207,9 +207,7 @@ pub fn (enc &Encoding) decode(src []u8) ?[]u8 {
// l := strip_newlines(mut dst, src)
// n, _ := enc.decode_(src[..l], mut dst) or {
// src := strip_newlines(src_)
n, _ := enc.decode_(src, mut buf) or {
return err
}
n, _ := enc.decode_(src, mut buf) or { return err }
return buf[..n]
}
@ -233,9 +231,8 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
mut dlen := 8
for j := 0; j < 8; {
if src.len == 0 {
if enc.padding_char != no_padding {
if enc.padding_char != base32.no_padding {
// We have reached the end and are missing padding
// return n, false, corrupt_input_error(olen - src.len - j)
return error(corrupt_input_error_msg(olen - src.len - j))
@ -332,7 +329,6 @@ fn strip_newlines(src []u8) []u8 {
return dst
}
fn corrupt_input_error_msg(e int) string {
// return error('illegal base32 data at input byte ' + strconv.FormatInt(int64(e), 10)
return 'illegal base32 data at input byte $e'

View File

@ -1,6 +1,5 @@
import encoding.base32
// TODO: add more tests
fn test_encode_and_decode() {
@ -9,9 +8,7 @@ fn test_encode_and_decode() {
encoded := base32.encode_string_to_string(input)
assert encoded == 'NBSWY3DPEB3A===='
decoded := base32.decode_string_to_string(encoded) or {
panic('error decoding: $err')
}
decoded := base32.decode_string_to_string(encoded) or { panic('error decoding: $err') }
assert decoded == input
encoder_no_padding := base32.new_std_encoding_with_padding(base32.no_padding)