mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: byte => u8
This commit is contained in:
@@ -65,7 +65,7 @@ pub fn encode_walpha(input string, alphabet Alphabet) string {
|
||||
i = sz - 1
|
||||
for carry = u32(b); i > high || carry != 0; i-- {
|
||||
carry = carry + 256 * u32(out[i])
|
||||
out[i] = byte(carry % 58)
|
||||
out[i] = u8(carry % 58)
|
||||
carry /= 58
|
||||
}
|
||||
high = 1
|
||||
@@ -94,7 +94,7 @@ 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 {
|
||||
ch_i := alphabet.encode.bytestr().index_byte(ch)
|
||||
ch_i := alphabet.encode.bytestr().index_u8(ch)
|
||||
if ch_i == -1 {
|
||||
return error(@MOD + '.' + @FN +
|
||||
': input string contains values not found in the provided alphabet')
|
||||
@@ -162,7 +162,7 @@ pub fn decode_walpha(str string, alphabet Alphabet) ?string {
|
||||
mut out_len := 0
|
||||
for j := 0; j < outi.len; j++ {
|
||||
for mask < 32 {
|
||||
binu[out_len] = byte(outi[j] >> mask)
|
||||
binu[out_len] = u8(outi[j] >> mask)
|
||||
mask -= 8
|
||||
out_len++
|
||||
}
|
||||
|
@@ -57,11 +57,11 @@ fn encode_from_buffer(dest &byte, src &byte, src_len int) int {
|
||||
match remain {
|
||||
2 {
|
||||
b[di + 2] = etable[val >> 6 & 0x3F]
|
||||
b[di + 3] = byte(`=`)
|
||||
b[di + 3] = u8(`=`)
|
||||
}
|
||||
1 {
|
||||
b[di + 2] = byte(`=`)
|
||||
b[di + 3] = byte(`=`)
|
||||
b[di + 2] = u8(`=`)
|
||||
b[di + 3] = u8(`=`)
|
||||
}
|
||||
else {
|
||||
panic('base64: This case should never occur.')
|
||||
@@ -125,9 +125,9 @@ fn decode_from_buffer(dest &byte, src &byte, src_len int) int {
|
||||
|
||||
for src_len - si >= 8 {
|
||||
// Converting 8 bytes of input into 6 bytes of output. Storing these in the upper bytes of an u64.
|
||||
datablock_64.data = assemble64(byte(index[d[si + 0]]), byte(index[d[si + 1]]),
|
||||
byte(index[d[si + 2]]), byte(index[d[si + 3]]), byte(index[d[si + 4]]),
|
||||
byte(index[d[si + 5]]), byte(index[d[si + 6]]), byte(index[d[si + 7]]))
|
||||
datablock_64.data = assemble64(u8(index[d[si + 0]]), u8(index[d[si + 1]]),
|
||||
u8(index[d[si + 2]]), u8(index[d[si + 3]]), u8(index[d[si + 4]]),
|
||||
u8(index[d[si + 5]]), u8(index[d[si + 6]]), u8(index[d[si + 7]]))
|
||||
|
||||
// Reading out the individual bytes from the u64. Watch out with endianess.
|
||||
$if little_endian {
|
||||
@@ -151,8 +151,8 @@ fn decode_from_buffer(dest &byte, src &byte, src_len int) int {
|
||||
}
|
||||
|
||||
for src_len - si >= 4 {
|
||||
datablock_32.data = assemble32(byte(index[d[si + 0]]), byte(index[d[si + 1]]),
|
||||
byte(index[d[si + 2]]), byte(index[d[si + 3]]))
|
||||
datablock_32.data = assemble32(u8(index[d[si + 0]]), u8(index[d[si + 1]]),
|
||||
u8(index[d[si + 2]]), u8(index[d[si + 3]]))
|
||||
$if little_endian {
|
||||
b[n_decoded_bytes + 0] = datablock_32.data_byte[3]
|
||||
b[n_decoded_bytes + 1] = datablock_32.data_byte[2]
|
||||
|
@@ -111,23 +111,23 @@ fn test_url_decode_str() {
|
||||
assert test == 'Hello Base64Url encoding!'
|
||||
}
|
||||
|
||||
fn test_encode_null_byte() {
|
||||
assert base64.encode([byte(`A`), 0, `C`]) == 'QQBD'
|
||||
fn test_encode_null_u8() {
|
||||
assert base64.encode([u8(`A`), 0, `C`]) == 'QQBD'
|
||||
}
|
||||
|
||||
fn test_encode_null_byte_str() {
|
||||
// While this works, bytestr() does a memcpy
|
||||
s := [byte(`A`), 0, `C`].bytestr()
|
||||
s := [u8(`A`), 0, `C`].bytestr()
|
||||
assert base64.encode_str(s) == 'QQBD'
|
||||
}
|
||||
|
||||
fn test_decode_null_byte() {
|
||||
assert base64.decode('QQBD') == [byte(`A`), 0, `C`]
|
||||
fn test_decode_null_u8() {
|
||||
assert base64.decode('QQBD') == [u8(`A`), 0, `C`]
|
||||
}
|
||||
|
||||
fn test_decode_null_byte_str() {
|
||||
// While this works, bytestr() does a memcpy
|
||||
s := [byte(`A`), 0, `C`].bytestr()
|
||||
s := [u8(`A`), 0, `C`].bytestr()
|
||||
assert base64.decode_str('QQBD') == s
|
||||
}
|
||||
|
||||
|
@@ -13,8 +13,8 @@ pub fn little_endian_u16(b []byte) u16 {
|
||||
[inline]
|
||||
pub fn little_endian_put_u16(mut b []byte, v u16) {
|
||||
_ = b[1] // bounds check
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> u16(8))
|
||||
b[0] = u8(v)
|
||||
b[1] = u8(v >> u16(8))
|
||||
}
|
||||
|
||||
[inline]
|
||||
@@ -26,10 +26,10 @@ pub fn little_endian_u32(b []byte) u32 {
|
||||
[inline]
|
||||
pub fn little_endian_put_u32(mut b []byte, v u32) {
|
||||
_ = b[3] // bounds check
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> u32(8))
|
||||
b[2] = byte(v >> u32(16))
|
||||
b[3] = byte(v >> u32(24))
|
||||
b[0] = u8(v)
|
||||
b[1] = u8(v >> u32(8))
|
||||
b[2] = u8(v >> u32(16))
|
||||
b[3] = u8(v >> u32(24))
|
||||
}
|
||||
|
||||
[inline]
|
||||
@@ -41,14 +41,14 @@ pub fn little_endian_u64(b []byte) u64 {
|
||||
[inline]
|
||||
pub fn little_endian_put_u64(mut b []byte, v u64) {
|
||||
_ = b[7] // bounds check
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> u64(8))
|
||||
b[2] = byte(v >> u64(16))
|
||||
b[3] = byte(v >> u64(24))
|
||||
b[4] = byte(v >> u64(32))
|
||||
b[5] = byte(v >> u64(40))
|
||||
b[6] = byte(v >> u64(48))
|
||||
b[7] = byte(v >> u64(56))
|
||||
b[0] = u8(v)
|
||||
b[1] = u8(v >> u64(8))
|
||||
b[2] = u8(v >> u64(16))
|
||||
b[3] = u8(v >> u64(24))
|
||||
b[4] = u8(v >> u64(32))
|
||||
b[5] = u8(v >> u64(40))
|
||||
b[6] = u8(v >> u64(48))
|
||||
b[7] = u8(v >> u64(56))
|
||||
}
|
||||
|
||||
// Big Endian
|
||||
@@ -61,8 +61,8 @@ pub fn big_endian_u16(b []byte) u16 {
|
||||
[inline]
|
||||
pub fn big_endian_put_u16(mut b []byte, v u16) {
|
||||
_ = b[1] // bounds check
|
||||
b[0] = byte(v >> u16(8))
|
||||
b[1] = byte(v)
|
||||
b[0] = u8(v >> u16(8))
|
||||
b[1] = u8(v)
|
||||
}
|
||||
|
||||
[inline]
|
||||
@@ -74,10 +74,10 @@ pub fn big_endian_u32(b []byte) u32 {
|
||||
[inline]
|
||||
pub fn big_endian_put_u32(mut b []byte, v u32) {
|
||||
_ = b[3] // bounds check
|
||||
b[0] = byte(v >> u32(24))
|
||||
b[1] = byte(v >> u32(16))
|
||||
b[2] = byte(v >> u32(8))
|
||||
b[3] = byte(v)
|
||||
b[0] = u8(v >> u32(24))
|
||||
b[1] = u8(v >> u32(16))
|
||||
b[2] = u8(v >> u32(8))
|
||||
b[3] = u8(v)
|
||||
}
|
||||
|
||||
[inline]
|
||||
@@ -89,12 +89,12 @@ pub fn big_endian_u64(b []byte) u64 {
|
||||
[inline]
|
||||
pub fn big_endian_put_u64(mut b []byte, v u64) {
|
||||
_ = b[7] // bounds check
|
||||
b[0] = byte(v >> u64(56))
|
||||
b[1] = byte(v >> u64(48))
|
||||
b[2] = byte(v >> u64(40))
|
||||
b[3] = byte(v >> u64(32))
|
||||
b[4] = byte(v >> u64(24))
|
||||
b[5] = byte(v >> u64(16))
|
||||
b[6] = byte(v >> u64(8))
|
||||
b[7] = byte(v)
|
||||
b[0] = u8(v >> u64(56))
|
||||
b[1] = u8(v >> u64(48))
|
||||
b[2] = u8(v >> u64(40))
|
||||
b[3] = u8(v >> u64(32))
|
||||
b[4] = u8(v >> u64(24))
|
||||
b[5] = u8(v >> u64(16))
|
||||
b[6] = u8(v >> u64(8))
|
||||
b[7] = u8(v)
|
||||
}
|
||||
|
@@ -54,9 +54,9 @@ pub fn encode(bytes []byte) string {
|
||||
// char2nibble converts an ASCII hex character to it's hex value
|
||||
fn char2nibble(b byte) ?byte {
|
||||
match b {
|
||||
`0`...`9` { return b - byte(`0`) }
|
||||
`A`...`F` { return b - byte(`A`) + 10 }
|
||||
`a`...`f` { return b - byte(`a`) + 10 }
|
||||
`0`...`9` { return b - u8(`0`) }
|
||||
`A`...`F` { return b - u8(`A`) + 10 }
|
||||
`a`...`f` { return b - u8(`a`) + 10 }
|
||||
else { return error('invalid hex char $b.ascii_str()') }
|
||||
}
|
||||
}
|
||||
|
@@ -2,15 +2,15 @@ module hex
|
||||
|
||||
fn test_decode() ? {
|
||||
assert decode('') ? == []
|
||||
assert decode('0') ? == [byte(0x0)]
|
||||
assert decode('f') ? == [byte(0xf)]
|
||||
assert decode('0f') ? == [byte(0x0f)]
|
||||
assert decode('ff') ? == [byte(0xff)]
|
||||
assert decode('123') ? == [byte(0x1), 0x23]
|
||||
assert decode('1234') ? == [byte(0x12), 0x34]
|
||||
assert decode('12345') ? == [byte(0x1), 0x23, 0x45]
|
||||
assert decode('0123456789abcdef') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
assert decode('123456789ABCDEF') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
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() ? {
|
||||
@@ -46,9 +46,9 @@ fn test_encode() ? {
|
||||
|
||||
fn test_decode_0x() ? {
|
||||
assert decode('0x') ? == []
|
||||
assert decode('0x0') ? == [byte(0x0)]
|
||||
assert decode('0X1234') ? == [byte(0x12), 0x34]
|
||||
assert decode('0x12345') ? == [byte(0x1), 0x23, 0x45]
|
||||
assert decode('0x0123456789abcdef') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
assert decode('0X123456789ABCDEF') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
|
||||
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]
|
||||
}
|
||||
|
@@ -147,7 +147,7 @@ pub fn is_control(r rune) bool {
|
||||
if r > max_latin_1 {
|
||||
return false
|
||||
}
|
||||
return props[byte(r)] == 1
|
||||
return props[u8(r)] == 1
|
||||
}
|
||||
|
||||
// is_letter returns true if the rune is unicode letter or in unicode category L
|
||||
@@ -155,7 +155,7 @@ pub fn is_letter(r rune) bool {
|
||||
if (r >= `a` && r <= `z`) || (r >= `A` && r <= `Z`) {
|
||||
return true
|
||||
} else if r <= max_latin_1 {
|
||||
return props[byte(r)] & p_l_mask != 0
|
||||
return props[u8(r)] & p_l_mask != 0
|
||||
}
|
||||
return is_excluding_latin(letter_table, r)
|
||||
}
|
||||
@@ -405,11 +405,11 @@ fn up_low(s string, upper_flag bool) string {
|
||||
if ch_len == 1 {
|
||||
if upper_flag == true {
|
||||
unsafe {
|
||||
str_res[index] = byte(C.toupper(s.str[index]))
|
||||
str_res[index] = u8(C.toupper(s.str[index]))
|
||||
}
|
||||
} else {
|
||||
unsafe {
|
||||
str_res[index] = byte(C.tolower(s.str[index]))
|
||||
str_res[index] = u8(C.tolower(s.str[index]))
|
||||
}
|
||||
}
|
||||
} else if ch_len > 1 && ch_len < 5 {
|
||||
@@ -451,8 +451,8 @@ fn up_low(s string, upper_flag bool) string {
|
||||
}
|
||||
|
||||
if ch_len == 2 {
|
||||
ch0 := byte((tab_char >> 6) & 0x1f) | 0xc0 // 110x xxxx
|
||||
ch1 := byte((tab_char >> 0) & 0x3f) | 0x80 // 10xx xxxx
|
||||
ch0 := u8((tab_char >> 6) & 0x1f) | 0xc0 // 110x xxxx
|
||||
ch1 := u8((tab_char >> 0) & 0x3f) | 0x80 // 10xx xxxx
|
||||
// C.printf("[%02x%02x] \n",ch0,ch1)
|
||||
|
||||
unsafe {
|
||||
@@ -462,11 +462,11 @@ fn up_low(s string, upper_flag bool) string {
|
||||
//****************************************************************
|
||||
// BUG: doesn't compile, workaround use shitf to right of 0 bit
|
||||
//****************************************************************
|
||||
// str_res[index + 1 ] = byte( tab_char & 0xbf ) // 1011 1111
|
||||
// str_res[index + 1 ] = u8( tab_char & 0xbf ) // 1011 1111
|
||||
} else if ch_len == 3 {
|
||||
ch0 := byte((tab_char >> 12) & 0x0f) | 0xe0 // 1110 xxxx
|
||||
ch1 := byte((tab_char >> 6) & 0x3f) | 0x80 // 10xx xxxx
|
||||
ch2 := byte((tab_char >> 0) & 0x3f) | 0x80 // 10xx xxxx
|
||||
ch0 := u8((tab_char >> 12) & 0x0f) | 0xe0 // 1110 xxxx
|
||||
ch1 := u8((tab_char >> 6) & 0x3f) | 0x80 // 10xx xxxx
|
||||
ch2 := u8((tab_char >> 0) & 0x3f) | 0x80 // 10xx xxxx
|
||||
// C.printf("[%02x%02x%02x] \n",ch0,ch1,ch2)
|
||||
|
||||
unsafe {
|
||||
|
Reference in New Issue
Block a user