Core library clean up: Make range expressions more consistent and replace uses of .. with ..=

This commit is contained in:
gingerBill
2021-06-14 11:15:25 +01:00
parent 3ca887a60a
commit 86649e6b44
23 changed files with 80 additions and 84 deletions

View File

@@ -91,7 +91,7 @@ equal_fold :: proc(u, v: []byte) -> bool {
if tr < utf8.RUNE_SELF {
switch sr {
case 'A'..'Z':
case 'A'..='Z':
if tr == (sr+'a')-'A' {
continue loop;
}

View File

@@ -139,18 +139,18 @@ append_token :: proc(a, b: ^Token) -> ^Token {
is_hex_digit :: proc(x: byte) -> bool {
switch x {
case '0'..'9', 'a'..'f', 'A'..'F':
case '0'..='9', 'a'..='f', 'A'..='F':
return true;
}
return false;
}
from_hex :: proc(x: byte) -> i32 {
switch x {
case '0'..'9':
case '0'..='9':
return i32(x) - '0';
case 'a'..'f':
case 'a'..='f':
return i32(x) - 'a' + 10;
case 'A'..'F':
case 'A'..='F':
return i32(x) - 'A' + 10;
}
return 16;

View File

@@ -5,9 +5,9 @@ import "core:unicode/utf8"
unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
hex_to_int :: proc(c: byte) -> int {
switch c {
case '0'..'9': return int(c-'0');
case 'a'..'f': return int(c-'a')+10;
case 'A'..'F': return int(c-'A')+10;
case '0'..='9': return int(c-'0');
case 'a'..='f': return int(c-'a')+10;
case 'A'..='F': return int(c-'A')+10;
}
return -1;
}
@@ -45,7 +45,7 @@ unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool
case '"': r = '"';
case '\'': r = '\'';
case '0'..'7':
case '0'..='7':
v := int(c-'0');
if len(s) < 2 {
return;

View File

@@ -224,11 +224,11 @@ scan_string :: proc(t: ^Tokenizer) -> string {
digit_val :: proc(r: rune) -> int {
switch r {
case '0'..'9':
case '0'..='9':
return int(r-'0');
case 'A'..'F':
case 'A'..='F':
return int(r-'A' + 10);
case 'a'..'f':
case 'a'..='f':
return int(r-'a' + 10);
}
return 16;
@@ -245,7 +245,7 @@ scan_escape :: proc(t: ^Tokenizer) -> bool {
advance_rune(t);
return true;
case '0'..'7':
case '0'..='7':
for digit_val(t.ch) < 8 {
advance_rune(t);
}

View File

@@ -288,11 +288,11 @@ load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := con
payload_crc_b: [4]u8;
payload_len_b: [4]u8;
for i in 0..3 {
for _, i in payload_crc_b {
payload_crc_b[i] = u8(compress.read_bits_lsb(&ctx, 8));
}
payload_crc := transmute(u32le)payload_crc_b;
for i in 0..3 {
for _, i in payload_len_b {
payload_len_b[i] = u8(compress.read_bits_lsb(&ctx, 8));
}
payload_len := int(transmute(u32le)payload_len_b);

View File

@@ -133,9 +133,7 @@ write_byte :: #force_inline proc(z: ^Context, c: u8) -> (err: io.Error) #no_boun
}
allocate_huffman_table :: proc(allocator := context.allocator) -> (z: ^Huffman_Table, err: Error) {
z = new(Huffman_Table, allocator);
return z, nil;
return new(Huffman_Table, allocator), nil;
}
build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
@@ -152,14 +150,14 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
}
sizes[0] = 0;
for i in 1..16 {
for i in 1..<(HUFFMAN_MAX_BITS+1) {
if sizes[i] > (1 << uint(i)) {
return E_Deflate.Huffman_Bad_Sizes;
}
}
code := int(0);
for i in 1..<16 {
for i in 1..<HUFFMAN_MAX_BITS {
next_code[i] = code;
z.firstcode[i] = u16(code);
z.firstsymbol[i] = u16(k);
@@ -169,12 +167,12 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
return E_Deflate.Huffman_Bad_Code_Lengths;
}
}
z.maxcode[i] = code << (16 - uint(i));
z.maxcode[i] = code << (HUFFMAN_MAX_BITS - uint(i));
code <<= 1;
k += int(sizes[i]);
}
z.maxcode[16] = 0x10000; // Sentinel
z.maxcode[HUFFMAN_MAX_BITS] = 0x10000; // Sentinel
c: int;
for v, ci in code_lengths {
@@ -197,7 +195,6 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
}
decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
r = 0;
err = nil;
@@ -233,7 +230,6 @@ decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err:
}
decode_huffman :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
if z.num_bits < 16 {
if z.num_bits == -100 {
return 0, E_ZLIB.Code_Buffer_Malformed;

View File

@@ -201,9 +201,9 @@ next_token :: proc(p: ^Parser) -> Token {
unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
hex_to_int :: proc(c: byte) -> int {
switch c {
case '0'..'9': return int(c-'0');
case 'a'..'f': return int(c-'a')+10;
case 'A'..'F': return int(c-'A')+10;
case '0'..='9': return int(c-'0');
case 'a'..='f': return int(c-'a')+10;
case 'A'..='F': return int(c-'A')+10;
}
return -1;
}
@@ -241,7 +241,7 @@ unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool
case '"': r = '"';
case '\'': r = '\'';
case '0'..'7':
case '0'..='7':
v := int(c-'0');
if len(s) < 2 {
return;

View File

@@ -232,7 +232,7 @@ get_pos :: proc(t: ^Tokenizer) -> Pos {
is_letter :: proc(r: rune) -> bool {
switch r {
case 'a'..'z', 'A'..'Z', '_':
case 'a'..='z', 'A'..='Z', '_':
return true;
}
return false;
@@ -240,7 +240,7 @@ is_letter :: proc(r: rune) -> bool {
is_digit :: proc(r: rune) -> bool {
switch r {
case '0'..'9':
case '0'..='9':
return true;
}
return false;
@@ -273,9 +273,9 @@ scan_identifier :: proc(t: ^Tokenizer) -> string {
digit_value :: proc(r: rune) -> int {
switch r {
case '0'..'9': return int(r - '0');
case 'a'..'f': return int(r - 'a' + 10);
case 'A'..'F': return int(r - 'A' + 10);
case '0'..='9': return int(r - '0');
case 'a'..='f': return int(r - 'a' + 10);
case 'A'..='F': return int(r - 'A' + 10);
}
return 16;
}

View File

@@ -290,9 +290,9 @@ unquote_string :: proc(token: Token, spec: Specification, allocator := context.a
for c in s[2:4] {
x: rune;
switch c {
case '0'..'9': x = c - '0';
case 'a'..'f': x = c - 'a' + 10;
case 'A'..'F': x = c - 'A' + 10;
case '0'..='9': x = c - '0';
case 'a'..='f': x = c - 'a' + 10;
case 'A'..='F': x = c - 'A' + 10;
case: return -1;
}
r = r*16 + x;
@@ -308,9 +308,9 @@ unquote_string :: proc(token: Token, spec: Specification, allocator := context.a
for c in s[2:6] {
x: rune;
switch c {
case '0'..'9': x = c - '0';
case 'a'..'f': x = c - 'a' + 10;
case 'A'..'F': x = c - 'A' + 10;
case '0'..='9': x = c - '0';
case 'a'..='f': x = c - 'a' + 10;
case 'A'..='F': x = c - 'A' + 10;
case: return -1;
}
r = r*16 + x;

View File

@@ -82,7 +82,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
for t.offset < len(t.data) {
next_rune(t);
switch t.r {
case '0'..'9', 'a'..'f', 'A'..'F':
case '0'..='9', 'a'..='f', 'A'..='F':
// Okay
case:
return;
@@ -100,7 +100,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
for i := 0; i < 4; i += 1 {
r := next_rune(t);
switch r {
case '0'..'9', 'a'..'f', 'A'..'F':
case '0'..='9', 'a'..='f', 'A'..='F':
// Okay
case:
return false;
@@ -149,7 +149,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
skip_alphanum :: proc(t: ^Tokenizer) {
for t.offset < len(t.data) {
switch next_rune(t) {
case 'A'..'Z', 'a'..'z', '0'..'9', '_':
case 'A'..='Z', 'a'..='z', '0'..='9', '_':
continue;
}
@@ -173,7 +173,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
token.kind = .EOF;
err = .EOF;
case 'A'..'Z', 'a'..'z', '_':
case 'A'..='Z', 'a'..='z', '_':
token.kind = .Ident;
skip_alphanum(t);
@@ -200,7 +200,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
case '-':
switch t.r {
case '0'..'9':
case '0'..='9':
// Okay
case:
// Illegal use of +/-
@@ -219,7 +219,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
}
fallthrough;
case '0'..'9':
case '0'..='9':
token.kind = t.parse_integers ? .Integer : .Float;
if t.spec == .JSON5 { // Hexadecimal Numbers
if curr_rune == '0' && (t.r == 'x' || t.r == 'X') {
@@ -361,7 +361,7 @@ is_valid_number :: proc(str: string, spec: Specification) -> bool {
switch s[0] {
case '0':
s = s[1:];
case '1'..'9':
case '1'..='9':
s = s[1:];
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
s = s[1:];
@@ -453,7 +453,7 @@ is_valid_string_literal :: proc(str: string, spec: Specification) -> bool {
for j := 0; j < 4; j += 1 {
c2 := hex[j];
switch c2 {
case '0'..'9', 'a'..'z', 'A'..'Z':
case '0'..='9', 'a'..='z', 'A'..='Z':
// Okay
case:
return false;

View File

@@ -1348,31 +1348,31 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
case .None:
copy(dest, src[:row_stride_in]);
case .Sub:
for i in 0..channels {
for i in 0..=channels {
dest[i] = src[i];
}
for k in 0..nk {
for k in 0..=nk {
dest[channels+k] = (src[channels+k] + dest[k]) & 255;
}
case .Up:
for k in 0..row_stride_in {
for k in 0..=row_stride_in {
dest[k] = (src[k] + up[k]) & 255;
}
case .Average:
for i in 0..channels {
for i in 0..=channels {
avg := up[i] >> 1;
dest[i] = (src[i] + avg) & 255;
}
for k in 0..nk {
for k in 0..=nk {
avg := u8((u16(up[channels+k]) + u16(dest[k])) >> 1);
dest[channels+k] = (src[channels+k] + avg) & 255;
}
case .Paeth:
for i in 0..channels {
for i in 0..=channels {
paeth := filter_paeth(0, up[i], 0);
dest[i] = (src[i] + paeth) & 255;
}
for k in 0..nk {
for k in 0..=nk {
paeth := filter_paeth(dest[k], up[channels+k], up[k]);
dest[channels+k] = (src[channels+k] + paeth) & 255;
}
@@ -1380,9 +1380,9 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
return false;
}
src = src [row_stride_in:];
up = dest;
dest = dest[row_stride_in:];
src = src[row_stride_in:];
up = dest;
dest = dest[row_stride_in:];
}
// Let's expand the bits

View File

@@ -347,7 +347,7 @@ is_abs :: proc(path: string) -> bool {
when ODIN_OS == "windows" {
if len(path) > 2 {
switch path[0] {
case 'A'..'Z', 'a'..'z':
case 'A'..='Z', 'a'..='z':
return path[1] == ':' && is_path_separator(path[2]);
}
}

View File

@@ -322,7 +322,7 @@ _is_abs :: proc(path: string) -> bool {
}
if len(path) > 2 {
switch path[0] {
case 'A'..'Z', 'a'..'z':
case 'A'..='Z', 'a'..='z':
return path[1] == ':' && is_path_separator(path[2]);
}
}

View File

@@ -39,7 +39,7 @@ volume_name_len :: proc(path: string) -> int {
c := path[0];
if path[1] == ':' {
switch c {
case 'a'..'z', 'A'..'Z':
case 'a'..='z', 'A'..='Z':
return 2;
}
}

View File

@@ -16,9 +16,9 @@ _digit_value :: proc(r: rune) -> int {
ri := int(r);
v: int = 16;
switch r {
case '0'..'9': v = ri-'0';
case 'a'..'z': v = ri-'a'+10;
case 'A'..'Z': v = ri-'A'+10;
case '0'..='9': v = ri-'0';
case 'a'..='z': v = ri-'a'+10;
case 'A'..='Z': v = ri-'A'+10;
}
return v;
}
@@ -557,9 +557,9 @@ quote_rune :: proc(buf: []byte, r: rune) -> string {
unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
hex_to_int :: proc(c: byte) -> int {
switch c {
case '0'..'9': return int(c-'0');
case 'a'..'f': return int(c-'a')+10;
case 'A'..'F': return int(c-'A')+10;
case '0'..='9': return int(c-'0');
case 'a'..='f': return int(c-'a')+10;
case 'A'..='F': return int(c-'A')+10;
}
return -1;
}
@@ -597,7 +597,7 @@ unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool
case '"': r = '"';
case '\'': r = '\'';
case '0'..'7':
case '0'..='7':
v := int(c-'0');
if len(s) < 2 {
return;

View File

@@ -314,9 +314,9 @@ write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe
is_printable :: proc(r: rune) -> bool {
if r <= 0xff {
switch r {
case 0x20..0x7e:
case 0x20..=0x7e:
return true;
case 0xa1..0xff: // ¡ through ÿ except for the soft hyphen
case 0xa1..=0xff: // ¡ through ÿ except for the soft hyphen
return r != 0xad; //
}
}

View File

@@ -85,9 +85,9 @@ is_delimiter :: proc(c: rune) -> bool {
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 '0'..='9': return false;
case 'a'..='z': return false;
case 'A'..='Z': return false;
case '_': return false;
}
return true;

View File

@@ -104,7 +104,7 @@ equal_fold :: proc(u, v: string) -> bool {
if tr < utf8.RUNE_SELF {
switch sr {
case 'A'..'Z':
case 'A'..='Z':
if tr == (sr+'a')-'A' {
continue loop;
}

View File

@@ -42,7 +42,7 @@ semaphore_destroy :: proc(s: ^Semaphore) {
semaphore_post :: proc(s: ^Semaphore, count := 1) {
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
for in 0..count-1 {
for in 0..<count {
res := darwin.semaphore_signal(s.handle);
assert(res == 0);
}

View File

@@ -33,7 +33,7 @@ semaphore_destroy :: proc(s: ^Semaphore) {
semaphore_post :: proc(s: ^Semaphore, count := 1) {
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
for in 0..count-1 {
for in 0..<count {
assert(unix.sem_post(&s.handle) == 0);
}
}

View File

@@ -33,7 +33,7 @@ semaphore_destroy :: proc(s: ^Semaphore) {
semaphore_post :: proc(s: ^Semaphore, count := 1) {
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
for in 0..count-1 {
for in 0..<count {
assert(unix.sem_post(&s.handle) == 0);
}
}

View File

@@ -421,8 +421,8 @@ scan_number :: proc(s: ^Scanner, ch: rune, seen_dot: bool) -> (rune, rune) {
scan_string :: proc(s: ^Scanner, quote: rune) -> (n: int) {
digit_val :: proc(ch: rune) -> int {
switch v := lower(ch); v {
case '0'..'9': return int(v - '0');
case 'a'..'z': return int(v - 'a');
case '0'..='9': return int(v - '0');
case 'a'..='z': return int(v - 'a');
}
return 16;
}
@@ -450,10 +450,10 @@ scan_string :: proc(s: ^Scanner, quote: rune) -> (n: int) {
switch ch {
case quote, 'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\':
ch = advance(s);
case '0'..'7': ch = scan_digits(s, advance(s), 8, 3);
case 'x': ch = scan_digits(s, advance(s), 16, 2);
case 'u': ch = scan_digits(s, advance(s), 16, 4);
case 'U': ch = scan_digits(s, advance(s), 16, 8);
case '0'..='7': ch = scan_digits(s, advance(s), 8, 3);
case 'x': ch = scan_digits(s, advance(s), 16, 2);
case 'u': ch = scan_digits(s, advance(s), 16, 4);
case 'U': ch = scan_digits(s, advance(s), 16, 8);
case:
error(s, "invalid char escape");
}

View File

@@ -39,7 +39,7 @@ encode :: proc(d: []u16, s: []rune) -> int {
d[n] = u16(r);
n += 1;
case _surr_self .. MAX_RUNE:
case _surr_self ..= MAX_RUNE:
if m+2 < n { break loop; }
r1, r2 := encode_surrogate_pair(r);
d[n] = u16(r1);
@@ -65,7 +65,7 @@ encode_string :: proc(d: []u16, s: string) -> int {
d[n] = u16(r);
n += 1;
case _surr_self .. MAX_RUNE:
case _surr_self ..= MAX_RUNE:
if m+2 < n { break loop; }
r1, r2 := encode_surrogate_pair(r);
d[n] = u16(r1);