builtin: add method `byte()` for string (#11998)

pull/12012/head
Ken 2021-09-28 16:08:10 +09:00 committed by GitHub
parent c151e075e1
commit a17b943e87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View File

@ -459,6 +459,11 @@ pub fn (s string) f64() f64 {
return strconv.atof64(s) return strconv.atof64(s)
} }
// u8 returns the value of the string as u8 `'1'.u8() == u8(1)`.
pub fn (s string) byte() u8 {
return byte(strconv.common_parse_uint(s, 0, 8, false, false) or { 0 })
}
// u16 returns the value of the string as u16 `'1'.u16() == u16(1)`. // u16 returns the value of the string as u16 `'1'.u16() == u16(1)`.
pub fn (s string) u16() u16 { pub fn (s string) u16() u16 {
return u16(strconv.common_parse_uint(s, 0, 16, false, false) or { 0 }) return u16(strconv.common_parse_uint(s, 0, 16, false, false) or { 0 })

View File

@ -418,6 +418,7 @@ fn test_arr_contains() {
fn test_to_num() { fn test_to_num() {
s := '7' s := '7'
assert s.int() == 7 assert s.int() == 7
assert s.byte() == 7
assert s.u64() == 7 assert s.u64() == 7
f := '71.5 hasdf' f := '71.5 hasdf'
// QTODO // QTODO