mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: change optional to result of io (#16075)
This commit is contained in:
@@ -393,7 +393,7 @@ fn converter(mut pn PrepNumber) u64 {
|
||||
}
|
||||
|
||||
// atof64 parses the string `s`, and if possible, converts it into a f64 number
|
||||
pub fn atof64(s string) ?f64 {
|
||||
pub fn atof64(s string) !f64 {
|
||||
if s.len == 0 {
|
||||
return error('expected a number found an empty string')
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module strconv
|
||||
|
||||
// atof64 return a f64 from a string doing a parsing operation
|
||||
pub fn atof64(s string) ?f64 {
|
||||
pub fn atof64(s string) !f64 {
|
||||
// TODO: handle parsing invalid numbers as close as possible to the pure V version
|
||||
// that may be slower, but more portable, and will guarantee that higher level code
|
||||
// works the same in the JS version, as well as in the C and Native versions.
|
||||
|
||||
@@ -19,7 +19,7 @@ pub fn byte_to_lower(c u8) u8 {
|
||||
|
||||
// common_parse_uint is called by parse_uint and allows the parsing
|
||||
// to stop on non or invalid digit characters and return with an error
|
||||
pub fn common_parse_uint(s string, _base int, _bit_size int, error_on_non_digit bool, error_on_high_digit bool) ?u64 {
|
||||
pub fn common_parse_uint(s string, _base int, _bit_size int, error_on_non_digit bool, error_on_high_digit bool) !u64 {
|
||||
result, err := common_parse_uint2(s, _base, _bit_size)
|
||||
// TODO: error_on_non_digit and error_on_high_digit have no difference
|
||||
if err != 0 && (error_on_non_digit || error_on_high_digit) {
|
||||
@@ -120,14 +120,14 @@ pub fn common_parse_uint2(s string, _base int, _bit_size int) (u64, int) {
|
||||
}
|
||||
|
||||
// parse_uint is like parse_int but for unsigned numbers.
|
||||
pub fn parse_uint(s string, _base int, _bit_size int) ?u64 {
|
||||
pub fn parse_uint(s string, _base int, _bit_size int) !u64 {
|
||||
return common_parse_uint(s, _base, _bit_size, true, true)
|
||||
}
|
||||
|
||||
// common_parse_int is called by parse int and allows the parsing
|
||||
// to stop on non or invalid digit characters and return with an error
|
||||
[direct_array_access]
|
||||
pub fn common_parse_int(_s string, base int, _bit_size int, error_on_non_digit bool, error_on_high_digit bool) ?i64 {
|
||||
pub fn common_parse_int(_s string, base int, _bit_size int, error_on_non_digit bool, error_on_high_digit bool) !i64 {
|
||||
if _s.len < 1 {
|
||||
// return error('parse_int: syntax error $s')
|
||||
return i64(0)
|
||||
@@ -149,7 +149,7 @@ pub fn common_parse_int(_s string, base int, _bit_size int, error_on_non_digit b
|
||||
// un := parse_uint(s, base, bit_size) or {
|
||||
// return i64(0)
|
||||
// }
|
||||
un := common_parse_uint(s, base, bit_size, error_on_non_digit, error_on_high_digit)?
|
||||
un := common_parse_uint(s, base, bit_size, error_on_non_digit, error_on_high_digit)!
|
||||
if un == 0 {
|
||||
return i64(0)
|
||||
}
|
||||
@@ -178,13 +178,13 @@ pub fn common_parse_int(_s string, base int, _bit_size int, error_on_non_digit b
|
||||
// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
|
||||
// correspond to int, int8, int16, int32, and int64.
|
||||
// If bitSize is below 0 or above 64, an error is returned.
|
||||
pub fn parse_int(_s string, base int, _bit_size int) ?i64 {
|
||||
pub fn parse_int(_s string, base int, _bit_size int) !i64 {
|
||||
return common_parse_int(_s, base, _bit_size, true, true)
|
||||
}
|
||||
|
||||
// atoi is equivalent to parse_int(s, 10, 0), converted to type int.
|
||||
[direct_array_access]
|
||||
pub fn atoi(s string) ?int {
|
||||
pub fn atoi(s string) !int {
|
||||
if s == '' {
|
||||
return error('strconv.atoi: parsing "": invalid syntax')
|
||||
}
|
||||
@@ -211,7 +211,7 @@ pub fn atoi(s string) ?int {
|
||||
return if s[0] == `-` { -n } else { n }
|
||||
}
|
||||
// Slow path for invalid, big, or underscored integers.
|
||||
int64 := parse_int(s, 10, 0)?
|
||||
int64 := parse_int(s, 10, 0)!
|
||||
return int(int64)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import strconv
|
||||
|
||||
fn test_atoi() ? {
|
||||
assert strconv.atoi('16')? == 16
|
||||
assert strconv.atoi('+16')? == 16
|
||||
assert strconv.atoi('-16')? == -16
|
||||
fn test_atoi() {
|
||||
assert strconv.atoi('16')! == 16
|
||||
assert strconv.atoi('+16')! == 16
|
||||
assert strconv.atoi('-16')! == -16
|
||||
|
||||
// invalid strings
|
||||
if x := strconv.atoi('str') {
|
||||
@@ -26,20 +26,20 @@ fn test_atoi() ? {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_parse_int() ? {
|
||||
fn test_parse_int() {
|
||||
// Different bases
|
||||
assert strconv.parse_int('16', 16, 0)? == 0x16
|
||||
assert strconv.parse_int('16', 8, 0)? == 0o16
|
||||
assert strconv.parse_int('11', 2, 0)? == 3
|
||||
assert strconv.parse_int('16', 16, 0)! == 0x16
|
||||
assert strconv.parse_int('16', 8, 0)! == 0o16
|
||||
assert strconv.parse_int('11', 2, 0)! == 3
|
||||
// Different bit sizes
|
||||
assert strconv.parse_int('127', 10, 8)? == 127
|
||||
assert strconv.parse_int('128', 10, 8)? == 127
|
||||
assert strconv.parse_int('32767', 10, 16)? == 32767
|
||||
assert strconv.parse_int('32768', 10, 16)? == 32767
|
||||
assert strconv.parse_int('2147483647', 10, 32)? == 2147483647
|
||||
assert strconv.parse_int('2147483648', 10, 32)? == 2147483647
|
||||
assert strconv.parse_int('9223372036854775807', 10, 64)? == 9223372036854775807
|
||||
assert strconv.parse_int('9223372036854775808', 10, 64)? == 9223372036854775807
|
||||
assert strconv.parse_int('127', 10, 8)! == 127
|
||||
assert strconv.parse_int('128', 10, 8)! == 127
|
||||
assert strconv.parse_int('32767', 10, 16)! == 32767
|
||||
assert strconv.parse_int('32768', 10, 16)! == 32767
|
||||
assert strconv.parse_int('2147483647', 10, 32)! == 2147483647
|
||||
assert strconv.parse_int('2147483648', 10, 32)! == 2147483647
|
||||
assert strconv.parse_int('9223372036854775807', 10, 64)! == 9223372036854775807
|
||||
assert strconv.parse_int('9223372036854775808', 10, 64)! == 9223372036854775807
|
||||
assert strconv.parse_int('baobab', 36, 64)? == 683058467
|
||||
// Invalid bit sizes
|
||||
if x := strconv.parse_int('123', 10, -1) {
|
||||
|
||||
Reference in New Issue
Block a user