Skip to content

Commit ce04b51

Browse files
committed
refact(rivet/tokenizer): simplify Tokenizer.decode_unicode_escaped_rune()
1 parent 9ce7a82 commit ce04b51

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

lib/rivet/src/ast/CHeader.ri

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
// Use of this source code is governed by an MIT license that can
33
// be found in the LICENSE file.
44

5-
import std/conv;
65
import std/traits;
76
import std/process;
87
import std/strings;
98
import { Path } from std/fs;
109

1110
import ../token;
12-
import ../report;
1311

1412
// Useful functions to get system `#define`s.
1513
// Will soon be used to implement `extern (C) import`.

lib/rivet/src/tokenizer/mod.ri

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,16 @@ func decode_u_escape_single(str: string, idx: uint) -> (uint, string) {
307307
}
308308

309309
// decode the flagged unicode escape sequences into their utf-8 bytes
310-
func decode_u_escapes(str: string, start: uint, escapes_pos: []uint) -> string {
311-
if escapes_pos.is_empty() {
312-
return str;
313-
}
314-
mut ss := @vec(string, escapes_pos.len * 2 + 1);
315-
ss.push(str[..escapes_pos[escapes_pos.len - 1] - start]);
316-
for i, pos in escapes_pos {
317-
idx := pos - start;
318-
(end_idx, segment) := decode_u_escape_single(str, idx);
310+
func decode_unicode_escaped_rune(str: string) -> string {
311+
(end_idx, segment) := decode_u_escape_single(str, 0);
312+
return if str.len == end_idx {
313+
segment
314+
} else {
315+
mut ss := @vec(string, 2);
319316
ss.push(segment);
320-
ss.push(if i + 1 < escapes_pos.len {
321-
str[end_idx..escapes_pos[i + 1] - start]
322-
} else {
323-
str[end_idx..]
324-
});
325-
}
326-
return utils.join(ss, "");
317+
ss.push(str[end_idx..]);
318+
utils.join(ss, "")
319+
};
327320
}
328321

329322
func trim_slash_line_break(s: string) -> string {

lib/rivet/src/tokenizer/next.ri

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ extend Tokenizer {
584584
if ch.len % 2 == 0 and (escaped_hex or escaped_unicode or escaped_octal) {
585585
if escaped_unicode {
586586
// there can only be one, so attempt to decode it now
587-
ch = decode_u_escapes(ch, 0, [0]);
587+
ch = decode_unicode_escaped_rune(ch);
588588
} else {
589589
// find escape sequence start positions
590590
mut escapes_pos := @vec(uint);

0 commit comments

Comments
 (0)