mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: change optional to result in most of the libraries (#16123)
This commit is contained in:
@@ -42,7 +42,7 @@ pub fn (alphabet Alphabet) str() string {
|
||||
|
||||
// new_alphabet instantiates an Alphabet object based on
|
||||
// the provided characters
|
||||
pub fn new_alphabet(str string) ?Alphabet {
|
||||
pub fn new_alphabet(str string) !Alphabet {
|
||||
if str.len != 58 {
|
||||
return error(@MOD + '.' + @FN + ': string must be 58 characters in length')
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ module base58
|
||||
import math
|
||||
|
||||
// encode_int encodes any integer type to base58 string with Bitcoin alphabet
|
||||
pub fn encode_int(input int) ?string {
|
||||
pub fn encode_int(input int) !string {
|
||||
return encode_int_walpha(input, btc_alphabet)
|
||||
}
|
||||
|
||||
// encode_int_walpha any integer type to base58 string with custom alphabet
|
||||
pub fn encode_int_walpha(input int, alphabet Alphabet) ?string {
|
||||
pub fn encode_int_walpha(input int, alphabet Alphabet) !string {
|
||||
if input <= 0 {
|
||||
return error(@MOD + '.' + @FN + ': input must be greater than zero')
|
||||
}
|
||||
@@ -97,12 +97,12 @@ pub fn encode_walpha_bytes(input []u8, alphabet Alphabet) []u8 {
|
||||
}
|
||||
|
||||
// decode_int decodes base58 string to an integer with Bitcoin alphabet
|
||||
pub fn decode_int(input string) ?int {
|
||||
pub fn decode_int(input string) !int {
|
||||
return decode_int_walpha(input, btc_alphabet)
|
||||
}
|
||||
|
||||
// decode_int_walpha decodes base58 string to an integer with custom alphabet
|
||||
pub fn decode_int_walpha(input string, alphabet Alphabet) ?int {
|
||||
pub fn decode_int_walpha(input string, alphabet Alphabet) !int {
|
||||
mut total := 0 // to hold the results
|
||||
b58 := input.reverse()
|
||||
for i, ch in b58 {
|
||||
@@ -121,27 +121,27 @@ pub fn decode_int_walpha(input string, alphabet Alphabet) ?int {
|
||||
}
|
||||
|
||||
// decode decodes the base58 input string, using the Bitcoin alphabet
|
||||
pub fn decode(str string) ?string {
|
||||
pub fn decode(str string) !string {
|
||||
return decode_walpha(str, btc_alphabet)
|
||||
}
|
||||
|
||||
// decode_bytes decodes the base58 encoded input array, using the Bitcoin alphabet
|
||||
pub fn decode_bytes(input []u8) ?[]u8 {
|
||||
pub fn decode_bytes(input []u8) ![]u8 {
|
||||
return decode_walpha_bytes(input, btc_alphabet)
|
||||
}
|
||||
|
||||
// decode_walpha decodes the base58 encoded input string, using custom alphabet
|
||||
pub fn decode_walpha(input string, alphabet Alphabet) ?string {
|
||||
pub fn decode_walpha(input string, alphabet Alphabet) !string {
|
||||
if input.len == 0 {
|
||||
return ''
|
||||
}
|
||||
bin := input.bytes()
|
||||
res := decode_walpha_bytes(bin, alphabet)?
|
||||
res := decode_walpha_bytes(bin, alphabet)!
|
||||
return res.bytestr()
|
||||
}
|
||||
|
||||
// decode_walpha_bytes decodes the base58 encoded input array using a custom alphabet
|
||||
pub fn decode_walpha_bytes(input []u8, alphabet Alphabet) ?[]u8 {
|
||||
pub fn decode_walpha_bytes(input []u8, alphabet Alphabet) ![]u8 {
|
||||
if input.len == 0 {
|
||||
return []
|
||||
}
|
||||
|
||||
@@ -1,41 +1,34 @@
|
||||
module base58
|
||||
|
||||
fn main() {
|
||||
test_encode_int() or {}
|
||||
test_decode_int() or {}
|
||||
test_encode_string()
|
||||
test_fails() or {}
|
||||
}
|
||||
|
||||
fn test_encode_int() ? {
|
||||
fn test_encode_int() {
|
||||
a := 0x24 // should be 'd' in base58
|
||||
assert encode_int(a)? == 'd'
|
||||
assert encode_int(a)! == 'd'
|
||||
|
||||
test_encode_int_walpha()?
|
||||
test_encode_int_walpha()
|
||||
}
|
||||
|
||||
fn test_encode_int_walpha() ? {
|
||||
fn test_encode_int_walpha() {
|
||||
// random alphabet
|
||||
abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or {
|
||||
panic(@MOD + '.' + @FN + ': this should never happen')
|
||||
}
|
||||
a := 0x24 // should be '_' in base58 with our custom alphabet
|
||||
assert encode_int_walpha(a, abc)? == '_'
|
||||
assert encode_int_walpha(a, abc)! == '_'
|
||||
}
|
||||
|
||||
fn test_decode_int() ? {
|
||||
fn test_decode_int() {
|
||||
a := 'd'
|
||||
assert decode_int(a)? == 0x24
|
||||
assert decode_int(a)! == 0x24
|
||||
|
||||
test_decode_int_walpha()?
|
||||
test_decode_int_walpha()
|
||||
}
|
||||
|
||||
fn test_decode_int_walpha() ? {
|
||||
fn test_decode_int_walpha() {
|
||||
abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or {
|
||||
panic(@MOD + '.' + @FN + ': this should never happen')
|
||||
}
|
||||
a := '_'
|
||||
assert decode_int_walpha(a, abc)? == 0x24
|
||||
assert decode_int_walpha(a, abc)! == 0x24
|
||||
}
|
||||
|
||||
fn test_encode_string() {
|
||||
@@ -49,18 +42,18 @@ fn test_encode_string() {
|
||||
assert encode_walpha(a, abc) == '0P7yfPSL0pQh2L5'
|
||||
}
|
||||
|
||||
fn test_decode_string() ? {
|
||||
fn test_decode_string() {
|
||||
a := 'TtaR6twpTGu8VpY'
|
||||
assert decode(a)? == 'lorem ipsum'
|
||||
assert decode(a)! == 'lorem ipsum'
|
||||
|
||||
abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or {
|
||||
panic(@MOD + '.' + @FN + ': this should never happen')
|
||||
}
|
||||
b := '0P7yfPSL0pQh2L5'
|
||||
assert decode_walpha(b, abc)? == 'lorem ipsum'
|
||||
assert decode_walpha(b, abc)! == 'lorem ipsum'
|
||||
}
|
||||
|
||||
fn test_fails() ? {
|
||||
fn test_fails() ! {
|
||||
a := -238
|
||||
b := 0
|
||||
if z := encode_int(a) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import encoding.base58
|
||||
import encoding.hex
|
||||
|
||||
fn test_encode() ? {
|
||||
fn test_encode() {
|
||||
for input, expected in {
|
||||
'': ''
|
||||
'6263': '2PMCRQ'
|
||||
@@ -14,20 +14,20 @@ fn test_encode() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode_int() ? {
|
||||
fn test_encode_int() {
|
||||
for input, expected in {
|
||||
0x6263: '8VG'
|
||||
0x61: '2g'
|
||||
0x626262: 'a3gV'
|
||||
0x636363: 'aPEr'
|
||||
} {
|
||||
output := base58.encode_int(input)?
|
||||
output := base58.encode_int(input)!
|
||||
println('> input: 0x${input:x} | => output: `$output`')
|
||||
assert output == expected
|
||||
}
|
||||
}
|
||||
|
||||
fn test_decode() ? {
|
||||
fn test_decode() {
|
||||
for output, expected in {
|
||||
'USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z': 'The quick brown fox jumps over the lazy dog.'
|
||||
'11StV1DL6CwTryKyV': '\x00\x00hello world'
|
||||
@@ -35,7 +35,7 @@ fn test_decode() ? {
|
||||
'14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3': hex.decode('0027b5891b01da2db74cde1689a97a2acbe23d5fb1c0205bf6')?.bytestr()
|
||||
'3vQB7B6MrGQZaxCuFg4oh': hex.decode('68656c6c6f20776f726c64bc62d4b8')?.bytestr()
|
||||
} {
|
||||
input := base58.decode(output)?
|
||||
input := base58.decode(output)!
|
||||
println('> output: `$output` | decoded input: `$input` | bytes: $input.bytes().hex()')
|
||||
assert input.bytes().hex() == expected.bytes().hex()
|
||||
}
|
||||
|
||||
@@ -68,8 +68,8 @@ pub fn new_reader(data string, config ReaderConfig) &Reader {
|
||||
|
||||
// read reads a row from the CSV data.
|
||||
// If successful, the result holds an array of each column's data.
|
||||
pub fn (mut r Reader) read() ?[]string {
|
||||
l := r.read_record()?
|
||||
pub fn (mut r Reader) read() ![]string {
|
||||
l := r.read_record()!
|
||||
return l
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ pub fn (mut r Reader) read() ?[]string {
|
||||
// }
|
||||
// return records
|
||||
// }
|
||||
fn (mut r Reader) read_line() ?string {
|
||||
fn (mut r Reader) read_line() !string {
|
||||
// last record
|
||||
if r.row_pos == r.data.len {
|
||||
return IError(&EndOfFileError{})
|
||||
@@ -119,7 +119,7 @@ fn (mut r Reader) read_line() ?string {
|
||||
return line
|
||||
}
|
||||
|
||||
fn (mut r Reader) read_record() ?[]string {
|
||||
fn (mut r Reader) read_record() ![]string {
|
||||
if r.delimiter == r.comment {
|
||||
return IError(&CommentIsDelimiterError{})
|
||||
}
|
||||
@@ -133,7 +133,7 @@ fn (mut r Reader) read_record() ?[]string {
|
||||
mut i := -1
|
||||
for {
|
||||
if need_read {
|
||||
l := r.read_line()?
|
||||
l := r.read_line()!
|
||||
if l.len <= 0 {
|
||||
if keep_raw {
|
||||
line += '\n'
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn new_writer(config WriterConfig) &Writer {
|
||||
}
|
||||
|
||||
// write writes a single record
|
||||
pub fn (mut w Writer) write(record []string) ?bool {
|
||||
pub fn (mut w Writer) write(record []string) !bool {
|
||||
if !valid_delim(w.delimiter) {
|
||||
return IError(&InvalidDelimiterError{})
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import strings
|
||||
// decode converts a hex string into an array of bytes. The expected
|
||||
// input format is 2 ASCII characters for each output byte. If the provided
|
||||
// string length is not a multiple of 2, an implicit `0` is prepended to it.
|
||||
pub fn decode(s string) ?[]u8 {
|
||||
pub fn decode(s string) ![]u8 {
|
||||
mut hex_str := s
|
||||
if hex_str.len >= 2 {
|
||||
if s[0] == `0` && (s[1] == `x` || s[1] == `X`) {
|
||||
@@ -15,24 +15,24 @@ pub fn decode(s string) ?[]u8 {
|
||||
if hex_str.len == 0 {
|
||||
return []u8{}
|
||||
} else if hex_str.len == 1 {
|
||||
return [char2nibble(hex_str[0])?]
|
||||
return [char2nibble(hex_str[0])!]
|
||||
} else if hex_str.len == 2 {
|
||||
n1 := char2nibble(hex_str[0])?
|
||||
n0 := char2nibble(hex_str[1])?
|
||||
n1 := char2nibble(hex_str[0])!
|
||||
n0 := char2nibble(hex_str[1])!
|
||||
return [(n1 << 4) | n0]
|
||||
}
|
||||
// calculate the first byte depending on if hex_str.len is odd
|
||||
mut val := char2nibble(hex_str[0])?
|
||||
mut val := char2nibble(hex_str[0])!
|
||||
if hex_str.len & 1 == 0 {
|
||||
val = (val << 4) | char2nibble(hex_str[1])?
|
||||
val = (val << 4) | char2nibble(hex_str[1])!
|
||||
}
|
||||
// set cap to hex_str.len/2 rounded up
|
||||
mut bytes := []u8{len: 1, cap: (hex_str.len + 1) >> 1, init: val}
|
||||
// iterate over every 2 bytes
|
||||
// the start index depends on if hex_str.len is odd
|
||||
for i := 2 - (hex_str.len & 1); i < hex_str.len; i += 2 {
|
||||
n1 := char2nibble(hex_str[i])?
|
||||
n0 := char2nibble(hex_str[i + 1])?
|
||||
n1 := char2nibble(hex_str[i])!
|
||||
n0 := char2nibble(hex_str[i + 1])!
|
||||
bytes << (n1 << 4) | n0
|
||||
}
|
||||
return bytes
|
||||
@@ -52,7 +52,7 @@ pub fn encode(bytes []u8) string {
|
||||
}
|
||||
|
||||
// char2nibble converts an ASCII hex character to it's hex value
|
||||
fn char2nibble(b u8) ?u8 {
|
||||
fn char2nibble(b u8) !u8 {
|
||||
match b {
|
||||
`0`...`9` { return b - u8(`0`) }
|
||||
`A`...`F` { return b - u8(`A`) + 10 }
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
module hex
|
||||
|
||||
fn test_decode() ? {
|
||||
assert decode('')? == []
|
||||
assert decode('0')? == [u8(0x0)]
|
||||
assert decode('f')? == [u8(0xf)]
|
||||
assert decode('0f')? == [u8(0x0f)]
|
||||
assert decode('ff')? == [u8(0xff)]
|
||||
assert decode('123')? == [u8(0x1), 0x23]
|
||||
assert decode('1234')? == [u8(0x12), 0x34]
|
||||
assert decode('12345')? == [u8(0x1), 0x23, 0x45]
|
||||
assert decode('0123456789abcdef')? == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
assert decode('123456789ABCDEF')? == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
fn test_decode() {
|
||||
assert decode('')! == []
|
||||
assert decode('0')! == [u8(0x0)]
|
||||
assert decode('f')! == [u8(0xf)]
|
||||
assert decode('0f')! == [u8(0x0f)]
|
||||
assert decode('ff')! == [u8(0xff)]
|
||||
assert decode('123')! == [u8(0x1), 0x23]
|
||||
assert decode('1234')! == [u8(0x12), 0x34]
|
||||
assert decode('12345')! == [u8(0x1), 0x23, 0x45]
|
||||
assert decode('0123456789abcdef')! == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
assert decode('123456789ABCDEF')! == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
}
|
||||
|
||||
fn test_decode_fails() ? {
|
||||
fn test_decode_fails() ! {
|
||||
if x := decode('foo') {
|
||||
return error('expected decode to fail, got $x')
|
||||
}
|
||||
@@ -31,24 +31,24 @@ fn test_decode_fails() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode() ? {
|
||||
assert encode(decode('')?) == ''
|
||||
assert encode(decode('0')?) == '00'
|
||||
assert encode(decode('f')?) == '0f'
|
||||
assert encode(decode('0f')?) == '0f'
|
||||
assert encode(decode('ff')?) == 'ff'
|
||||
assert encode(decode('123')?) == '0123'
|
||||
assert encode(decode('1234')?) == '1234'
|
||||
assert encode(decode('12345')?) == '012345'
|
||||
assert encode(decode('abcdef')?) == 'abcdef'
|
||||
assert encode(decode('ABCDEF')?) == 'abcdef'
|
||||
fn test_encode() {
|
||||
assert encode(decode('')!) == ''
|
||||
assert encode(decode('0')!) == '00'
|
||||
assert encode(decode('f')!) == '0f'
|
||||
assert encode(decode('0f')!) == '0f'
|
||||
assert encode(decode('ff')!) == 'ff'
|
||||
assert encode(decode('123')!) == '0123'
|
||||
assert encode(decode('1234')!) == '1234'
|
||||
assert encode(decode('12345')!) == '012345'
|
||||
assert encode(decode('abcdef')!) == 'abcdef'
|
||||
assert encode(decode('ABCDEF')!) == 'abcdef'
|
||||
}
|
||||
|
||||
fn test_decode_0x() ? {
|
||||
assert decode('0x')? == []
|
||||
assert decode('0x0')? == [u8(0x0)]
|
||||
assert decode('0X1234')? == [u8(0x12), 0x34]
|
||||
assert decode('0x12345')? == [u8(0x1), 0x23, 0x45]
|
||||
assert decode('0x0123456789abcdef')? == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
assert decode('0X123456789ABCDEF')? == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
fn test_decode_0x() {
|
||||
assert decode('0x')! == []
|
||||
assert decode('0x0')! == [u8(0x0)]
|
||||
assert decode('0X1234')! == [u8(0x12), 0x34]
|
||||
assert decode('0x12345')! == [u8(0x1), 0x23, 0x45]
|
||||
assert decode('0x0123456789abcdef')! == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
assert decode('0X123456789ABCDEF')! == [u8(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user