Skip to content

Commit

Permalink
refact(rivet+rivetc): remove need to always use the b prefix to cre…
Browse files Browse the repository at this point in the history
…ate a byte literal

If a value of type `uint8` is expected, then there is no need to use this prefix, otherwise
the literal will become of type `rune`.
  • Loading branch information
StunxFS committed Dec 17, 2023
1 parent 8c79915 commit ccf6bb4
Show file tree
Hide file tree
Showing 38 changed files with 253 additions and 242 deletions.
2 changes: 1 addition & 1 deletion cmd/src/tools/cmd_fmt.ri
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub func cmd_fmt(args: []string) -> ! {
fp.add_usage_example("directory/");
fp.add_usage_example("-w file.ri");
write_to_file := fp.bool_flag(
"write-to-file", b'w', "Write result to source file(s) instead of to stdout.") ?? false;
"write-to-file", 'w', "Write result to source file(s) instead of to stdout.") ?? false;
remaining := fp.finalize() catch |err_fp| {
console.ewriteln(fp.usage());
utils.error(err_fp.to_string())
Expand Down
2 changes: 1 addition & 1 deletion cmd/src/tools/cmd_new.ri
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub func cmd_new(args: []string, is_init: bool) -> ! {
fp.add_usage_example("-t lib");
fp.add_usage_example("my_project_name -t lib");
template = fp.string_flag(
"template", b't', "Template used by the project (default: bin)."
"template", 't', "Template used by the project (default: bin)."
) ?? "bin";
}
init_git := fp.bool_flag(
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/StringBuilder.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct StringBuilder < Stringable {
self.write_raw_with_len(s.ptr, s.len);
}
}
self.write_byte(b'\n');
self.write_byte('\n');
}

pub func write_join(mut self, ss: []string, sep: string := "") {
Expand All @@ -85,7 +85,7 @@ pub struct StringBuilder < Stringable {

pub func writeln_fmt(mut self, s: string, args: ...Stringable) {
self.write_string(s.fmt(args));
self.write_byte(b'\n');
self.write_byte('\n');
}

pub func last_n(self, n: uint) -> string {
Expand Down
22 changes: 11 additions & 11 deletions lib/core/src/StringFormatter.ri
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub struct StringFormatter {
b := unsafe { self.buf.ptr[self.i] };
b2 := if self.i + 1 < self.buf.len unsafe { self.buf.ptr[self.i + 1] } else { 0 };
match b {
b'{' -> {
if b2 == b'{' { // escaping '{'
self.res.write_byte(b'{');
'{' -> {
if b2 == '{' { // escaping '{'
self.res.write_byte('{');
self.i += 1;
} else if b2 == b':' || b2 == b'}' {
if b2 == b':' {
} else if b2 == ':' || b2 == '}' {
if b2 == ':' {
self.i += 1;
}
(has_fwidth, mut fwidth) := self.fwidth();
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct StringFormatter {
self.i += 1;
mut buf := StaticBuffer();
while unsafe {
self.buf.ptr[self.i] != b'}' && self.buf.ptr[self.i] != b':'
self.buf.ptr[self.i] != '}' && self.buf.ptr[self.i] != ':'
} {
buf.push(unsafe { self.buf.ptr[self.i] });
self.i += 1;
Expand Down Expand Up @@ -95,9 +95,9 @@ pub struct StringFormatter {
);
}
},
b'}' -> {
if b2 == b'}' { // escaping '}'
self.res.write_byte(b'}');
'}' -> {
if b2 == '}' { // escaping '}'
self.res.write_byte('}');
self.i += 1;
} else {
runtime_error(
Expand All @@ -113,13 +113,13 @@ pub struct StringFormatter {
}

func fwidth(mut self) -> (bool, int) {
if unsafe { self.buf.ptr[self.i] != b':' } {
if unsafe { self.buf.ptr[self.i] != ':' } {
return (false, 0);
}
self.i += 1;
start := self.i;
mut buf := StaticBuffer();
while unsafe { self.buf.ptr[self.i] != b'}' } {
while unsafe { self.buf.ptr[self.i] != '}' } {
buf.push(unsafe { self.buf.ptr[self.i] });
self.i += 1;
if self.i >= self.buf.len {
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/demangle.ri
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func demangle_symbol(name: string) -> string {
buf.clear();

byte = unsafe { name.ptr[idx] };
if byte == b'F' {
if byte == 'F' {
res.write_string("()");
break;
} else if byte == b'M' {
} else if byte == 'M' {
res.write_string("(self)");
break;
} else if !byte.is_digit() {
Expand Down
6 changes: 3 additions & 3 deletions lib/core/src/int.ri
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extend int32 < Stringable {
if is_neg {
// prepend `-` if it's negative
index -= 1;
buf[index] = b'-';
buf[index] = '-';
}

diff := @as(uint, max) - index;
Expand Down Expand Up @@ -120,7 +120,7 @@ extend int64 < Stringable {

while n > 0 {
n1 := n / 100;
d = @as(int64, @as(uint32, n - (n1 * 100))) << @as(int64, 1);
d = @as(int64, @as(uint32, n - (n1 * 100))) << 1;
n = n1;
buf[index] = digitPairs.ptr[@as(uint, d)];
index -= 1;
Expand All @@ -138,7 +138,7 @@ extend int64 < Stringable {
if is_neg {
// prepend `-` if it's negative
index -= 1;
buf[index] = b'-';
buf[index] = '-';
}

diff := @as(uint, max) - index;
Expand Down
20 changes: 10 additions & 10 deletions lib/core/src/rune.ri
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extend rune < Stringable {
}
if num < radix {
num_ := @as(uint8, num);
byte := if num_ < 10 { b'0' + num_ } else { b'a' + num_ - 10 };
byte := if num_ < 10 { b'0' + num_ } else { 'a' + num_ - 10 };
return byte.to_rune();
}
return none;
Expand Down Expand Up @@ -79,21 +79,21 @@ func utf32_decode_to_buffer(code: rune, buffer: [&]mut uint8) -> uint {
1
},
(icode <= 2047) -> { // 0x7FF
buffer[0] = @as(uint8, 192) | @as(uint8, icode >> 6); // 0xC0 - 110xxxxx
buffer[1] = @as(uint8, 128) | @as(uint8, icode & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[0] = 192 | @as(uint8, icode >> 6); // 0xC0 - 110xxxxx
buffer[1] = 128 | @as(uint8, icode & 63); // 0x80 - 0x3F - 10xxxxxx
2
},
(icode <= 65535) -> { // 0xFFFF
buffer[0] = @as(uint8, 224) | @as(uint8, icode >> 12); // 0xE0 - 1110xxxx
buffer[1] = @as(uint8, 128) | (@as(uint8, icode >> 6) & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[2] = @as(uint8, 128) | @as(uint8, icode & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[0] = 224 | @as(uint8, icode >> 12); // 0xE0 - 1110xxxx
buffer[1] = 128 | (@as(uint8, icode >> 6) & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[2] = 128 | @as(uint8, icode & 63); // 0x80 - 0x3F - 10xxxxxx
3
},
(icode <= 1114111) -> { // 0x10FFFF
buffer[0] = @as(uint8, 240) | @as(uint8, icode >> 18); // 0xF0 - 11110xxx
buffer[1] = @as(uint8, 128) | (@as(uint8, icode >> 12) & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[2] = @as(uint8, 128) | (@as(uint8, icode >> 6) & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[3] = @as(uint8, 128) | @as(uint8, icode & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[0] = 240 | @as(uint8, icode >> 18); // 0xF0 - 11110xxx
buffer[1] = 128 | (@as(uint8, icode >> 12) & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[2] = 128 | (@as(uint8, icode >> 6) & 63); // 0x80 - 0x3F - 10xxxxxx
buffer[3] = 128 | @as(uint8, icode & 63); // 0x80 - 0x3F - 10xxxxxx
4
},
else -> 0
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/string.c.ri
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct string < Stringable, Hashable, Throwable {
mut is_in_word := false;
mut is_space := false;
for i, c in self.as_bytes() {
is_space = c in [@as(uint8, 32), 9, 10];
is_space = c in [32, 9, 10];
if !is_space {
word_len += 1;
}
Expand Down Expand Up @@ -669,7 +669,7 @@ pub struct string < Stringable, Hashable, Throwable {
/// Returns an iterator that iterates over the slices of `self` that are not
/// any of the bytes in `delimiter_bytes`.
///
/// `" abc def ghi ".tokenize(b' ')` will return slices for "abc", "def",
/// `" abc def ghi ".tokenize(' ')` will return slices for "abc", "def",
/// "ghi", none, in that order.
///
/// If `self` is empty, the iterator will return none.
Expand Down
54 changes: 27 additions & 27 deletions lib/core/src/parse_int.ri → lib/core/src/string_parse_int.ri
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#[inline]
func lower(c: uint8) -> uint8 {
return c | (b'x' - b'X');
return c | ('x' - 'X');
}

extend string {
Expand All @@ -23,22 +23,22 @@ extend string {
(base == 0) -> {
// look for octal, hex prefix.
base = 10;
if s[0] == b'0' {
if s[0] == '0' {
match {
(s.len >= 3 && lower(s[1]) == b'b') -> {
(s.len >= 3 && lower(s[1]) == 'b') -> {
base = 2;
start_index = 2;
},
(s.len >= 3 && lower(s[1]) == b'o') -> {
(s.len >= 3 && lower(s[1]) == 'o') -> {
base = 8;
start_index = 2;
},
(s.len >= 3 && lower(s[1]) == b'x') -> {
(s.len >= 3 && lower(s[1]) == 'x') -> {
base = 16;
start_index = 2;
},
// manage leading zeros in decimal base's numbers
s.len >= 2 && (s[1] >= b'0' && s[1] <= b'9') -> {
s.len >= 2 && (s[1] >= '0' && s[1] <= '9') -> {
base = 10;
start_index = 1;
},
Expand All @@ -65,7 +65,7 @@ extend string {
max_val: uint := if bit_size == 64 {
uint.MAX
} else {
(@as(uint, 1) << bit_size) - 1
(1 << bit_size) - 1
};
mut underscores := false;
mut n: uint := 0;
Expand All @@ -77,12 +77,12 @@ extend string {

mut d: uint8 := 0;
match {
(c == b'_' && base0) -> {
(c == '_' && base0) -> {
underscores = true;
continue;
},
(b'0' <= c && c <= b'9') -> d = c - b'0',
(b'a' <= cl && cl <= b'z') -> d = cl - b'a' + 1,
(c >= '0' && c <= '9') -> d = c - '0',
(cl >= 'a' && cl <= 'z') -> d = cl - 'a' + 1,
else -> throw InvalidSyntaxError("invalid syntax")
}

Expand Down Expand Up @@ -133,9 +133,9 @@ extend string {
// pick off leading sign.
mut s0 := self;
mut neg := false;
if self[0] == b'+' {
if self[0] == '+' {
s0 = self.substr(1);
} else if self[0] == b'-' {
} else if self[0] == '-' {
neg = true;
s0 = self.substr(1);
}
Expand Down Expand Up @@ -167,49 +167,49 @@ func underscore_ok(s_: string) -> bool {
// 0 for a digit or base prefix,
// _ for an underscore,
// ! for none of the above.
mut saw := b'^';
mut saw := '^';
mut i: uint := 0;
mut s := s_;

// optional sign.
if s.len >= 1 && (s[0] == b'-' || s[0] == b'+') {
if s.len >= 1 && (s[0] == '-' || s[0] == '+') {
s = s.substr(1);
}

// optional base prefix.
mut hex := false;
if s.len >= 2 && s[i] == b'0' && (
lower(s[1]) == b'b' || lower(s[1]) == b'o' || lower(s[1]) == b'x'
if s.len >= 2 && s[i] == '0' && (
lower(s[1]) == 'b' || lower(s[1]) == 'o' || lower(s[1]) == 'x'
) {
// base prefix counts as a digit for "underscore as digit separator"
i = 2;
saw = b'0';
hex = lower(s[1]) == b'x';
saw = '0';
hex = lower(s[1]) == 'x';
}

// number proper.
while i < s.len : i += 1 {
// digits are always okay.
if (b'0' <= s[i] && s[i] <= b'9') ||
(hex && b'a' <= lower(s[i]) && lower(s[i]) <= b'f') {
saw = b'0';
if (s[i] >= '0' && s[i] <= '9') ||
(hex && lower(s[i]) >= 'a' && lower(s[i]) <= 'f') {
saw = '0';
continue;
}
// underscore must follow digit.
if s[i] == b'_' {
if saw != b'0' {
if s[i] == '_' {
if saw != '0' {
return false;
}
saw = b'_';
saw = '_';
continue;
}
// underscore must also be followed by digit.
if saw == b'_' {
if saw == '_' {
return false;
}
// saw non-digit, non-underscore.
saw = b'!';
saw = '!';
}

return saw != b'_';
return saw != '_';
}
20 changes: 10 additions & 10 deletions lib/core/src/uint.ri
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@ extend uint8 < Stringable {
/// Returns `true` if the byte is in range 0-9 and `false` otherwise.
#[inline]
pub func is_digit(self) -> bool {
return self >= b'0' && self <= b'9';
return self >= '0' && self <= '9';
}

/// Returns `true` if the byte is a binary digit (0 or 1) and `false` otherwise.
#[inline]
pub func is_bin_digit(self) -> bool {
return self == b'0' || self == b'1';
return self == '0' || self == '1';
}

/// Returns `true` if the byte is in range 0-7 and `false` otherwise.
#[inline]
pub func is_oct_digit(self) -> bool {
return self >= b'0' && self <= b'7';
return self >= '0' && self <= '7';
}

/// Returns `true` if the byte is either in range 0-9, a-f or A-F and `false`
/// otherwise.
#[inline]
pub func is_hex_digit(self) -> bool {
return (self >= b'0' && self <= b'9')
|| (self >= b'a' && self <= b'f')
|| (self >= b'A' && self <= b'F');
return (self >= '0' && self <= '9')
|| (self >= 'a' && self <= 'f')
|| (self >= 'A' && self <= 'F');
}

/// Returns `true` if the byte is in range a-z or A-Z and `false` otherwise.
#[inline]
pub func is_letter(self) -> bool {
return (self >= b'a' && self <= b'z') || (self >= b'A' && self <= b'Z');
return (self >= 'a' && self <= 'z') || (self >= 'A' && self <= 'Z');
}

/// Returns `true` if the byte is in range a-z or A-Z or 1-9 and `false` otherwise.
Expand All @@ -62,13 +62,13 @@ extend uint8 < Stringable {
/// Returns `true` if the byte is upper and `false` otherwise.
#[inline]
pub func is_upper(self) -> bool {
return (self >= b'A' && self <= b'Z');
return (self >= 'A' && self <= 'Z');
}

/// Returns `true` if the byte is lower and `false` otherwise.
#[inline]
pub func is_lower(self) -> bool {
return (self >= b'a' && self <= b'z');
return (self >= 'a' && self <= 'z');
}

/// Calculates length to read from the first byte.
Expand Down Expand Up @@ -132,7 +132,7 @@ extend uint32 < Stringable {

while n > 0 {
n1 := n / 100;
d = (n - (n1 * 100)) << @as(uint32, 1);
d = (n - (n1 * 100)) << 1;
n = n1;
buf[index] = digitPairs.ptr[d];
index -= 1;
Expand Down
Loading

0 comments on commit ccf6bb4

Please sign in to comment.