mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
vlib: change byte to u8 (#19930)
This commit is contained in:
parent
53e1e5e686
commit
cc220e60a5
22 changed files with 39 additions and 45 deletions
|
@ -2,7 +2,7 @@ module builtin
|
||||||
|
|
||||||
import dlmalloc
|
import dlmalloc
|
||||||
|
|
||||||
fn mm_alloc(size u64) (&byte, Errno) {
|
fn mm_alloc(size u64) (&u8, Errno) {
|
||||||
// BEGIN CONSTS
|
// BEGIN CONSTS
|
||||||
// the constants need to be here, since the initialization of other constants,
|
// the constants need to be here, since the initialization of other constants,
|
||||||
// which happen before these ones would, require malloc
|
// which happen before these ones would, require malloc
|
||||||
|
|
|
@ -337,16 +337,16 @@ fn split_int_errno(rc_in u64) (i64, Errno) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 0 sys_read unsigned int fd char *buf size_t count
|
// 0 sys_read unsigned int fd char *buf size_t count
|
||||||
pub fn sys_read(fd i64, buf &byte, count u64) (i64, Errno) {
|
pub fn sys_read(fd i64, buf &u8, count u64) (i64, Errno) {
|
||||||
return split_int_errno(sys_call3(0, u64(fd), u64(buf), count))
|
return split_int_errno(sys_call3(0, u64(fd), u64(buf), count))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1 sys_write unsigned int fd, const char *buf, size_t count
|
// 1 sys_write unsigned int fd, const char *buf, size_t count
|
||||||
pub fn sys_write(fd i64, buf &byte, count u64) (i64, Errno) {
|
pub fn sys_write(fd i64, buf &u8, count u64) (i64, Errno) {
|
||||||
return split_int_errno(sys_call3(1, u64(fd), u64(buf), count))
|
return split_int_errno(sys_call3(1, u64(fd), u64(buf), count))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sys_open(filename &byte, flags i64, mode int) (i64, Errno) {
|
pub fn sys_open(filename &u8, flags i64, mode int) (i64, Errno) {
|
||||||
// 2 sys_open const char *filename int flags int mode
|
// 2 sys_open const char *filename int flags int mode
|
||||||
return split_int_errno(sys_call3(2, u64(filename), u64(flags), u64(mode)))
|
return split_int_errno(sys_call3(2, u64(filename), u64(flags), u64(mode)))
|
||||||
}
|
}
|
||||||
|
@ -357,7 +357,7 @@ pub fn sys_close(fd i64) Errno {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9 sys_mmap unsigned long addr unsigned long len unsigned long prot unsigned long flags unsigned long fd unsigned long off
|
// 9 sys_mmap unsigned long addr unsigned long len unsigned long prot unsigned long flags unsigned long fd unsigned long off
|
||||||
pub fn sys_mmap(addr &byte, len u64, prot Mm_prot, flags Map_flags, fildes u64, off u64) (&byte, Errno) {
|
pub fn sys_mmap(addr &u8, len u64, prot Mm_prot, flags Map_flags, fildes u64, off u64) (&u8, Errno) {
|
||||||
rc := sys_call6(9, u64(addr), len, u64(prot), u64(flags), fildes, off)
|
rc := sys_call6(9, u64(addr), len, u64(prot), u64(flags), fildes, off)
|
||||||
a, e := split_int_errno(rc)
|
a, e := split_int_errno(rc)
|
||||||
return &u8(a), e
|
return &u8(a), e
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub fn mm_pages(size u64) u32 {
|
||||||
return u32(pages)
|
return u32(pages)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mm_alloc(size u64) (&byte, Errno) {
|
pub fn mm_alloc(size u64) (&u8, Errno) {
|
||||||
pages := mm_pages(size)
|
pages := mm_pages(size)
|
||||||
n_bytes := u64(pages * u32(Linux_mem.page_size))
|
n_bytes := u64(pages * u32(Linux_mem.page_size))
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ pub fn mm_alloc(size u64) (&byte, Errno) {
|
||||||
return &u8(0), e
|
return &u8(0), e
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mm_free(addr &byte) Errno {
|
pub fn mm_free(addr &u8) Errno {
|
||||||
ap := &int(addr - 4)
|
ap := &int(addr - 4)
|
||||||
size := u64(*ap) * u64(Linux_mem.page_size)
|
size := u64(*ap) * u64(Linux_mem.page_size)
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ pub fn mem_copy(dest0 voidptr, src0 voidptr, n isize) voidptr {
|
||||||
}
|
}
|
||||||
|
|
||||||
@[unsafe]
|
@[unsafe]
|
||||||
pub fn malloc(n isize) &byte {
|
pub fn malloc(n isize) &u8 {
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
panic('malloc(<0)')
|
panic('malloc(<0)')
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,17 @@ module builtin
|
||||||
|
|
||||||
pub struct string {
|
pub struct string {
|
||||||
pub:
|
pub:
|
||||||
str &byte
|
str &u8
|
||||||
len int
|
len int
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn strlen(s &byte) int {
|
pub fn strlen(s &u8) int {
|
||||||
mut i := 0
|
mut i := 0
|
||||||
for ; s[i] != 0; i++ {}
|
for ; s[i] != 0; i++ {}
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tos(s &byte, len int) string {
|
pub fn tos(s &u8, len int) string {
|
||||||
if s == 0 {
|
if s == 0 {
|
||||||
panic('tos(): nil string')
|
panic('tos(): nil string')
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ pub fn tos_clone(s byteptr) string {
|
||||||
|
|
||||||
// Same as `tos`, but calculates the length. Called by `string(bytes)` casts.
|
// Same as `tos`, but calculates the length. Called by `string(bytes)` casts.
|
||||||
// Used only internally.
|
// Used only internally.
|
||||||
pub fn tos2(s &byte) string {
|
pub fn tos2(s &u8) string {
|
||||||
if s == 0 {
|
if s == 0 {
|
||||||
panic('tos2: nil string')
|
panic('tos2: nil string')
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ pub fn string_ne(s1 string, s2 string) bool {
|
||||||
return !string_eq(s1, s2)
|
return !string_eq(s1, s2)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn i64_tos(buf &byte, len int, n0 i64, base int) string {
|
pub fn i64_tos(buf &u8, len int, n0 i64, base int) string {
|
||||||
if base < 2 {
|
if base < 2 {
|
||||||
panic('base must be >= 2')
|
panic('base must be >= 2')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module builtin
|
module builtin
|
||||||
|
|
||||||
fn C.wyhash(&byte, u64, u64, &u64) u64
|
fn C.wyhash(&u8, u64, u64, &u64) u64
|
||||||
|
|
||||||
fn C.wyhash64(u64, u64) u64
|
fn C.wyhash64(u64, u64) u64
|
||||||
|
|
||||||
|
|
|
@ -105,11 +105,11 @@ fn memcmp(a &C.void, b &C.void, n usize) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vsprintf(str &char, format &char, ap &byte) int {
|
fn vsprintf(str &char, format &char, ap &u8) int {
|
||||||
panic('vsprintf(): string interpolation is not supported in `-freestanding`')
|
panic('vsprintf(): string interpolation is not supported in `-freestanding`')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vsnprintf(str &char, size usize, format &char, ap &byte) int {
|
fn vsnprintf(str &char, size usize, format &char, ap &u8) int {
|
||||||
panic('vsnprintf(): string interpolation is not supported in `-freestanding`')
|
panic('vsnprintf(): string interpolation is not supported in `-freestanding`')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,17 +119,17 @@ enum Errno {
|
||||||
}
|
}
|
||||||
|
|
||||||
// not really needed
|
// not really needed
|
||||||
fn bare_read(buf &byte, count u64) (i64, Errno) {
|
fn bare_read(buf &u8, count u64) (i64, Errno) {
|
||||||
return 0, Errno.eerror
|
return 0, Errno.eerror
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bare_print(buf &byte, len u64) {
|
pub fn bare_print(buf &u8, len u64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bare_eprint(buf &byte, len u64) {
|
fn bare_eprint(buf &u8, len u64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(_fd i64, _buf &byte, _count u64) i64 {
|
pub fn write(_fd i64, _buf &u8, _count u64) i64 {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ module rand
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
fn C.arc4random_buf(p &byte, n usize)
|
fn C.arc4random_buf(p &u8, n usize)
|
||||||
|
|
||||||
// read returns an array of `bytes_needed` random bytes read from the OS.
|
// read returns an array of `bytes_needed` random bytes read from the OS.
|
||||||
pub fn read(bytes_needed int) ![]u8 {
|
pub fn read(bytes_needed int) ![]u8 {
|
||||||
|
|
|
@ -6,7 +6,7 @@ module rand
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
fn C.arc4random_buf(p &byte, n usize)
|
fn C.arc4random_buf(p &u8, n usize)
|
||||||
|
|
||||||
// read returns an array of `bytes_needed` random bytes read from the OS.
|
// read returns an array of `bytes_needed` random bytes read from the OS.
|
||||||
pub fn read(bytes_needed int) ![]u8 {
|
pub fn read(bytes_needed int) ![]u8 {
|
||||||
|
|
|
@ -6,7 +6,7 @@ module rand
|
||||||
|
|
||||||
#include <sys/random.h>
|
#include <sys/random.h>
|
||||||
|
|
||||||
fn C.getrandom(p &byte, n usize, flags u32) int
|
fn C.getrandom(p &u8, n usize, flags u32) int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
read_batch_size = 256
|
read_batch_size = 256
|
||||||
|
|
|
@ -351,7 +351,7 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) !(int, bool) {
|
||||||
|
|
||||||
// strip_newlines removes newline characters and returns the number
|
// strip_newlines removes newline characters and returns the number
|
||||||
// of non-newline characters copied to dst.
|
// of non-newline characters copied to dst.
|
||||||
// fn strip_newlines(mut dst []u8, src []byte) int {
|
// fn strip_newlines(mut dst []u8, src []u8) int {
|
||||||
// mut offset := 0
|
// mut offset := 0
|
||||||
// for b in src {
|
// for b in src {
|
||||||
// if b in [`\r`, `\n`] {
|
// if b in [`\r`, `\n`] {
|
||||||
|
|
|
@ -1148,7 +1148,6 @@ fn test_maxof_minof() {
|
||||||
assert maxof[i32]() == 2147483647
|
assert maxof[i32]() == 2147483647
|
||||||
assert maxof[i64]() == 9223372036854775807
|
assert maxof[i64]() == 9223372036854775807
|
||||||
assert maxof[u8]() == 255
|
assert maxof[u8]() == 255
|
||||||
assert maxof[byte]() == 255
|
|
||||||
assert maxof[u16]() == 65535
|
assert maxof[u16]() == 65535
|
||||||
assert maxof[u32]() == 4294967295
|
assert maxof[u32]() == 4294967295
|
||||||
assert maxof[u64]() == 18446744073709551615
|
assert maxof[u64]() == 18446744073709551615
|
||||||
|
@ -1161,7 +1160,6 @@ fn test_maxof_minof() {
|
||||||
assert minof[i32]() == -2147483648
|
assert minof[i32]() == -2147483648
|
||||||
assert minof[i64]() == -9223372036854775807 - 1
|
assert minof[i64]() == -9223372036854775807 - 1
|
||||||
assert minof[u8]() == 0
|
assert minof[u8]() == 0
|
||||||
assert minof[byte]() == 0
|
|
||||||
assert minof[u16]() == 0
|
assert minof[u16]() == 0
|
||||||
assert minof[u32]() == 0
|
assert minof[u32]() == 0
|
||||||
assert minof[u64]() == 0
|
assert minof[u64]() == 0
|
||||||
|
|
|
@ -188,7 +188,7 @@ fn C.stbi_write_bmp(filename &char, w int, h int, comp int, buffer &u8) int
|
||||||
fn C.stbi_write_tga(filename &char, w int, h int, comp int, buffer &u8) int
|
fn C.stbi_write_tga(filename &char, w int, h int, comp int, buffer &u8) int
|
||||||
fn C.stbi_write_jpg(filename &char, w int, h int, comp int, buffer &u8, quality int) int
|
fn C.stbi_write_jpg(filename &char, w int, h int, comp int, buffer &u8, quality int) int
|
||||||
|
|
||||||
// fn C.stbi_write_hdr(filename &char, w int, h int, comp int, buffer &byte) int // buffer &byte => buffer &f32
|
// fn C.stbi_write_hdr(filename &char, w int, h int, comp int, buffer &u8) int // buffer &u8 => buffer &f32
|
||||||
|
|
||||||
// stbi_write_png write on path a PNG file
|
// stbi_write_png write on path a PNG file
|
||||||
// row_stride_in_bytes is usually equal to: w * comp
|
// row_stride_in_bytes is usually equal to: w * comp
|
||||||
|
@ -222,7 +222,7 @@ pub fn stbi_write_jpg(path string, w int, h int, comp int, buf &u8, quality int)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub fn stbi_write_hdr(path string, w int, h int, comp int, buf &byte) ! {
|
pub fn stbi_write_hdr(path string, w int, h int, comp int, buf &u8) ! {
|
||||||
if 0 == C.stbi_write_hdr(&char(path.str), w , h , comp , buf){
|
if 0 == C.stbi_write_hdr(&char(path.str), w , h , comp , buf){
|
||||||
return error('stbi_image failed to write hdr file to "$path"')
|
return error('stbi_image failed to write hdr file to "$path"')
|
||||||
}
|
}
|
||||||
|
|
|
@ -537,7 +537,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
|
||||||
}
|
}
|
||||||
if node.op == .assign {
|
if node.op == .assign {
|
||||||
// `mut arr := [u8(1),2,3]`
|
// `mut arr := [u8(1),2,3]`
|
||||||
// `arr = [byte(4),5,6]`
|
// `arr = [u8(4),5,6]`
|
||||||
left_info := left_sym.info as ast.Array
|
left_info := left_sym.info as ast.Array
|
||||||
left_elem_type := c.table.unaliased_type(left_info.elem_type)
|
left_elem_type := c.table.unaliased_type(left_info.elem_type)
|
||||||
if left_type_unwrapped.nr_muls() == right_type_unwrapped.nr_muls()
|
if left_type_unwrapped.nr_muls() == right_type_unwrapped.nr_muls()
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
fn get_bytes_array() []byte {
|
fn get_bytes_array() []u8 {
|
||||||
return [byte(97), 98, 99]
|
return [u8(97), 98, 99]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_element_aliased_array_method_call() {
|
fn test_element_aliased_array_method_call() {
|
||||||
assert get_bytes_array().bytestr() == 'abc'
|
assert get_bytes_array().bytestr() == 'abc'
|
||||||
|
|
||||||
arr := [byte(97), 98, 99]
|
arr := [u8(97), 98, 99]
|
||||||
assert arr.bytestr() == 'abc'
|
assert arr.bytestr() == 'abc'
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
fn test_assign_array_of_aliases() {
|
fn test_assign_array_of_aliases() {
|
||||||
mut arr := [u8(1), 2, 3]
|
mut arr := [u8(1), 2, 3]
|
||||||
arr = [byte(4), 5, 6]
|
arr = [u8(4), 5, 6]
|
||||||
|
|
||||||
println(arr)
|
println(arr)
|
||||||
assert arr.len == 3
|
assert arr.len == 3
|
||||||
|
|
|
@ -32,7 +32,6 @@ fn test_primitives() {
|
||||||
check_init_value_for_arrays_of_option[i64]()
|
check_init_value_for_arrays_of_option[i64]()
|
||||||
|
|
||||||
check_init_value_for_arrays_of_option[u8]()
|
check_init_value_for_arrays_of_option[u8]()
|
||||||
check_init_value_for_arrays_of_option[byte]()
|
|
||||||
check_init_value_for_arrays_of_option[u16]()
|
check_init_value_for_arrays_of_option[u16]()
|
||||||
check_init_value_for_arrays_of_option[u32]()
|
check_init_value_for_arrays_of_option[u32]()
|
||||||
check_init_value_for_arrays_of_option[u64]()
|
check_init_value_for_arrays_of_option[u64]()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
type UUID = [16]byte
|
type UUID = [16]u8
|
||||||
|
|
||||||
const codec_test_uuid = UUID([byte(0x6b), 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4,
|
const codec_test_uuid = UUID([u8(0x6b), 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00,
|
||||||
0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8]!)
|
0xc0, 0x4f, 0xd4, 0x30, 0xc8]!)
|
||||||
|
|
||||||
fn test_const_from_bytes() {
|
fn test_const_from_bytes() {
|
||||||
println(codec_test_uuid)
|
println(codec_test_uuid)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
fn f1(mut b []byte) string {
|
fn f1(mut b []u8) string {
|
||||||
return '${b}'
|
return '${b}'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn f2(b []byte) string {
|
fn f2(b []u8) string {
|
||||||
return '${b}'
|
return '${b}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn (f Foo) show_array_of_u8(data []u8) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fn_with_array_of_aliases_argument() {
|
fn test_fn_with_array_of_aliases_argument() {
|
||||||
a := [byte(1), 2, 3]
|
a := [u8(1), 2, 3]
|
||||||
|
|
||||||
s1 := show_array_of_u8(a)
|
s1 := show_array_of_u8(a)
|
||||||
println(s1)
|
println(s1)
|
||||||
|
|
|
@ -27,7 +27,6 @@ fn test_typeof_for_builtin_int_types() {
|
||||||
assert typeof(u32(1)).name == 'u32'
|
assert typeof(u32(1)).name == 'u32'
|
||||||
assert typeof(u64(1)).name == 'u64'
|
assert typeof(u64(1)).name == 'u64'
|
||||||
//
|
//
|
||||||
assert typeof(byte(1)).name == 'byte'
|
|
||||||
assert typeof(char(1)).name == 'char'
|
assert typeof(char(1)).name == 'char'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -429,8 +429,6 @@ fn (e &Encoder) encode_array[U](val []U, level int, mut wr io.Writer) ! {
|
||||||
e.encode_any(i64(val[i]), level + 1, mut wr)!
|
e.encode_any(i64(val[i]), level + 1, mut wr)!
|
||||||
} $else $if U is u8 {
|
} $else $if U is u8 {
|
||||||
e.encode_any(u8(val[i]), level + 1, mut wr)!
|
e.encode_any(u8(val[i]), level + 1, mut wr)!
|
||||||
} $else $if U is byte {
|
|
||||||
e.encode_any(u8(val[i]), level + 1, mut wr)!
|
|
||||||
} $else $if U is u16 {
|
} $else $if U is u16 {
|
||||||
e.encode_any(u16(val[i]), level + 1, mut wr)!
|
e.encode_any(u16(val[i]), level + 1, mut wr)!
|
||||||
} $else $if U is u32 {
|
} $else $if U is u32 {
|
||||||
|
|
|
@ -176,11 +176,11 @@ fn test_encode_alias_struct() {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StByteArray {
|
struct StByteArray {
|
||||||
ba []byte
|
ba []u8
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_byte_array() {
|
fn test_byte_array() {
|
||||||
assert json.encode(StByteArray{ ba: [byte(1), 2, 3, 4, 5] }) == '{"ba":[1,2,3,4,5]}'
|
assert json.encode(StByteArray{ ba: [u8(1), 2, 3, 4, 5] }) == '{"ba":[1,2,3,4,5]}'
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Bar {
|
struct Bar {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue