mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: replace []byte with []u8
This commit is contained in:
@@ -4,7 +4,7 @@ module io
|
||||
struct BufferedReader {
|
||||
mut:
|
||||
reader Reader
|
||||
buf []byte
|
||||
buf []u8
|
||||
offset int // current offset in the buffer
|
||||
len int
|
||||
fails int // how many times fill_buffer has read 0 bytes in a row
|
||||
@@ -28,7 +28,7 @@ pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader {
|
||||
// create
|
||||
r := &BufferedReader{
|
||||
reader: o.reader
|
||||
buf: []byte{len: o.cap, cap: o.cap}
|
||||
buf: []u8{len: o.cap, cap: o.cap}
|
||||
offset: 0
|
||||
mfails: o.retries
|
||||
}
|
||||
@@ -36,7 +36,7 @@ pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader {
|
||||
}
|
||||
|
||||
// read fufills the Reader interface
|
||||
pub fn (mut r BufferedReader) read(mut buf []byte) ?int {
|
||||
pub fn (mut r BufferedReader) read(mut buf []u8) ?int {
|
||||
if r.end_of_stream {
|
||||
return none
|
||||
}
|
||||
@@ -108,7 +108,7 @@ pub fn (mut r BufferedReader) read_line() ?string {
|
||||
if r.end_of_stream {
|
||||
return none
|
||||
}
|
||||
mut line := []byte{}
|
||||
mut line := []u8{}
|
||||
for {
|
||||
if r.needs_fill() {
|
||||
// go fetch some new data
|
||||
|
||||
@@ -10,7 +10,7 @@ fn imin(a int, b int) int {
|
||||
return if a < b { a } else { b }
|
||||
}
|
||||
|
||||
fn (mut s StringReader) read(mut buf []byte) ?int {
|
||||
fn (mut s StringReader) read(mut buf []u8) ?int {
|
||||
$if debug {
|
||||
eprintln('>>>> StringReader.read output buf.len: $buf.len')
|
||||
}
|
||||
@@ -24,14 +24,14 @@ fn (mut s StringReader) read(mut buf []byte) ?int {
|
||||
return read
|
||||
}
|
||||
|
||||
fn read_from_string(text string, capacity int) []byte {
|
||||
fn read_from_string(text string, capacity int) []u8 {
|
||||
mut str := StringReader{
|
||||
text: text
|
||||
}
|
||||
mut stream := io.new_buffered_reader(reader: str, cap: capacity)
|
||||
//
|
||||
mut buf := []byte{len: 1}
|
||||
mut res := []byte{}
|
||||
mut buf := []u8{len: 1}
|
||||
mut res := []u8{}
|
||||
mut i := 0
|
||||
for {
|
||||
z := stream.read(mut buf) or { break }
|
||||
@@ -50,7 +50,7 @@ pub fn test_reading_from_a_string() {
|
||||
assert read_from_string('ab', capacity) == [u8(`a`), `b`]
|
||||
assert read_from_string('abc', capacity) == [u8(`a`), `b`, `c`]
|
||||
assert read_from_string('abcde', capacity) == [u8(`a`), `b`, `c`, `d`, `e`]
|
||||
large_string_bytes := []byte{len: 1000, init: `x`}
|
||||
large_string_bytes := []u8{len: 1000, init: `x`}
|
||||
large_string := large_string_bytes.bytestr()
|
||||
assert read_from_string(large_string, capacity) == large_string_bytes
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ const (
|
||||
)
|
||||
|
||||
pub fn cp(mut src Reader, mut dst Writer) ? {
|
||||
mut buf := []byte{len: io.buf_max_len}
|
||||
mut buf := []u8{len: io.buf_max_len}
|
||||
for {
|
||||
len := src.read(mut buf) or { break }
|
||||
dst.write(buf[..len]) or { return err }
|
||||
|
||||
@@ -2,17 +2,17 @@ import io
|
||||
|
||||
struct Buf {
|
||||
pub:
|
||||
bytes []byte
|
||||
bytes []u8
|
||||
mut:
|
||||
i int
|
||||
}
|
||||
|
||||
struct Writ {
|
||||
pub mut:
|
||||
bytes []byte
|
||||
bytes []u8
|
||||
}
|
||||
|
||||
fn (mut b Buf) read(mut buf []byte) ?int {
|
||||
fn (mut b Buf) read(mut buf []u8) ?int {
|
||||
if !(b.i < b.bytes.len) {
|
||||
return none
|
||||
}
|
||||
@@ -21,7 +21,7 @@ fn (mut b Buf) read(mut buf []byte) ?int {
|
||||
return n
|
||||
}
|
||||
|
||||
fn (mut w Writ) write(buf []byte) ?int {
|
||||
fn (mut w Writ) write(buf []u8) ?int {
|
||||
if buf.len <= 0 {
|
||||
return none
|
||||
}
|
||||
@@ -34,7 +34,7 @@ fn test_copy() {
|
||||
bytes: 'abcdefghij'.repeat(10).bytes()
|
||||
}
|
||||
mut dst := Writ{
|
||||
bytes: []byte{}
|
||||
bytes: []u8{}
|
||||
}
|
||||
io.cp(mut src, mut dst) or { assert false }
|
||||
assert dst.bytes == src.bytes
|
||||
|
||||
@@ -22,7 +22,7 @@ pub mut:
|
||||
// written. If any writer fails to write the full length an error is returned
|
||||
// and writing to other writers stops. If any writer returns an error the error
|
||||
// is returned immediately and writing to other writers stops.
|
||||
pub fn (mut m MultiWriter) write(buf []byte) ?int {
|
||||
pub fn (mut m MultiWriter) write(buf []u8) ?int {
|
||||
for mut w in m.writers {
|
||||
n := w.write(buf) ?
|
||||
if n != buf.len {
|
||||
|
||||
@@ -40,20 +40,20 @@ fn test_multi_writer_write_error() {
|
||||
|
||||
struct TestWriter {
|
||||
pub mut:
|
||||
bytes []byte
|
||||
bytes []u8
|
||||
}
|
||||
|
||||
fn (mut w TestWriter) write(buf []byte) ?int {
|
||||
fn (mut w TestWriter) write(buf []u8) ?int {
|
||||
w.bytes << buf
|
||||
return buf.len
|
||||
}
|
||||
|
||||
struct TestIncompleteWriter {
|
||||
pub mut:
|
||||
bytes []byte
|
||||
bytes []u8
|
||||
}
|
||||
|
||||
fn (mut w TestIncompleteWriter) write(buf []byte) ?int {
|
||||
fn (mut w TestIncompleteWriter) write(buf []u8) ?int {
|
||||
b := buf[..buf.len - 1]
|
||||
w.bytes << b
|
||||
return b.len
|
||||
@@ -61,6 +61,6 @@ fn (mut w TestIncompleteWriter) write(buf []byte) ?int {
|
||||
|
||||
struct TestErrorWriter {}
|
||||
|
||||
fn (mut w TestErrorWriter) write(buf []byte) ?int {
|
||||
fn (mut w TestErrorWriter) write(buf []u8) ?int {
|
||||
return error('error writer errored')
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ pub interface Reader {
|
||||
// A type that implements this should return
|
||||
// `none` on end of stream (EOF) instead of just returning 0
|
||||
mut:
|
||||
read(mut buf []byte) ?int
|
||||
read(mut buf []u8) ?int
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -25,11 +25,11 @@ mut:
|
||||
|
||||
// read_all reads all bytes from a reader until either a 0 length read
|
||||
// or if read_to_end_of_stream is true then the end of the stream (`none`)
|
||||
pub fn read_all(config ReadAllConfig) ?[]byte {
|
||||
pub fn read_all(config ReadAllConfig) ?[]u8 {
|
||||
mut r := config.reader
|
||||
read_till_eof := config.read_to_end_of_stream
|
||||
|
||||
mut b := []byte{len: io.read_all_len}
|
||||
mut b := []u8{len: io.read_all_len}
|
||||
mut read := 0
|
||||
for {
|
||||
new_read := r.read(mut b[read..]) or { break }
|
||||
@@ -46,8 +46,8 @@ pub fn read_all(config ReadAllConfig) ?[]byte {
|
||||
|
||||
// read_any reads any available bytes from a reader
|
||||
// (until the reader returns a read of 0 length)
|
||||
pub fn read_any(mut r Reader) ?[]byte {
|
||||
mut b := []byte{len: io.read_all_len}
|
||||
pub fn read_any(mut r Reader) ?[]u8 {
|
||||
mut b := []u8{len: io.read_all_len}
|
||||
mut read := 0
|
||||
for {
|
||||
new_read := r.read(mut b[read..]) or { break }
|
||||
@@ -64,5 +64,5 @@ pub fn read_any(mut r Reader) ?[]byte {
|
||||
|
||||
// RandomReader represents a stream of data that can be read from at a random location
|
||||
pub interface RandomReader {
|
||||
read_from(pos u64, mut buf []byte) ?int
|
||||
read_from(pos u64, mut buf []u8) ?int
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ module io
|
||||
|
||||
struct Buf {
|
||||
pub:
|
||||
bytes []byte
|
||||
bytes []u8
|
||||
mut:
|
||||
i int
|
||||
}
|
||||
|
||||
fn (mut b Buf) read(mut buf []byte) ?int {
|
||||
fn (mut b Buf) read(mut buf []u8) ?int {
|
||||
if !(b.i < b.bytes.len) {
|
||||
return none
|
||||
}
|
||||
@@ -44,7 +44,7 @@ mut:
|
||||
place int
|
||||
}
|
||||
|
||||
fn (mut s StringReader) read(mut buf []byte) ?int {
|
||||
fn (mut s StringReader) read(mut buf []u8) ?int {
|
||||
if s.place >= s.text.len {
|
||||
return none
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@ mut:
|
||||
w Writer
|
||||
}
|
||||
|
||||
pub fn (mut r ReaderWriterImpl) read(mut buf []byte) ?int {
|
||||
pub fn (mut r ReaderWriterImpl) read(mut buf []u8) ?int {
|
||||
return r.r.read(mut buf)
|
||||
}
|
||||
|
||||
pub fn (mut r ReaderWriterImpl) write(buf []byte) ?int {
|
||||
pub fn (mut r ReaderWriterImpl) write(buf []u8) ?int {
|
||||
return r.w.write(buf)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ module io
|
||||
// Writer represents a stream of data that can be wrote to
|
||||
pub interface Writer {
|
||||
mut:
|
||||
write(buf []byte) ?int
|
||||
write(buf []u8) ?int
|
||||
}
|
||||
|
||||
// RandomWriter represents a stream of data that can be wrote to
|
||||
// at a random pos
|
||||
pub interface RandomWriter {
|
||||
write_to(pos u64, buf []byte) ?int
|
||||
write_to(pos u64, buf []u8) ?int
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user