Remove unneeded semicolons from the core library

This commit is contained in:
gingerBill
2021-08-31 22:21:13 +01:00
parent b176af2742
commit 251da264ed
187 changed files with 27227 additions and 27227 deletions
+7 -7
View File
@@ -3,20 +3,20 @@ package strings
import "core:unicode/utf8"
Ascii_Set :: distinct [8]u32;
Ascii_Set :: distinct [8]u32
ascii_set_make :: proc(chars: string) -> (as: Ascii_Set, ok: bool) #no_bounds_check {
for i in 0..<len(chars) {
c := chars[i];
c := chars[i]
if c >= utf8.RUNE_SELF {
return;
return
}
as[c>>5] |= 1 << uint(c&31);
as[c>>5] |= 1 << uint(c&31)
}
ok = true;
return;
ok = true
return
}
ascii_set_contains :: proc(as: Ascii_Set, c: byte) -> bool #no_bounds_check {
return as[c>>5] & (1<<(c&31)) != 0;
return as[c>>5] & (1<<(c&31)) != 0
}
+155 -155
View File
@@ -5,199 +5,199 @@ import "core:unicode/utf8"
import "core:strconv"
import "core:io"
Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool);
Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool)
Builder :: struct {
buf: [dynamic]byte,
}
make_builder_none :: proc(allocator := context.allocator) -> Builder {
return Builder{buf=make([dynamic]byte, allocator)};
return Builder{buf=make([dynamic]byte, allocator)}
}
make_builder_len :: proc(len: int, allocator := context.allocator) -> Builder {
return Builder{buf=make([dynamic]byte, len, allocator)};
return Builder{buf=make([dynamic]byte, len, allocator)}
}
make_builder_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder {
return Builder{buf=make([dynamic]byte, len, cap, allocator)};
return Builder{buf=make([dynamic]byte, len, cap, allocator)}
}
make_builder :: proc{
make_builder_none,
make_builder_len,
make_builder_len_cap,
};
}
init_builder_none :: proc(b: ^Builder, allocator := context.allocator) {
b.buf = make([dynamic]byte, allocator);
b.buf = make([dynamic]byte, allocator)
}
init_builder_len :: proc(b: ^Builder, len: int, allocator := context.allocator) {
b.buf = make([dynamic]byte, len, allocator);
b.buf = make([dynamic]byte, len, allocator)
}
init_builder_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) {
b.buf = make([dynamic]byte, len, cap, allocator);
b.buf = make([dynamic]byte, len, cap, allocator)
}
init_builder :: proc{
init_builder_none,
init_builder_len,
init_builder_len_cap,
};
}
@(private)
_builder_stream_vtable := &io.Stream_VTable{
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
b := (^Builder)(s.stream_data);
n = write_bytes(b, p);
b := (^Builder)(s.stream_data)
n = write_bytes(b, p)
if len(b.buf) == cap(b.buf) {
err = .EOF;
err = .EOF
}
return;
return
},
impl_write_byte = proc(s: io.Stream, c: byte) -> io.Error {
b := (^Builder)(s.stream_data);
_ = write_byte(b, c);
b := (^Builder)(s.stream_data)
_ = write_byte(b, c)
if len(b.buf) == cap(b.buf) {
return .EOF;
return .EOF
}
return nil;
return nil
},
impl_size = proc(s: io.Stream) -> i64 {
b := (^Builder)(s.stream_data);
return i64(len(b.buf));
b := (^Builder)(s.stream_data)
return i64(len(b.buf))
},
impl_destroy = proc(s: io.Stream) -> io.Error {
b := (^Builder)(s.stream_data);
delete(b.buf);
return .None;
b := (^Builder)(s.stream_data)
delete(b.buf)
return .None
},
};
}
to_stream :: proc(b: ^Builder) -> io.Stream {
return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b};
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;
w, _ := io.to_writer(to_stream(b))
return w
}
destroy_builder :: proc(b: ^Builder) {
delete(b.buf);
clear(&b.buf);
delete(b.buf)
clear(&b.buf)
}
grow_builder :: proc(b: ^Builder, cap: int) {
reserve(&b.buf, cap);
reserve(&b.buf, cap)
}
reset_builder :: proc(b: ^Builder) {
clear(&b.buf);
clear(&b.buf)
}
builder_from_slice :: proc(backing: []byte) -> Builder {
s := transmute(mem.Raw_Slice)backing;
s := transmute(mem.Raw_Slice)backing
d := mem.Raw_Dynamic_Array{
data = s.data,
len = 0,
cap = s.len,
allocator = mem.nil_allocator(),
};
}
return Builder{
buf = transmute([dynamic]byte)d,
};
}
}
to_string :: proc(b: Builder) -> string {
return string(b.buf[:]);
return string(b.buf[:])
}
builder_len :: proc(b: Builder) -> int {
return len(b.buf);
return len(b.buf)
}
builder_cap :: proc(b: Builder) -> int {
return cap(b.buf);
return cap(b.buf)
}
builder_space :: proc(b: Builder) -> int {
return max(cap(b.buf), len(b.buf), 0);
return max(cap(b.buf), len(b.buf), 0)
}
write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
if builder_space(b^) > 0 {
append(&b.buf, x);
n += 1;
append(&b.buf, x)
n += 1
}
return;
return
}
write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
x := x;
x := x
for len(x) != 0 {
space := builder_space(b^);
space := builder_space(b^)
if space == 0 {
break; // No need to append
break // No need to append
}
i := min(space, len(x));
n += i;
append(&b.buf, ..x[:i]);
i := min(space, len(x))
n += i
append(&b.buf, ..x[:i])
if len(x) <= i {
break; // No more data to append
break // No more data to append
}
x = x[i:];
x = x[i:]
}
return;
return
}
write_rune_builder :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
return io.write_rune(to_writer(b), r);
return io.write_rune(to_writer(b), r)
}
write_quoted_rune_builder :: proc(b: ^Builder, r: rune) -> (n: int) {
return write_quoted_rune(to_writer(b), r);
return write_quoted_rune(to_writer(b), r)
}
@(private)
_write_byte :: proc(w: io.Writer, c: byte) -> int {
err := io.write_byte(w, c);
return 1 if err == nil else 0;
err := io.write_byte(w, c)
return 1 if err == nil else 0
}
write_quoted_rune :: proc(w: io.Writer, r: rune) -> (n: int) {
quote := byte('\'');
n += _write_byte(w, quote);
buf, width := utf8.encode_rune(r);
quote := byte('\'')
n += _write_byte(w, quote)
buf, width := utf8.encode_rune(r)
if width == 1 && r == utf8.RUNE_ERROR {
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]);
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(w, r, quote);
n += write_escaped_rune(w, r, quote)
}
n += _write_byte(w, quote);
return;
n += _write_byte(w, quote)
return
}
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);
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;
n, _ = io.write(w, transmute([]byte)s)
return
}
@@ -205,109 +205,109 @@ write_string_writer :: proc(w: io.Writer, s: string) -> (n: int) {
pop_byte :: proc(b: ^Builder) -> (r: byte) {
if len(b.buf) == 0 {
return 0;
return 0
}
r = b.buf[len(b.buf)-1];
d := cast(^mem.Raw_Dynamic_Array)&b.buf;
d.len = max(d.len-1, 0);
return;
r = b.buf[len(b.buf)-1]
d := cast(^mem.Raw_Dynamic_Array)&b.buf
d.len = max(d.len-1, 0)
return
}
pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
r, width = utf8.decode_last_rune(b.buf[:]);
d := cast(^mem.Raw_Dynamic_Array)&b.buf;
d.len = max(d.len-width, 0);
return;
r, width = utf8.decode_last_rune(b.buf[:])
d := cast(^mem.Raw_Dynamic_Array)&b.buf
d.len = max(d.len-width, 0)
return
}
@(private)
DIGITS_LOWER := "0123456789abcdefx";
DIGITS_LOWER := "0123456789abcdefx"
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);
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);
n += _write_byte(w, quote)
for width, s := 0, str; len(s) > 0; s = s[width:] {
r := rune(s[0]);
width = 1;
r := rune(s[0])
width = 1
if r >= utf8.RUNE_SELF {
r, width = utf8.decode_rune_in_string(s);
r, width = utf8.decode_rune_in_string(s)
}
if width == 1 && r == utf8.RUNE_ERROR {
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_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(w, r, quote);
n += write_escaped_rune(w, r, quote)
}
n += _write_byte(w, quote);
return;
n += _write_byte(w, quote)
return
}
write_encoded_rune :: proc{
write_encoded_rune_builder,
write_encoded_rune_writer,
};
}
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);
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(w, '\'');
n += _write_byte(w, '\'')
}
switch r {
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 '\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(w, `\x`);
buf: [2]byte;
s := strconv.append_bits(buf[:], u64(r), 16, true, 64, strconv.digits, nil);
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(w, "00");
case 1: n += _write_byte(w, '0');
case 2: n += write_string(w, s);
case 0: n += write_string(w, "00")
case 1: n += _write_byte(w, '0')
case 2: n += write_string(w, s)
}
} else {
rn, _ := io.write_rune(w, r);
n += rn;
rn, _ := io.write_rune(w, r)
n += rn
}
}
if write_quote {
n += _write_byte(w, '\'');
n += _write_byte(w, '\'')
}
return;
return
}
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);
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) {
@@ -315,89 +315,89 @@ write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe
if r <= 0xff {
switch r {
case 0x20..=0x7e:
return true;
return true
case 0xa1..=0xff: // ¡ through ÿ except for the soft hyphen
return r != 0xad; //
return r != 0xad //
}
}
// TODO(bill): A proper unicode library will be needed!
return false;
return false
}
if html_safe {
switch r {
case '<', '>', '&':
n += _write_byte(w, '\\');
n += _write_byte(w, 'u');
n += _write_byte(w, '\\')
n += _write_byte(w, 'u')
for s := 12; s >= 0; s -= 4 {
n += _write_byte(w, DIGITS_LOWER[r>>uint(s) & 0xf]);
n += _write_byte(w, DIGITS_LOWER[r>>uint(s) & 0xf])
}
return;
return
}
}
if r == rune(quote) || r == '\\' {
n += _write_byte(w, '\\');
n += _write_byte(w, byte(r));
return;
n += _write_byte(w, '\\')
n += _write_byte(w, byte(r))
return
} else if is_printable(r) {
n += write_encoded_rune(w, r, false);
return;
n += write_encoded_rune(w, r, false)
return
}
switch r {
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 '\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(w, '\\');
n += _write_byte(w, 'x');
n += _write_byte(w, DIGITS_LOWER[byte(c)>>4]);
n += _write_byte(w, 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;
c = 0xfffd
fallthrough
case c < 0x10000:
n += _write_byte(w, '\\');
n += _write_byte(w, 'u');
n += _write_byte(w, '\\')
n += _write_byte(w, 'u')
for s := 12; s >= 0; s -= 4 {
n += _write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf]);
n += _write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf])
}
case:
n += _write_byte(w, '\\');
n += _write_byte(w, 'U');
n += _write_byte(w, '\\')
n += _write_byte(w, 'U')
for s := 28; s >= 0; s -= 4 {
n += _write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf]);
n += _write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf])
}
}
}
return;
return
}
write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
buf: [32]byte;
s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil);
return write_string(b, s);
buf: [32]byte
s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
return write_string(b, s)
}
write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
buf: [32]byte;
s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil);
return write_string(b, s);
buf: [32]byte
s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
return write_string(b, s)
}
write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
return write_u64(b, u64(i), base);
return write_u64(b, u64(i), base)
}
write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
return write_i64(b, i64(i), base);
return write_i64(b, i64(i), base)
}
+109 -109
View File
@@ -6,91 +6,91 @@ import "core:unicode/utf8"
to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) -> string {
if len(s) == 0 {
return "";
return ""
}
b: Builder;
init_builder(&b, 0, 0, allocator);
b: Builder
init_builder(&b, 0, 0, allocator)
s := s;
s := s
for c, i in s {
if c != utf8.RUNE_ERROR {
continue;
continue
}
_, w := utf8.decode_rune_in_string(s[i:]);
_, w := utf8.decode_rune_in_string(s[i:])
if w == 1 {
grow_builder(&b, len(s) + len(replacement));
write_string(&b, s[:i]);
s = s[i:];
break;
grow_builder(&b, len(s) + len(replacement))
write_string(&b, s[:i])
s = s[i:]
break
}
}
if builder_cap(b) == 0 {
return clone(s, allocator);
return clone(s, allocator)
}
invalid := false;
invalid := false
for i := 0; i < len(s); /**/ {
c := s[i];
c := s[i]
if c < utf8.RUNE_SELF {
i += 1;
invalid = false;
write_byte(&b, c);
continue;
i += 1
invalid = false
write_byte(&b, c)
continue
}
_, w := utf8.decode_rune_in_string(s[i:]);
_, w := utf8.decode_rune_in_string(s[i:])
if w == 1 {
i += 1;
i += 1
if !invalid {
invalid = true;
write_string(&b, replacement);
invalid = true
write_string(&b, replacement)
}
continue;
continue
}
invalid = false;
write_string(&b, s[i:][:w]);
i += w;
invalid = false
write_string(&b, s[i:][:w])
i += w
}
return to_string(b);
return to_string(b)
}
to_lower :: proc(s: string, allocator := context.allocator) -> string {
b: Builder;
init_builder(&b, 0, len(s), allocator);
b: Builder
init_builder(&b, 0, len(s), allocator)
for r in s {
write_rune_builder(&b, unicode.to_lower(r));
write_rune_builder(&b, unicode.to_lower(r))
}
return to_string(b);
return to_string(b)
}
to_upper :: proc(s: string, allocator := context.allocator) -> string {
b: Builder;
init_builder(&b, 0, len(s), allocator);
b: Builder
init_builder(&b, 0, len(s), allocator)
for r in s {
write_rune_builder(&b, unicode.to_upper(r));
write_rune_builder(&b, unicode.to_upper(r))
}
return to_string(b);
return to_string(b)
}
is_delimiter :: proc(c: rune) -> bool {
return c == '-' || c == '_' || is_space(c);
return c == '-' || c == '_' || is_space(c)
}
is_separator :: proc(r: rune) -> bool {
if r <= 0x7f {
switch r {
case '0'..='9': return false;
case 'a'..='z': return false;
case 'A'..='Z': return false;
case '_': return false;
case '0'..='9': return false
case 'a'..='z': return false
case 'A'..='Z': return false
case '_': return false
}
return true;
return true
}
// TODO(bill): unicode categories
@@ -98,172 +98,172 @@ is_separator :: proc(r: rune) -> bool {
// return false;
// }
return unicode.is_space(r);
return unicode.is_space(r)
}
string_case_iterator :: proc(w: io.Writer, s: string, callback: proc(w: io.Writer, prev, curr, next: rune)) {
prev, curr: rune;
prev, curr: rune
for next in s {
if curr == 0 {
prev = curr;
curr = next;
continue;
prev = curr
curr = next
continue
}
callback(w, prev, curr, next);
callback(w, prev, curr, next)
prev = curr;
curr = next;
prev = curr
curr = next
}
if len(s) > 0 {
callback(w, prev, curr, 0);
callback(w, prev, curr, 0)
}
}
to_lower_camel_case :: to_camel_case;
to_lower_camel_case :: to_camel_case
to_camel_case :: proc(s: string, allocator := context.allocator) -> string {
s := s;
s = trim_space(s);
b: Builder;
init_builder(&b, 0, len(s), allocator);
w := to_writer(&b);
s := s
s = trim_space(s)
b: Builder
init_builder(&b, 0, len(s), allocator)
w := to_writer(&b)
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
if !is_delimiter(curr) {
if is_delimiter(prev) {
io.write_rune(w, unicode.to_upper(curr));
io.write_rune(w, unicode.to_upper(curr))
} else if unicode.is_lower(prev) {
io.write_rune(w, curr);
io.write_rune(w, curr)
} else {
io.write_rune(w, unicode.to_lower(curr));
io.write_rune(w, unicode.to_lower(curr))
}
}
});
})
return to_string(b);
return to_string(b)
}
to_upper_camel_case :: to_pascal_case;
to_upper_camel_case :: to_pascal_case
to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
s := s;
s = trim_space(s);
b: Builder;
init_builder(&b, 0, len(s), allocator);
w := to_writer(&b);
s := s
s = trim_space(s)
b: Builder
init_builder(&b, 0, len(s), allocator)
w := to_writer(&b)
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
if !is_delimiter(curr) {
if is_delimiter(prev) || prev == 0 {
io.write_rune(w, unicode.to_upper(curr));
io.write_rune(w, unicode.to_upper(curr))
} else if unicode.is_lower(prev) {
io.write_rune(w, curr);
io.write_rune(w, curr)
} else {
io.write_rune(w, unicode.to_lower(curr));
io.write_rune(w, unicode.to_lower(curr))
}
}
});
})
return to_string(b);
return to_string(b)
}
to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allocator := context.allocator) -> string {
s := s;
s = trim_space(s);
b: Builder;
init_builder(&b, 0, len(s), allocator);
w := to_writer(&b);
s := s
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;
adjust_case := unicode.to_upper if all_upper_case else unicode.to_lower
prev, curr: rune;
prev, curr: rune
for next in s {
if is_delimiter(curr) {
if !is_delimiter(prev) {
io.write_rune(w, 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)) {
io.write_rune(w, delimiter);
io.write_rune(w, delimiter)
}
io.write_rune(w, adjust_case(curr));
io.write_rune(w, adjust_case(curr))
} else if curr != 0 {
io.write_rune(w, adjust_case(curr));
io.write_rune(w, adjust_case(curr))
}
prev = curr;
curr = next;
prev = curr
curr = next
}
if len(s) > 0 {
if unicode.is_upper(curr) && unicode.is_lower(prev) && prev != 0 {
io.write_rune(w, delimiter);
io.write_rune(w, delimiter)
}
io.write_rune(w, adjust_case(curr));
io.write_rune(w, adjust_case(curr))
}
return to_string(b);
return to_string(b)
}
to_snake_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '_', false, allocator);
return to_delimiter_case(s, '_', false, allocator)
}
to_screaming_snake_case :: to_upper_snake_case;
to_screaming_snake_case :: to_upper_snake_case
to_upper_snake_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '_', true, allocator);
return to_delimiter_case(s, '_', true, allocator)
}
to_kebab_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '-', false, allocator);
return to_delimiter_case(s, '-', false, allocator)
}
to_upper_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '-', true, allocator);
return to_delimiter_case(s, '-', true, allocator)
}
to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
delimiter :: '_';
delimiter :: '_'
s := s;
s = trim_space(s);
b: Builder;
init_builder(&b, 0, len(s), allocator);
w := to_writer(&b);
s := s
s = trim_space(s)
b: Builder
init_builder(&b, 0, len(s), allocator)
w := to_writer(&b)
prev, curr: rune;
prev, curr: rune
for next in s {
if is_delimiter(curr) {
if !is_delimiter(prev) {
io.write_rune(w, 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)) {
io.write_rune(w, delimiter);
io.write_rune(w, delimiter)
}
io.write_rune(w, unicode.to_upper(curr));
io.write_rune(w, unicode.to_upper(curr))
} else if curr != 0 {
io.write_rune(w, unicode.to_lower(curr));
io.write_rune(w, unicode.to_lower(curr))
}
prev = curr;
curr = next;
prev = curr
curr = next
}
if len(s) > 0 {
if unicode.is_upper(curr) && unicode.is_lower(prev) && prev != 0 {
io.write_rune(w, delimiter);
io.write_rune(w, unicode.to_upper(curr));
io.write_rune(w, delimiter)
io.write_rune(w, unicode.to_upper(curr))
} else {
io.write_rune(w, unicode.to_lower(curr));
io.write_rune(w, unicode.to_lower(curr))
}
}
return to_string(b);
return to_string(b)
}
+18 -18
View File
@@ -13,42 +13,42 @@ Intern :: struct {
}
intern_init :: proc(m: ^Intern, allocator := context.allocator, map_allocator := context.allocator) {
m.allocator = allocator;
m.entries = make(map[string]^Intern_Entry, 16, map_allocator);
m.allocator = allocator
m.entries = make(map[string]^Intern_Entry, 16, map_allocator)
}
intern_destroy :: proc(m: ^Intern) {
for _, value in m.entries {
free(value, m.allocator);
free(value, m.allocator)
}
delete(m.entries);
delete(m.entries)
}
intern_get :: proc(m: ^Intern, text: string) -> string {
entry := _intern_get_entry(m, text);
#no_bounds_check return string(entry.str[:entry.len]);
entry := _intern_get_entry(m, text)
#no_bounds_check return string(entry.str[:entry.len])
}
intern_get_cstring :: proc(m: ^Intern, text: string) -> cstring {
entry := _intern_get_entry(m, text);
return cstring(&entry.str[0]);
entry := _intern_get_entry(m, text)
return cstring(&entry.str[0])
}
_intern_get_entry :: proc(m: ^Intern, text: string) -> ^Intern_Entry #no_bounds_check {
if prev, ok := m.entries[text]; ok {
return prev;
return prev
}
if m.allocator.procedure == nil {
m.allocator = context.allocator;
m.allocator = context.allocator
}
entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1;
new_entry := (^Intern_Entry)(mem.alloc(entry_size, align_of(Intern_Entry), m.allocator));
entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1
new_entry := (^Intern_Entry)(mem.alloc(entry_size, align_of(Intern_Entry), m.allocator))
new_entry.len = len(text);
copy(new_entry.str[:new_entry.len], text);
new_entry.str[new_entry.len] = 0;
new_entry.len = len(text)
copy(new_entry.str[:new_entry.len], text)
new_entry.str[new_entry.len] = 0
key := string(new_entry.str[:new_entry.len]);
m.entries[key] = new_entry;
return new_entry;
key := string(new_entry.str[:new_entry.len])
m.entries[key] = new_entry
return new_entry
}
+91 -91
View File
@@ -10,190 +10,190 @@ Reader :: struct {
}
reader_init :: proc(r: ^Reader, s: string) {
r.s = s;
r.i = 0;
r.prev_rune = -1;
r.s = s
r.i = 0
r.prev_rune = -1
}
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
s.stream_data = r;
s.stream_vtable = _reader_vtable;
return;
s.stream_data = r
s.stream_vtable = _reader_vtable
return
}
to_reader :: proc(r: ^Reader, s: string) -> io.Reader {
reader_init(r, s);
rr, _ := io.to_reader(reader_to_stream(r));
return rr;
reader_init(r, s)
rr, _ := io.to_reader(reader_to_stream(r))
return rr
}
to_reader_at :: proc(r: ^Reader, s: string) -> io.Reader_At {
reader_init(r, s);
rr, _ := io.to_reader_at(reader_to_stream(r));
return rr;
reader_init(r, s)
rr, _ := io.to_reader_at(reader_to_stream(r))
return rr
}
to_byte_reader :: proc(r: ^Reader, s: string) -> io.Byte_Reader {
reader_init(r, s);
rr, _ := io.to_byte_reader(reader_to_stream(r));
return rr;
reader_init(r, s)
rr, _ := io.to_byte_reader(reader_to_stream(r))
return rr
}
to_rune_reader :: proc(r: ^Reader, s: string) -> io.Rune_Reader {
reader_init(r, s);
rr, _ := io.to_rune_reader(reader_to_stream(r));
return rr;
reader_init(r, s)
rr, _ := io.to_rune_reader(reader_to_stream(r))
return rr
}
reader_length :: proc(r: ^Reader) -> int {
if r.i >= i64(len(r.s)) {
return 0;
return 0
}
return int(i64(len(r.s)) - r.i);
return int(i64(len(r.s)) - r.i)
}
reader_size :: proc(r: ^Reader) -> i64 {
return i64(len(r.s));
return i64(len(r.s))
}
reader_read :: proc(r: ^Reader, p: []byte) -> (n: int, err: io.Error) {
if r.i >= i64(len(r.s)) {
return 0, .EOF;
return 0, .EOF
}
r.prev_rune = -1;
n = copy(p, r.s[r.i:]);
r.i += i64(n);
return;
r.prev_rune = -1
n = copy(p, r.s[r.i:])
r.i += i64(n)
return
}
reader_read_at :: proc(r: ^Reader, p: []byte, off: i64) -> (n: int, err: io.Error) {
if off < 0 {
return 0, .Invalid_Offset;
return 0, .Invalid_Offset
}
if off >= i64(len(r.s)) {
return 0, .EOF;
return 0, .EOF
}
n = copy(p, r.s[off:]);
n = copy(p, r.s[off:])
if n < len(p) {
err = .EOF;
err = .EOF
}
return;
return
}
reader_read_byte :: proc(r: ^Reader) -> (byte, io.Error) {
r.prev_rune = -1;
r.prev_rune = -1
if r.i >= i64(len(r.s)) {
return 0, .EOF;
return 0, .EOF
}
b := r.s[r.i];
r.i += 1;
return b, nil;
b := r.s[r.i]
r.i += 1
return b, nil
}
reader_unread_byte :: proc(r: ^Reader) -> io.Error {
if r.i <= 0 {
return .Invalid_Unread;
return .Invalid_Unread
}
r.prev_rune = -1;
r.i -= 1;
return nil;
r.prev_rune = -1
r.i -= 1
return nil
}
reader_read_rune :: proc(r: ^Reader) -> (ch: rune, size: int, err: io.Error) {
if r.i >= i64(len(r.s)) {
r.prev_rune = -1;
return 0, 0, .EOF;
r.prev_rune = -1
return 0, 0, .EOF
}
r.prev_rune = int(r.i);
r.prev_rune = int(r.i)
if c := r.s[r.i]; c < utf8.RUNE_SELF {
r.i += 1;
return rune(c), 1, nil;
r.i += 1
return rune(c), 1, nil
}
ch, size = utf8.decode_rune_in_string(r.s[r.i:]);
r.i += i64(size);
return;
ch, size = utf8.decode_rune_in_string(r.s[r.i:])
r.i += i64(size)
return
}
reader_unread_rune :: proc(r: ^Reader) -> io.Error {
if r.i <= 0 {
return .Invalid_Unread;
return .Invalid_Unread
}
if r.prev_rune < 0 {
return .Invalid_Unread;
return .Invalid_Unread
}
r.i = i64(r.prev_rune);
r.prev_rune = -1;
return nil;
r.i = i64(r.prev_rune)
r.prev_rune = -1
return nil
}
reader_seek :: proc(r: ^Reader, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {
r.prev_rune = -1;
abs: i64;
r.prev_rune = -1
abs: i64
switch whence {
case .Start:
abs = offset;
abs = offset
case .Current:
abs = r.i + offset;
abs = r.i + offset
case .End:
abs = i64(len(r.s)) + offset;
abs = i64(len(r.s)) + offset
case:
return 0, .Invalid_Whence;
return 0, .Invalid_Whence
}
if abs < 0 {
return 0, .Invalid_Offset;
return 0, .Invalid_Offset
}
r.i = abs;
return abs, nil;
r.i = abs
return abs, nil
}
reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {
r.prev_rune = -1;
r.prev_rune = -1
if r.i >= i64(len(r.s)) {
return 0, nil;
return 0, nil
}
s := r.s[r.i:];
m: int;
m, err = io.write_string(w, s);
s := r.s[r.i:]
m: int
m, err = io.write_string(w, s)
if m > len(s) {
panic("bytes.Reader.write_to: invalid io.write_string count");
panic("bytes.Reader.write_to: invalid io.write_string count")
}
r.i += i64(m);
n = i64(m);
r.i += i64(m)
n = i64(m)
if m != len(s) && err == nil {
err = .Short_Write;
err = .Short_Write
}
return;
return
}
@(private)
_reader_vtable := &io.Stream_VTable{
impl_size = proc(s: io.Stream) -> i64 {
r := (^Reader)(s.stream_data);
return reader_size(r);
r := (^Reader)(s.stream_data)
return reader_size(r)
},
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
r := (^Reader)(s.stream_data);
return reader_read(r, p);
r := (^Reader)(s.stream_data)
return reader_read(r, p)
},
impl_read_at = proc(s: io.Stream, p: []byte, off: i64) -> (n: int, err: io.Error) {
r := (^Reader)(s.stream_data);
return reader_read_at(r, p, off);
r := (^Reader)(s.stream_data)
return reader_read_at(r, p, off)
},
impl_read_byte = proc(s: io.Stream) -> (byte, io.Error) {
r := (^Reader)(s.stream_data);
return reader_read_byte(r);
r := (^Reader)(s.stream_data)
return reader_read_byte(r)
},
impl_unread_byte = proc(s: io.Stream) -> io.Error {
r := (^Reader)(s.stream_data);
return reader_unread_byte(r);
r := (^Reader)(s.stream_data)
return reader_unread_byte(r)
},
impl_read_rune = proc(s: io.Stream) -> (ch: rune, size: int, err: io.Error) {
r := (^Reader)(s.stream_data);
return reader_read_rune(r);
r := (^Reader)(s.stream_data)
return reader_read_rune(r)
},
impl_unread_rune = proc(s: io.Stream) -> io.Error {
r := (^Reader)(s.stream_data);
return reader_unread_rune(r);
r := (^Reader)(s.stream_data)
return reader_unread_rune(r)
},
impl_seek = proc(s: io.Stream, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {
r := (^Reader)(s.stream_data);
return reader_seek(r, offset, whence);
r := (^Reader)(s.stream_data)
return reader_seek(r, offset, whence)
},
impl_write_to = proc(s: io.Stream, w: io.Writer) -> (n: i64, err: io.Error) {
r := (^Reader)(s.stream_data);
return reader_write_to(r, w);
r := (^Reader)(s.stream_data)
return reader_write_to(r, w)
},
};
}
+473 -473
View File
File diff suppressed because it is too large Load Diff