Improve packages io and strings; add io.Section_Reader

This commit is contained in:
gingerBill
2020-12-03 15:57:46 +00:00
parent 047586afc6
commit 0ef02e6737
5 changed files with 173 additions and 73 deletions
+18 -26
View File
@@ -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;
}
}
+31 -26
View File
@@ -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));
}
}
+22 -14
View File
@@ -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:];
}
}