Integrate package io into core library

This commit is contained in:
gingerBill
2020-12-03 10:45:26 +00:00
parent 334a8c46e8
commit 18da0b3418
9 changed files with 621 additions and 429 deletions

View File

@@ -14,7 +14,8 @@ Marshal_Error :: enum {
}
marshal :: proc(v: any, allocator := context.allocator) -> ([]byte, Marshal_Error) {
b := strings.make_builder(allocator);
b: strings.Builder;
strings.init_builder(&b, allocator);
err := marshal_arg(&b, v);
@@ -129,7 +130,7 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
case b32: val = bool(b);
case b64: val = bool(b);
}
write_string(b, val ? "true" : "false");
write_string_builder(b, val ? "true" : "false");
case Type_Info_Any:
return .Unsupported_Type;

File diff suppressed because it is too large Load Diff

View File

@@ -114,6 +114,21 @@ to_writer_to :: proc(s: Stream) -> (w: Writer_To, err: Conversion_Error) {
}
return;
}
to_Write_Closer :: proc(s: Stream) -> (w: Write_Closer, err: Conversion_Error) {
w.stream = s;
if s.stream_vtable == nil || s.impl_write == nil || s.impl_close == nil {
err = .Missing_Procedure;
}
return;
}
to_Write_Seeker :: proc(s: Stream) -> (w: Write_Seeker, err: Conversion_Error) {
w.stream = s;
if s.stream_vtable == nil || s.impl_write == nil || s.impl_seek == nil {
err = .Missing_Procedure;
}
return;
}
to_byte_reader :: proc(s: Stream) -> (b: Byte_Reader, err: Conversion_Error) {
b.stream = s;

View File

@@ -96,6 +96,9 @@ Read_Writer :: struct {using stream: Stream};
Read_Closer :: struct {using stream: Stream};
Read_Write_Closer :: struct {using stream: Stream};
Read_Write_Seeker :: struct {using stream: Stream};
Write_Closer :: struct {using stream: Stream};
Write_Seeker :: struct {using stream: Stream};
Write_Flusher :: struct {using stream: Stream};
Write_Flush_Closer :: struct {using stream: Stream};
@@ -117,7 +120,6 @@ destroy :: proc(s: Stream) -> Error {
if s.stream_vtable != nil && s.impl_destroy != nil {
return s->impl_destroy();
}
// Instead of .Empty, .None is fine in this case
return close_err;
}
@@ -205,9 +207,14 @@ read_at :: proc(r: Reader_At, p: []byte, offset: i64) -> (n: int, err: Error) {
if err != nil {
return 0, err;
}
defer r->impl_seek(current_offset, .Start);
return r->impl_read(p);
n, err = r->impl_read(p);
if err != nil {
return;
}
_, err = r->impl_seek(current_offset, .Start);
return;
}
write_at :: proc(w: Writer_At, p: []byte, offset: i64) -> (n: int, err: Error) {

View File

@@ -14,6 +14,13 @@ write_i64 :: proc(w: Writer, i: i64, base: int = 10) -> (n: int, err: Error) {
return write_string(w, s);
}
write_uint :: proc(w: Writer, i: uint, base: int = 10) -> (n: int, err: Error) {
return write_u64(w, u64(i), base);
}
write_int :: proc(w: Writer, i: int, base: int = 10) -> (n: int, err: Error) {
return write_i64(w, i64(i), base);
}
@(private)
Tee_Reader :: struct {
using stream: Stream,

View File

@@ -1,5 +1,6 @@
package reflect
import "core:io"
import "core:strings"
are_types_identical :: proc(a, b: ^Type_Info) -> bool {
@@ -360,258 +361,273 @@ is_relative_slice :: proc(info: ^Type_Info) -> bool {
write_typeid :: proc(buf: ^strings.Builder, id: typeid) {
write_typeid_builder :: proc(buf: ^strings.Builder, id: typeid) {
write_type(buf, type_info_of(id));
}
write_typeid_writer :: proc(writer: io.Writer, id: typeid) {
write_type(writer, type_info_of(id));
}
write_type :: proc(buf: ^strings.Builder, ti: ^Type_Info) {
write_typeid :: proc{
write_typeid_builder,
write_typeid_writer,
};
write_type :: proc{
write_type_builder,
write_type_writer,
};
write_type_builder :: proc(buf: ^strings.Builder, ti: ^Type_Info) {
write_type_writer(strings.to_writer(buf), ti);
}
write_type_writer :: proc(w: io.Writer, ti: ^Type_Info) {
using strings;
if ti == nil {
write_string(buf, "nil");
write_string(w, "nil");
return;
}
switch info in ti.variant {
case Type_Info_Named:
write_string(buf, info.name);
write_string(w, info.name);
case Type_Info_Integer:
switch ti.id {
case int: write_string(buf, "int");
case uint: write_string(buf, "uint");
case uintptr: write_string(buf, "uintptr");
case int: write_string(w, "int");
case uint: write_string(w, "uint");
case uintptr: write_string(w, "uintptr");
case:
write_byte(buf, 'i' if info.signed else 'u');
write_i64(buf, i64(8*ti.size), 10);
io.write_byte(w, 'i' if info.signed else 'u');
io.write_i64(w, i64(8*ti.size), 10);
switch info.endianness {
case .Platform: // Okay
case .Little: write_string(buf, "le");
case .Big: write_string(buf, "be");
case .Little: write_string(w, "le");
case .Big: write_string(w, "be");
}
}
case Type_Info_Rune:
write_string(buf, "rune");
io.write_string(w, "rune");
case Type_Info_Float:
write_byte(buf, 'f');
write_i64(buf, i64(8*ti.size), 10);
io.write_byte(w, 'f');
io.write_i64(w, i64(8*ti.size), 10);
switch info.endianness {
case .Platform: // Okay
case .Little: write_string(buf, "le");
case .Big: write_string(buf, "be");
case .Little: write_string(w, "le");
case .Big: write_string(w, "be");
}
case Type_Info_Complex:
write_string(buf, "complex");
write_i64(buf, i64(8*ti.size), 10);
io.write_string(w, "complex");
io.write_i64(w, i64(8*ti.size), 10);
case Type_Info_Quaternion:
write_string(buf, "quaternion");
write_i64(buf, i64(8*ti.size), 10);
io.write_string(w, "quaternion");
io.write_i64(w, i64(8*ti.size), 10);
case Type_Info_String:
if info.is_cstring {
write_string(buf, "cstring");
write_string(w, "cstring");
} else {
write_string(buf, "string");
write_string(w, "string");
}
case Type_Info_Boolean:
switch ti.id {
case bool: write_string(buf, "bool");
case bool: write_string(w, "bool");
case:
write_byte(buf, 'b');
write_i64(buf, i64(8*ti.size), 10);
io.write_byte(w, 'b');
io.write_i64(w, i64(8*ti.size), 10);
}
case Type_Info_Any:
write_string(buf, "any");
write_string(w, "any");
case Type_Info_Type_Id:
write_string(buf, "typeid");
write_string(w, "typeid");
case Type_Info_Pointer:
if info.elem == nil {
write_string(buf, "rawptr");
write_string(w, "rawptr");
} else {
write_string(buf, "^");
write_type(buf, info.elem);
write_string(w, "^");
write_type(w, info.elem);
}
case Type_Info_Procedure:
write_string(buf, "proc");
write_string(w, "proc");
if info.params == nil {
write_string(buf, "()");
write_string(w, "()");
} else {
t := info.params.variant.(Type_Info_Tuple);
write_string(buf, "(");
write_string(w, "(");
for t, i in t.types {
if i > 0 {
write_string(buf, ", ");
write_string(w, ", ");
}
write_type(buf, t);
write_type(w, t);
}
write_string(buf, ")");
write_string(w, ")");
}
if info.results != nil {
write_string(buf, " -> ");
write_type(buf, info.results);
write_string(w, " -> ");
write_type(w, info.results);
}
case Type_Info_Tuple:
count := len(info.names);
if count != 1 { write_string(buf, "("); }
if count != 1 { write_string(w, "("); }
for name, i in info.names {
if i > 0 { write_string(buf, ", "); }
if i > 0 { write_string(w, ", "); }
t := info.types[i];
if len(name) > 0 {
write_string(buf, name);
write_string(buf, ": ");
write_string(w, name);
write_string(w, ": ");
}
write_type(buf, t);
write_type(w, t);
}
if count != 1 { write_string(buf, ")"); }
if count != 1 { write_string(w, ")"); }
case Type_Info_Array:
write_string(buf, "[");
write_i64(buf, i64(info.count), 10);
write_string(buf, "]");
write_type(buf, info.elem);
io.write_string(w, "[");
io.write_i64(w, i64(info.count), 10);
io.write_string(w, "]");
write_type(w, info.elem);
case Type_Info_Enumerated_Array:
write_string(buf, "[");
write_type(buf, info.index);
write_string(buf, "]");
write_type(buf, info.elem);
write_string(w, "[");
write_type(w, info.index);
write_string(w, "]");
write_type(w, info.elem);
case Type_Info_Dynamic_Array:
write_string(buf, "[dynamic]");
write_type(buf, info.elem);
io.write_string(w, "[dynamic]");
write_type(w, info.elem);
case Type_Info_Slice:
write_string(buf, "[]");
write_type(buf, info.elem);
io.write_string(w, "[]");
write_type(w, info.elem);
case Type_Info_Map:
write_string(buf, "map[");
write_type(buf, info.key);
write_byte(buf, ']');
write_type(buf, info.value);
io.write_string(w, "map[");
write_type(w, info.key);
io.write_byte(w, ']');
write_type(w, info.value);
case Type_Info_Struct:
switch info.soa_kind {
case .None: // Ignore
case .Fixed:
write_string(buf, "#soa[");
write_i64(buf, i64(info.soa_len));
write_byte(buf, ']');
write_type(buf, info.soa_base_type);
io.write_string(w, "#soa[");
io.write_i64(w, i64(info.soa_len));
io.write_byte(w, ']');
write_type(w, info.soa_base_type);
return;
case .Slice:
write_string(buf, "#soa[]");
write_type(buf, info.soa_base_type);
io.write_string(w, "#soa[]");
write_type(w, info.soa_base_type);
return;
case .Dynamic:
write_string(buf, "#soa[dynamic]");
write_type(buf, info.soa_base_type);
io.write_string(w, "#soa[dynamic]");
write_type(w, info.soa_base_type);
return;
}
write_string(buf, "struct ");
if info.is_packed { write_string(buf, "#packed "); }
if info.is_raw_union { write_string(buf, "#raw_union "); }
write_string(w, "struct ");
if info.is_packed { write_string(w, "#packed "); }
if info.is_raw_union { write_string(w, "#raw_union "); }
if info.custom_align {
write_string(buf, "#align ");
write_i64(buf, i64(ti.align), 10);
write_byte(buf, ' ');
io.write_string(w, "#align ");
io.write_i64(w, i64(ti.align), 10);
io.write_byte(w, ' ');
}
write_byte(buf, '{');
io.write_byte(w, '{');
for name, i in info.names {
if i > 0 { write_string(buf, ", "); }
write_string(buf, name);
write_string(buf, ": ");
write_type(buf, info.types[i]);
if i > 0 { write_string(w, ", "); }
io.write_string(w, name);
io.write_string(w, ": ");
write_type(w, info.types[i]);
}
write_byte(buf, '}');
io.write_byte(w, '}');
case Type_Info_Union:
write_string(buf, "union ");
write_string(w, "union ");
if info.custom_align {
write_string(buf, "#align ");
write_i64(buf, i64(ti.align), 10);
write_byte(buf, ' ');
write_string(w, "#align ");
io.write_i64(w, i64(ti.align), 10);
io.write_byte(w, ' ');
}
write_byte(buf, '{');
io.write_byte(w, '{');
for variant, i in info.variants {
if i > 0 { write_string(buf, ", "); }
write_type(buf, variant);
if i > 0 { write_string(w, ", "); }
write_type(w, variant);
}
write_byte(buf, '}');
io.write_byte(w, '}');
case Type_Info_Enum:
write_string(buf, "enum ");
write_type(buf, info.base);
write_string(buf, " {");
write_string(w, "enum ");
write_type(w, info.base);
write_string(w, " {");
for name, i in info.names {
if i > 0 { write_string(buf, ", "); }
write_string(buf, name);
if i > 0 { write_string(w, ", "); }
write_string(w, name);
}
write_byte(buf, '}');
io.write_byte(w, '}');
case Type_Info_Bit_Field:
write_string(buf, "bit_field ");
write_string(w, "bit_field ");
if ti.align != 1 {
write_string(buf, "#align ");
write_i64(buf, i64(ti.align), 10);
write_byte(buf, ' ');
write_string(w, "#align ");
io.write_i64(w, i64(ti.align), 10);
io.write_byte(w, ' ');
}
write_string(buf, " {");
write_string(w, " {");
for name, i in info.names {
if i > 0 { write_string(buf, ", "); }
write_string(buf, name);
write_string(buf, ": ");
write_i64(buf, i64(info.bits[i]), 10);
if i > 0 { write_string(w, ", "); }
write_string(w, name);
write_string(w, ": ");
io.write_i64(w, i64(info.bits[i]), 10);
}
write_byte(buf, '}');
io.write_byte(w, '}');
case Type_Info_Bit_Set:
write_string(buf, "bit_set[");
write_string(w, "bit_set[");
switch {
case is_enum(info.elem):
write_type(buf, info.elem);
write_type(w, info.elem);
case is_rune(info.elem):
write_encoded_rune(buf, rune(info.lower));
write_string(buf, "..");
write_encoded_rune(buf, rune(info.upper));
write_encoded_rune(w, rune(info.lower));
write_string(w, "..");
write_encoded_rune(w, rune(info.upper));
case:
write_i64(buf, info.lower, 10);
write_string(buf, "..");
write_i64(buf, info.upper, 10);
io.write_i64(w, info.lower, 10);
write_string(w, "..");
io.write_i64(w, info.upper, 10);
}
if info.underlying != nil {
write_string(buf, "; ");
write_type(buf, info.underlying);
write_string(w, "; ");
write_type(w, info.underlying);
}
write_byte(buf, ']');
io.write_byte(w, ']');
case Type_Info_Opaque:
write_string(buf, "opaque ");
write_type(buf, info.elem);
write_string(w, "opaque ");
write_type(w, info.elem);
case Type_Info_Simd_Vector:
if info.is_x86_mmx {
write_string(buf, "intrinsics.x86_mmx");
write_string(w, "intrinsics.x86_mmx");
} else {
write_string(buf, "#simd[");
write_i64(buf, i64(info.count));
write_byte(buf, ']');
write_type(buf, info.elem);
write_string(w, "#simd[");
io.write_i64(w, i64(info.count));
io.write_byte(w, ']');
write_type(w, info.elem);
}
case Type_Info_Relative_Pointer:
write_string(buf, "#relative(");
write_type(buf, info.base_integer);
write_string(buf, ") ");
write_type(buf, info.pointer);
write_string(w, "#relative(");
write_type(w, info.base_integer);
write_string(w, ") ");
write_type(w, info.pointer);
case Type_Info_Relative_Slice:
write_string(buf, "#relative(");
write_type(buf, info.base_integer);
write_string(buf, ") ");
write_type(buf, info.slice);
write_string(w, "#relative(");
write_type(w, info.base_integer);
write_string(w, ") ");
write_type(w, info.slice);
}
}

View File

@@ -3,6 +3,7 @@ package strings
import "core:mem"
import "core:unicode/utf8"
import "core:strconv"
import "core:io"
Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool);
@@ -50,6 +51,39 @@ init_builder :: proc{
init_builder_len_cap,
};
@(private)
_builder_stream_vtable := &io.Stream_VTable{
impl_flush = proc(s: io.Stream) -> io.Error {
b := (^Builder)(s.stream_data);
flush_builder(b);
return nil;
},
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
b := (^Builder)(s.stream_data);
n = write_bytes(b, p);
if len(b.buf) == cap(b.buf) {
err = .EOF;
}
return;
},
impl_write_byte = proc(s: io.Stream, c: byte) -> io.Error {
b := (^Builder)(s.stream_data);
_ = write_byte(b, c);
if len(b.buf) == cap(b.buf) {
return .EOF;
}
return nil;
},
};
to_stream :: proc(b: ^Builder) -> io.Stream {
return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b};
}
to_writer :: proc(b: ^Builder) -> io.Writer {
w, _ := io.to_writer(to_stream(b));
return w;
}
@@ -139,36 +173,75 @@ write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
return;
}
write_rune :: proc(b: ^Builder, r: rune) -> int {
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(b, byte(r));
return _write_byte(w, byte(r));
}
s, n := utf8.encode_rune(r);
write_bytes(b, s[:n]);
n, _ = io.write(w, s[:n]);
return n;
}
write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
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);
}
@(private)
_write_byte :: proc(w: io.Writer, r: byte) -> int {
err := io.write_byte(w, r);
return 1 if err == nil else 0;
}
write_quoted_rune_writer :: proc(w: io.Writer, r: rune) -> (n: int) {
quote := byte('\'');
n += write_byte(b, quote);
n += _write_byte(w, quote);
buf, width := utf8.encode_rune(r);
if width == 1 && r == utf8.RUNE_ERROR {
n += write_byte(b, '\\');
n += write_byte(b, 'x');
n += write_byte(b, DIGITS_LOWER[buf[0]>>4]);
n += write_byte(b, DIGITS_LOWER[buf[0]&0xf]);
n += _write_byte(w, '\\');
n += _write_byte(w, 'x');
n += _write_byte(w, DIGITS_LOWER[buf[0]>>4]);
n += _write_byte(w, DIGITS_LOWER[buf[0]&0xf]);
} else {
n += write_escaped_rune(b, r, quote);
n += write_escaped_rune(w, r, quote);
}
n += write_byte(b, quote);
n += _write_byte(w, quote);
return;
}
write_string :: proc(b: ^Builder, s: string) -> (n: int) {
return write_bytes(b, transmute([]byte)s);
write_string :: proc{
write_string_builder,
write_string_writer,
};
write_string_builder :: proc(b: ^Builder, s: string) -> (n: int) {
return write_string_writer(to_writer(b), s);
}
write_string_writer :: proc(w: io.Writer, s: string) -> (n: int) {
n, _ = io.write(w, transmute([]byte)s);
return;
}
pop_byte :: proc(b: ^Builder) -> (r: byte) {
if len(b.buf) == 0 {
return 0;
@@ -190,8 +263,17 @@ pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
@(private, static)
DIGITS_LOWER := "0123456789abcdefx";
write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
n += write_byte(b, quote);
write_quoted_string :: proc{
write_quoted_string_builder,
write_quoted_string_writer,
};
write_quoted_string_builder :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
return write_quoted_string_writer(to_writer(b), str, quote);
}
write_quoted_string_writer :: proc(w: io.Writer, str: string, quote: byte = '"') -> (n: int) {
n += _write_byte(w, quote);
for width, s := 0, str; len(s) > 0; s = s[width:] {
r := rune(s[0]);
width = 1;
@@ -199,57 +281,74 @@ write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n:
r, width = utf8.decode_rune_in_string(s);
}
if width == 1 && r == utf8.RUNE_ERROR {
n += write_byte(b, '\\');
n += write_byte(b, 'x');
n += write_byte(b, DIGITS_LOWER[s[0]>>4]);
n += write_byte(b, DIGITS_LOWER[s[0]&0xf]);
n += _write_byte(w, '\\');
n += _write_byte(w, 'x');
n += _write_byte(w, DIGITS_LOWER[s[0]>>4]);
n += _write_byte(w, DIGITS_LOWER[s[0]&0xf]);
continue;
}
n += write_escaped_rune(b, r, quote);
n += write_escaped_rune(w, r, quote);
}
n += write_byte(b, quote);
n += _write_byte(w, quote);
return;
}
write_encoded_rune :: proc{
write_encoded_rune_builder,
write_encoded_rune_writer,
};
write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
write_encoded_rune_builder :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
return write_encoded_rune_writer(to_writer(b), r, write_quote);
}
write_encoded_rune_writer :: proc(w: io.Writer, r: rune, write_quote := true) -> (n: int) {
if write_quote {
n += write_byte(b, '\'');
n += _write_byte(w, '\'');
}
switch r {
case '\a': n += write_string(b, `\a"`);
case '\b': n += write_string(b, `\b"`);
case '\e': n += write_string(b, `\e"`);
case '\f': n += write_string(b, `\f"`);
case '\n': n += write_string(b, `\n"`);
case '\r': n += write_string(b, `\r"`);
case '\t': n += write_string(b, `\t"`);
case '\v': n += write_string(b, `\v"`);
case '\a': n += write_string(w, `\a"`);
case '\b': n += write_string(w, `\b"`);
case '\e': n += write_string(w, `\e"`);
case '\f': n += write_string(w, `\f"`);
case '\n': n += write_string(w, `\n"`);
case '\r': n += write_string(w, `\r"`);
case '\t': n += write_string(w, `\t"`);
case '\v': n += write_string(w, `\v"`);
case:
if r < 32 {
n += write_string(b, `\x`);
n += write_string(w, `\x`);
buf: [2]byte;
s := strconv.append_bits(buf[:], u64(r), 16, true, 64, strconv.digits, nil);
switch len(s) {
case 0: n += write_string(b, "00");
case 1: n += write_byte(b, '0');
case 2: n += write_string(b, s);
case 0: n += write_string(w, "00");
case 1: n += _write_byte(w, '0');
case 2: n += write_string(w, s);
}
} else {
n += write_rune(b, r);
n += write_rune(w, r);
}
}
if write_quote {
n += write_byte(b, '\'');
n += _write_byte(w, '\'');
}
return;
}
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
write_escaped_rune :: proc{
write_escaped_rune_builder,
write_escaped_rune_writer,
};
write_escaped_rune_builder :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
return write_escaped_rune_writer(to_writer(b), r, quote, html_safe);
}
write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe := false) -> (n: int) {
is_printable :: proc(r: rune) -> bool {
if r <= 0xff {
switch r {
@@ -267,54 +366,54 @@ write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false
if html_safe {
switch r {
case '<', '>', '&':
n += write_byte(b, '\\');
n += write_byte(b, 'u');
n += _write_byte(w, '\\');
n += _write_byte(w, 'u');
for s := 12; s >= 0; s -= 4 {
n += write_byte(b, DIGITS_LOWER[r>>uint(s) & 0xf]);
n += _write_byte(w, DIGITS_LOWER[r>>uint(s) & 0xf]);
}
return;
}
}
if r == rune(quote) || r == '\\' {
n += write_byte(b, '\\');
n += write_byte(b, byte(r));
n += _write_byte(w, '\\');
n += _write_byte(w, byte(r));
return;
} else if is_printable(r) {
n += write_encoded_rune(b, r, false);
n += write_encoded_rune(w, r, false);
return;
}
switch r {
case '\a': n += write_string(b, `\a`);
case '\b': n += write_string(b, `\b`);
case '\e': n += write_string(b, `\e`);
case '\f': n += write_string(b, `\f`);
case '\n': n += write_string(b, `\n`);
case '\r': n += write_string(b, `\r`);
case '\t': n += write_string(b, `\t`);
case '\v': n += write_string(b, `\v`);
case '\a': n += write_string(w, `\a`);
case '\b': n += write_string(w, `\b`);
case '\e': n += write_string(w, `\e`);
case '\f': n += write_string(w, `\f`);
case '\n': n += write_string(w, `\n`);
case '\r': n += write_string(w, `\r`);
case '\t': n += write_string(w, `\t`);
case '\v': n += write_string(w, `\v`);
case:
switch c := r; {
case c < ' ':
n += write_byte(b, '\\');
n += write_byte(b, 'x');
n += write_byte(b, DIGITS_LOWER[byte(c)>>4]);
n += write_byte(b, DIGITS_LOWER[byte(c)&0xf]);
n += _write_byte(w, '\\');
n += _write_byte(w, 'x');
n += _write_byte(w, DIGITS_LOWER[byte(c)>>4]);
n += _write_byte(w, DIGITS_LOWER[byte(c)&0xf]);
case c > utf8.MAX_RUNE:
c = 0xfffd;
fallthrough;
case c < 0x10000:
n += write_byte(b, '\\');
n += write_byte(b, 'u');
n += _write_byte(w, '\\');
n += _write_byte(w, 'u');
for s := 12; s >= 0; s -= 4 {
n += write_byte(b, DIGITS_LOWER[c>>uint(s) & 0xf]);
n += _write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf]);
}
case:
n += write_byte(b, '\\');
n += write_byte(b, 'U');
n += _write_byte(w, '\\');
n += _write_byte(w, 'U');
for s := 28; s >= 0; s -= 4 {
n += write_byte(b, DIGITS_LOWER[c>>uint(s) & 0xf]);
n += _write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf]);
}
}
}

View File

@@ -8,7 +8,8 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) ->
return "";
}
b := make_builder_len_cap(0, 0, allocator);
b: Builder;
init_builder(&b, 0, 0, allocator);
s := s;
for c, i in s {
@@ -57,14 +58,16 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) ->
}
to_lower :: proc(s: string, allocator := context.allocator) -> string {
b := make_builder(0, len(s), allocator);
b: Builder;
init_builder(&b, 0, len(s), allocator);
for r in s {
write_rune(&b, unicode.to_lower(r));
}
return to_string(b);
}
to_upper :: proc(s: string, allocator := context.allocator) -> string {
b := make_builder(0, len(s), allocator);
b: Builder;
init_builder(&b, 0, len(s), allocator);
for r in s {
write_rune(&b, unicode.to_upper(r));
}
@@ -123,7 +126,8 @@ to_lower_camel_case :: to_camel_case;
to_camel_case :: proc(s: string, allocator := context.allocator) -> string {
s := s;
s = trim_space(s);
b := make_builder(0, len(s), allocator);
b: Builder;
init_builder(&b, 0, len(s), allocator);
string_case_iterator(&b, s, proc(b: ^Builder, prev, curr, next: rune) {
if !is_delimiter(curr) {
@@ -144,7 +148,8 @@ to_upper_camel_case :: to_pascal_case;
to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
s := s;
s = trim_space(s);
b := make_builder(0, len(s), allocator);
b: Builder;
init_builder(&b, 0, len(s), allocator);
string_case_iterator(&b, s, proc(b: ^Builder, prev, curr, next: rune) {
if !is_delimiter(curr) {
@@ -164,7 +169,8 @@ to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allocator := context.allocator) -> string {
s := s;
s = trim_space(s);
b := make_builder(0, len(s), allocator);
b: Builder;
init_builder(&b, 0, len(s), allocator);
adjust_case := unicode.to_upper if all_upper_case else unicode.to_lower;
@@ -221,7 +227,8 @@ to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
s := s;
s = trim_space(s);
b := make_builder(0, len(s), allocator);
b: Builder;
init_builder(&b, 0, len(s), allocator);
prev, curr: rune;

View File

@@ -756,7 +756,8 @@ split_multi :: proc(s: string, substrs: []string, skip_empty := false, allocator
// Adjacent invalid bytes are only replaced once
scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string {
str := s;
b := make_builder(0, len(str), allocator);
b: Builder;
init_builder(&b, 0, len(s), allocator);
has_error := false;
cursor := 0;
@@ -811,7 +812,8 @@ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) ->
return "";
}
b := make_builder(allocator);
b: Builder;
init_builder(&b, allocator);
str := s;
column: int;
@@ -868,7 +870,8 @@ centre_justify :: proc(str: string, length: int, pad: string, allocator := conte
remains := length-1;
pad_len := rune_count(pad);
b := make_builder(allocator);
b: Builder;
init_builder(&b, allocator);
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
write_pad_string(&b, pad, pad_len, remains/2);
@@ -888,7 +891,8 @@ left_justify :: proc(str: string, length: int, pad: string, allocator := context
remains := length-1;
pad_len := rune_count(pad);
b := make_builder(allocator);
b: Builder;
init_builder(&b, allocator);
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
write_string(&b, str);
@@ -907,7 +911,8 @@ right_justify :: proc(str: string, length: int, pad: string, allocator := contex
remains := length-1;
pad_len := rune_count(pad);
b := make_builder(allocator);
b: Builder;
init_builder(&b, allocator);
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
write_pad_string(&b, pad, pad_len, remains);