mirror of
https://github.com/odin-lang/Odin.git
synced 2026-01-04 20:17:48 +00:00
Improve packages io and strings; add io.Section_Reader
This commit is contained in:
@@ -35,13 +35,16 @@ Error :: enum i32 {
|
||||
Invalid_Offset,
|
||||
Invalid_Unread,
|
||||
|
||||
Negative_Read,
|
||||
Negative_Write,
|
||||
|
||||
// Empty is returned when a procedure has not been implemented for an io.Stream
|
||||
Empty = -1,
|
||||
}
|
||||
|
||||
Close_Proc :: distinct proc(using s: Stream) -> Error;
|
||||
Flush_Proc :: distinct proc(using s: Stream) -> Error;
|
||||
Seek_Proc :: distinct proc(using s: Stream, offset: i64, whence: Seek_From) -> (i64, Error);
|
||||
Seek_Proc :: distinct proc(using s: Stream, offset: i64, whence: Seek_From) -> (n: i64, err: Error);
|
||||
Size_Proc :: distinct proc(using s: Stream) -> i64;
|
||||
Read_Proc :: distinct proc(using s: Stream, p: []byte) -> (n: int, err: Error);
|
||||
Read_At_Proc :: distinct proc(using s: Stream, p: []byte, off: i64) -> (n: int, err: Error);
|
||||
@@ -120,7 +123,10 @@ destroy :: proc(s: Stream) -> Error {
|
||||
if s.stream_vtable != nil && s.impl_destroy != nil {
|
||||
return s->impl_destroy();
|
||||
}
|
||||
return close_err;
|
||||
if close_err != .None {
|
||||
return close_err;
|
||||
}
|
||||
return .Empty;
|
||||
}
|
||||
|
||||
read :: proc(s: Reader, p: []byte) -> (n: int, err: Error) {
|
||||
@@ -364,6 +370,11 @@ write_string :: proc(s: Writer, str: string) -> (n: int, err: Error) {
|
||||
}
|
||||
|
||||
write_rune :: proc(s: Writer, r: rune) -> (n: int, err: Error) {
|
||||
if r < utf8.RUNE_SELF {
|
||||
err = write_byte(s, byte(r));
|
||||
n = 1 if err == nil else 0;
|
||||
return;
|
||||
}
|
||||
buf, w := utf8.encode_rune(r);
|
||||
return write(s, buf[:w]);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,11 @@ _tee_reader_vtable := &Stream_VTable{
|
||||
},
|
||||
};
|
||||
|
||||
// tee_reader
|
||||
// tee_reader returns a Reader that writes to 'w' what it reads from 'r'
|
||||
// All reads from 'r' performed through it are matched with a corresponding write to 'w'
|
||||
// There is no internal buffering done
|
||||
// The write must complete before th read completes
|
||||
// Any error encountered whilst writing is reported as a 'read' error
|
||||
// tee_reader must call io.destroy when done with
|
||||
tee_reader :: proc(r: Reader, w: Writer, allocator := context.allocator) -> (out: Reader) {
|
||||
t := new(Tee_Reader, allocator);
|
||||
@@ -61,9 +65,8 @@ tee_reader :: proc(r: Reader, w: Writer, allocator := context.allocator) -> (out
|
||||
}
|
||||
|
||||
|
||||
// A Limited_Reader reads from r but limits the amount of
|
||||
// data returned to just n bytes. Each call to read
|
||||
// updates n to reflect the new amount remaining.
|
||||
// A Limited_Reader reads from r but limits the amount of data returned to just n bytes.
|
||||
// Each call to read updates n to reflect the new amount remaining.
|
||||
// read returns EOF when n <= 0 or when the underlying r returns EOF.
|
||||
Limited_Reader :: struct {
|
||||
r: Reader, // underlying reader
|
||||
@@ -72,7 +75,7 @@ Limited_Reader :: struct {
|
||||
|
||||
@(private)
|
||||
_limited_reader_vtable := &Stream_VTable{
|
||||
impl_read = proc(using s: Stream, p: []byte) -> (n: int, err: Error) {
|
||||
impl_read = proc(s: Stream, p: []byte) -> (n: int, err: Error) {
|
||||
l := (^Limited_Reader)(s.stream_data);
|
||||
if l.n <= 0 {
|
||||
return 0, .EOF;
|
||||
@@ -106,3 +109,84 @@ inline_limited_reader :: proc(l: ^Limited_Reader, r: Reader, n: i64) -> Reader {
|
||||
l.n = n;
|
||||
return limited_reader_to_reader(l);
|
||||
}
|
||||
|
||||
// Section_Reader implements read, seek, and read_at on a section of an underlying Reader_At
|
||||
Section_Reader :: struct {
|
||||
r: Reader_At,
|
||||
base: i64,
|
||||
off: i64,
|
||||
limit: i64,
|
||||
}
|
||||
|
||||
init_section_reader :: proc(s: ^Section_Reader, r: Reader_At, off: i64, n: i64) {
|
||||
s.r = r;
|
||||
s.off = off;
|
||||
s.limit = off + n;
|
||||
return;
|
||||
}
|
||||
section_reader_to_stream :: proc(s: ^Section_Reader) -> (out: Stream) {
|
||||
out.stream_data = s;
|
||||
out.stream_vtable = _section_reader_vtable;
|
||||
return;
|
||||
}
|
||||
|
||||
@(private)
|
||||
_section_reader_vtable := &Stream_VTable{
|
||||
impl_read = proc(stream: Stream, p: []byte) -> (n: int, err: Error) {
|
||||
s := (^Section_Reader)(stream.stream_data);
|
||||
if s.off >= s.limit {
|
||||
return 0, .EOF;
|
||||
}
|
||||
p := p;
|
||||
if max := s.limit - s.off; i64(len(p)) > max {
|
||||
p = p[0:max];
|
||||
}
|
||||
n, err = read_at(s.r, p, s.off);
|
||||
s.off += i64(n);
|
||||
return;
|
||||
},
|
||||
impl_read_at = proc(stream: Stream, p: []byte, off: i64) -> (n: int, err: Error) {
|
||||
s := (^Section_Reader)(stream.stream_data);
|
||||
p, off := p, off;
|
||||
|
||||
if off < 0 || off >= s.limit - s.base {
|
||||
return 0, .EOF;
|
||||
}
|
||||
off += s.base;
|
||||
if max := s.limit - off; i64(len(p)) > max {
|
||||
p = p[0:max];
|
||||
n, err = read_at(s.r, p, off);
|
||||
if err == nil {
|
||||
err = .EOF;
|
||||
}
|
||||
return;
|
||||
}
|
||||
return read_at(s.r, p, off);
|
||||
},
|
||||
impl_seek = proc(stream: Stream, offset: i64, whence: Seek_From) -> (n: i64, err: Error) {
|
||||
s := (^Section_Reader)(stream.stream_data);
|
||||
|
||||
offset := offset;
|
||||
switch whence {
|
||||
case:
|
||||
return 0, .Invalid_Whence;
|
||||
case .Start:
|
||||
offset += s.base;
|
||||
case .Current:
|
||||
offset += s.off;
|
||||
case .End:
|
||||
offset += s.limit;
|
||||
}
|
||||
if offset < s.base {
|
||||
return 0, .Invalid_Offset;
|
||||
}
|
||||
s.off = offset;
|
||||
n = offset - s.base;
|
||||
return;
|
||||
},
|
||||
impl_size = proc(stream: Stream) -> i64 {
|
||||
s := (^Section_Reader)(stream.stream_data);
|
||||
return s.limit - s.base;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -74,6 +74,16 @@ _builder_stream_vtable := &io.Stream_VTable{
|
||||
}
|
||||
return nil;
|
||||
},
|
||||
impl_size = proc(s: io.Stream) -> i64 {
|
||||
b := (^Builder)(s.stream_data);
|
||||
return i64(len(b.buf));
|
||||
},
|
||||
impl_destroy = proc(s: io.Stream) -> io.Error {
|
||||
b := (^Builder)(s.stream_data);
|
||||
flush_builder(b);
|
||||
delete(b.buf);
|
||||
return .None;
|
||||
},
|
||||
};
|
||||
|
||||
to_stream :: proc(b: ^Builder) -> io.Stream {
|
||||
@@ -173,42 +183,23 @@ write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
|
||||
return;
|
||||
}
|
||||
|
||||
write_rune :: proc{
|
||||
write_rune_builder,
|
||||
write_rune_writer,
|
||||
};
|
||||
write_rune_builder :: proc(b: ^Builder, r: rune) -> int {
|
||||
return write_rune_writer(to_writer(b), r);
|
||||
}
|
||||
write_rune_writer :: proc(w: io.Writer, r: rune) -> int {
|
||||
if r < utf8.RUNE_SELF {
|
||||
return _write_byte(w, byte(r));
|
||||
}
|
||||
|
||||
s, n := utf8.encode_rune(r);
|
||||
n, _ = io.write(w, s[:n]);
|
||||
return n;
|
||||
write_rune_builder :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
|
||||
return io.write_rune(to_writer(b), r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
write_quoted_rune :: proc{
|
||||
write_quoted_rune_builder,
|
||||
write_quoted_rune_writer,
|
||||
};
|
||||
|
||||
write_quoted_rune_builder :: proc(b: ^Builder, r: rune) -> (n: int) {
|
||||
return write_quoted_rune_writer(to_writer(b), r);
|
||||
return write_quoted_rune(to_writer(b), r);
|
||||
}
|
||||
|
||||
@(private)
|
||||
_write_byte :: proc(w: io.Writer, r: byte) -> int {
|
||||
err := io.write_byte(w, r);
|
||||
_write_byte :: proc(w: io.Writer, c: byte) -> int {
|
||||
err := io.write_byte(w, c);
|
||||
return 1 if err == nil else 0;
|
||||
}
|
||||
|
||||
write_quoted_rune_writer :: proc(w: io.Writer, r: rune) -> (n: int) {
|
||||
|
||||
write_quoted_rune :: proc(w: io.Writer, r: rune) -> (n: int) {
|
||||
quote := byte('\'');
|
||||
n += _write_byte(w, quote);
|
||||
buf, width := utf8.encode_rune(r);
|
||||
@@ -328,7 +319,8 @@ write_encoded_rune_writer :: proc(w: io.Writer, r: rune, write_quote := true) ->
|
||||
case 2: n += write_string(w, s);
|
||||
}
|
||||
} else {
|
||||
n += write_rune(w, r);
|
||||
rn, _ := io.write_rune(w, r);
|
||||
n += rn;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package strings
|
||||
|
||||
import "core:io"
|
||||
import "core:unicode"
|
||||
import "core:unicode/utf8"
|
||||
|
||||
@@ -61,7 +62,7 @@ to_lower :: proc(s: string, allocator := context.allocator) -> string {
|
||||
b: Builder;
|
||||
init_builder(&b, 0, len(s), allocator);
|
||||
for r in s {
|
||||
write_rune(&b, unicode.to_lower(r));
|
||||
write_rune_builder(&b, unicode.to_lower(r));
|
||||
}
|
||||
return to_string(b);
|
||||
}
|
||||
@@ -69,7 +70,7 @@ to_upper :: proc(s: string, allocator := context.allocator) -> string {
|
||||
b: Builder;
|
||||
init_builder(&b, 0, len(s), allocator);
|
||||
for r in s {
|
||||
write_rune(&b, unicode.to_upper(r));
|
||||
write_rune_builder(&b, unicode.to_upper(r));
|
||||
}
|
||||
return to_string(b);
|
||||
}
|
||||
@@ -101,7 +102,7 @@ is_separator :: proc(r: rune) -> bool {
|
||||
}
|
||||
|
||||
|
||||
string_case_iterator :: proc(b: ^Builder, s: string, callback: proc(b: ^Builder, prev, curr, next: rune)) {
|
||||
string_case_iterator :: proc(w: io.Writer, s: string, callback: proc(w: io.Writer, prev, curr, next: rune)) {
|
||||
prev, curr: rune;
|
||||
for next in s {
|
||||
if curr == 0 {
|
||||
@@ -110,14 +111,14 @@ string_case_iterator :: proc(b: ^Builder, s: string, callback: proc(b: ^Builder,
|
||||
continue;
|
||||
}
|
||||
|
||||
callback(b, prev, curr, next);
|
||||
callback(w, prev, curr, next);
|
||||
|
||||
prev = curr;
|
||||
curr = next;
|
||||
}
|
||||
|
||||
if len(s) > 0 {
|
||||
callback(b, prev, curr, 0);
|
||||
callback(w, prev, curr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,15 +129,16 @@ to_camel_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||
s = trim_space(s);
|
||||
b: Builder;
|
||||
init_builder(&b, 0, len(s), allocator);
|
||||
w := to_writer(&b);
|
||||
|
||||
string_case_iterator(&b, s, proc(b: ^Builder, prev, curr, next: rune) {
|
||||
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
|
||||
if !is_delimiter(curr) {
|
||||
if is_delimiter(prev) {
|
||||
write_rune(b, unicode.to_upper(curr));
|
||||
io.write_rune(w, unicode.to_upper(curr));
|
||||
} else if unicode.is_lower(prev) {
|
||||
write_rune(b, curr);
|
||||
io.write_rune(w, curr);
|
||||
} else {
|
||||
write_rune(b, unicode.to_lower(curr));
|
||||
io.write_rune(w, unicode.to_lower(curr));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -150,15 +152,16 @@ to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||
s = trim_space(s);
|
||||
b: Builder;
|
||||
init_builder(&b, 0, len(s), allocator);
|
||||
w := to_writer(&b);
|
||||
|
||||
string_case_iterator(&b, s, proc(b: ^Builder, prev, curr, next: rune) {
|
||||
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
|
||||
if !is_delimiter(curr) {
|
||||
if is_delimiter(prev) || prev == 0 {
|
||||
write_rune(b, unicode.to_upper(curr));
|
||||
io.write_rune(w, unicode.to_upper(curr));
|
||||
} else if unicode.is_lower(prev) {
|
||||
write_rune(b, curr);
|
||||
io.write_rune(w, curr);
|
||||
} else {
|
||||
write_rune(b, unicode.to_lower(curr));
|
||||
io.write_rune(w, unicode.to_lower(curr));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -171,6 +174,7 @@ to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allo
|
||||
s = trim_space(s);
|
||||
b: Builder;
|
||||
init_builder(&b, 0, len(s), allocator);
|
||||
w := to_writer(&b);
|
||||
|
||||
adjust_case := unicode.to_upper if all_upper_case else unicode.to_lower;
|
||||
|
||||
@@ -179,15 +183,15 @@ to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allo
|
||||
for next in s {
|
||||
if is_delimiter(curr) {
|
||||
if !is_delimiter(prev) {
|
||||
write_rune(&b, delimiter);
|
||||
io.write_rune(w, delimiter);
|
||||
}
|
||||
} else if unicode.is_upper(curr) {
|
||||
if unicode.is_lower(prev) || (unicode.is_upper(prev) && unicode.is_lower(next)) {
|
||||
write_rune(&b, delimiter);
|
||||
io.write_rune(w, delimiter);
|
||||
}
|
||||
write_rune(&b, adjust_case(curr));
|
||||
io.write_rune(w, adjust_case(curr));
|
||||
} else if curr != 0 {
|
||||
write_rune(&b, adjust_case(curr));
|
||||
io.write_rune(w, adjust_case(curr));
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
@@ -196,9 +200,9 @@ to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allo
|
||||
|
||||
if len(s) > 0 {
|
||||
if unicode.is_upper(curr) && unicode.is_lower(prev) && prev != 0 {
|
||||
write_rune(&b, delimiter);
|
||||
io.write_rune(w, delimiter);
|
||||
}
|
||||
write_rune(&b, adjust_case(curr));
|
||||
io.write_rune(w, adjust_case(curr));
|
||||
}
|
||||
|
||||
return to_string(b);
|
||||
@@ -229,21 +233,22 @@ to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||
s = trim_space(s);
|
||||
b: Builder;
|
||||
init_builder(&b, 0, len(s), allocator);
|
||||
w := to_writer(&b);
|
||||
|
||||
prev, curr: rune;
|
||||
|
||||
for next in s {
|
||||
if is_delimiter(curr) {
|
||||
if !is_delimiter(prev) {
|
||||
write_rune(&b, delimiter);
|
||||
io.write_rune(w, delimiter);
|
||||
}
|
||||
} else if unicode.is_upper(curr) {
|
||||
if unicode.is_lower(prev) || (unicode.is_upper(prev) && unicode.is_lower(next)) {
|
||||
write_rune(&b, delimiter);
|
||||
io.write_rune(w, delimiter);
|
||||
}
|
||||
write_rune(&b, unicode.to_upper(curr));
|
||||
io.write_rune(w, unicode.to_upper(curr));
|
||||
} else if curr != 0 {
|
||||
write_rune(&b, unicode.to_lower(curr));
|
||||
io.write_rune(w, unicode.to_lower(curr));
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
@@ -252,10 +257,10 @@ to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||
|
||||
if len(s) > 0 {
|
||||
if unicode.is_upper(curr) && unicode.is_lower(prev) && prev != 0 {
|
||||
write_rune(&b, delimiter);
|
||||
write_rune(&b, unicode.to_upper(curr));
|
||||
io.write_rune(w, delimiter);
|
||||
io.write_rune(w, unicode.to_upper(curr));
|
||||
} else {
|
||||
write_rune(&b, unicode.to_lower(curr));
|
||||
io.write_rune(w, unicode.to_lower(curr));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package strings
|
||||
|
||||
import "core:io"
|
||||
import "core:mem"
|
||||
import "core:unicode/utf8"
|
||||
|
||||
@@ -814,6 +815,7 @@ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) ->
|
||||
|
||||
b: Builder;
|
||||
init_builder(&b, allocator);
|
||||
writer := to_writer(&b);
|
||||
str := s;
|
||||
column: int;
|
||||
|
||||
@@ -824,7 +826,7 @@ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) ->
|
||||
expand := tab_size - column%tab_size;
|
||||
|
||||
for i := 0; i < expand; i += 1 {
|
||||
write_byte(&b, ' ');
|
||||
io.write_byte(writer, ' ');
|
||||
}
|
||||
|
||||
column += expand;
|
||||
@@ -835,7 +837,7 @@ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) ->
|
||||
column += w;
|
||||
}
|
||||
|
||||
write_rune(&b, r);
|
||||
io.write_rune(writer, r);
|
||||
}
|
||||
|
||||
str = str[w:];
|
||||
@@ -874,9 +876,11 @@ centre_justify :: proc(str: string, length: int, pad: string, allocator := conte
|
||||
init_builder(&b, allocator);
|
||||
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
|
||||
|
||||
write_pad_string(&b, pad, pad_len, remains/2);
|
||||
write_string(&b, str);
|
||||
write_pad_string(&b, pad, pad_len, (remains+1)/2);
|
||||
w := to_writer(&b);
|
||||
|
||||
write_pad_string(w, pad, pad_len, remains/2);
|
||||
io.write_string(w, str);
|
||||
write_pad_string(w, pad, pad_len, (remains+1)/2);
|
||||
|
||||
return to_string(b);
|
||||
}
|
||||
@@ -895,8 +899,10 @@ left_justify :: proc(str: string, length: int, pad: string, allocator := context
|
||||
init_builder(&b, allocator);
|
||||
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
|
||||
|
||||
write_string(&b, str);
|
||||
write_pad_string(&b, pad, pad_len, remains);
|
||||
w := to_writer(&b);
|
||||
|
||||
io.write_string(w, str);
|
||||
write_pad_string(w, pad, pad_len, remains);
|
||||
|
||||
return to_string(b);
|
||||
}
|
||||
@@ -915,8 +921,10 @@ right_justify :: proc(str: string, length: int, pad: string, allocator := contex
|
||||
init_builder(&b, allocator);
|
||||
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
|
||||
|
||||
write_pad_string(&b, pad, pad_len, remains);
|
||||
write_string(&b, str);
|
||||
w := to_writer(&b);
|
||||
|
||||
write_pad_string(w, pad, pad_len, remains);
|
||||
io.write_string(w, str);
|
||||
|
||||
return to_string(b);
|
||||
}
|
||||
@@ -925,19 +933,19 @@ right_justify :: proc(str: string, length: int, pad: string, allocator := contex
|
||||
|
||||
|
||||
@private
|
||||
write_pad_string :: proc(b: ^Builder, pad: string, pad_len, remains: int) {
|
||||
write_pad_string :: proc(w: io.Writer, pad: string, pad_len, remains: int) {
|
||||
repeats := remains / pad_len;
|
||||
|
||||
for i := 0; i < repeats; i += 1 {
|
||||
write_string(b, pad);
|
||||
io.write_string(w, pad);
|
||||
}
|
||||
|
||||
n := remains % pad_len;
|
||||
p := pad;
|
||||
|
||||
for i := 0; i < n; i += 1 {
|
||||
r, w := utf8.decode_rune_in_string(p);
|
||||
write_rune(b, r);
|
||||
p = p[w:];
|
||||
r, width := utf8.decode_rune_in_string(p);
|
||||
io.write_rune(w, r);
|
||||
p = p[width:];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user