diff options
author | 2021-10-23 22:54:39 -0700 | |
---|---|---|
committer | 2021-10-23 22:54:39 -0700 | |
commit | 309298fa42ca9f87ebcd88d697f285a68d3149c4 (patch) | |
tree | 6fb1d58f1725ee9291018f995645dd7579d65375 | |
parent | a0a2fa964b3ac5258806fda325e343f667330951 (diff) | |
download | bun-309298fa42ca9f87ebcd88d697f285a68d3149c4.tar.gz bun-309298fa42ca9f87ebcd88d697f285a68d3149c4.tar.zst bun-309298fa42ca9f87ebcd88d697f285a68d3149c4.zip |
Fix bug with MutableString.ensureValidIdentifier
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | src/string_mutable.zig | 26 |
2 files changed, 17 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore index 48f69ca15..5e2b4efcb 100644 --- a/.gitignore +++ b/.gitignore @@ -71,4 +71,5 @@ bun-test-scratch misctools/fetch src/deps/libiconv -src/deps/openssl
\ No newline at end of file +src/deps/openssl +src/tests.zig
\ No newline at end of file diff --git a/src/string_mutable.zig b/src/string_mutable.zig index a6149ff44..b8084dd50 100644 --- a/src/string_mutable.zig +++ b/src/string_mutable.zig @@ -54,18 +54,23 @@ pub const MutableString = struct { return "_"; } + var iterator = strings.CodepointIterator.init(str); + var cursor = strings.CodepointIterator.Cursor{}; + var has_needed_gap = false; var needs_gap = false; var start_i: usize = 0; + if (!iterator.next(&cursor)) return "_"; + // Common case: no gap necessary. No allocation necessary. - needs_gap = !js_lexer.isIdentifierStart(@intCast(js_lexer.CodePoint, str[0])); + needs_gap = !js_lexer.isIdentifierStart(cursor.c); if (!needs_gap) { // Are there any non-alphanumeric chars at all? - for (str[1..str.len]) |c, i| { - if (!js_lexer.isIdentifierContinue(@intCast(js_lexer.CodePoint, c))) { + while (iterator.next(&cursor)) { + if (!js_lexer.isIdentifierContinue(cursor.c) or cursor.width > 1) { needs_gap = true; - start_i = 1 + i; + start_i = cursor.i; break; } } @@ -78,21 +83,20 @@ pub const MutableString = struct { var i: usize = 0; var slice = str[start_i..]; + iterator = strings.CodepointIterator.init(slice); + cursor = strings.CodepointIterator.Cursor{}; - while (i < slice.len) : (i += 1) { - const c = @intCast(js_lexer.CodePoint, slice[i]); - if (js_lexer.isIdentifierContinue(c)) { + while (iterator.next(&cursor)) { + if (js_lexer.isIdentifierContinue(cursor.c) and cursor.width == 1) { if (needs_gap) { try mutable.appendChar('_'); needs_gap = false; has_needed_gap = true; } - - try mutable.appendChar(slice[i]); + try mutable.append(slice[cursor.i .. cursor.i + @as(u32, cursor.width)]); } else if (!needs_gap) { needs_gap = true; // skip the code point, replace it with a single _ - i += std.math.max(strings.utf8ByteSequenceLength(slice[i]), 1) - 1; } } @@ -212,7 +216,7 @@ test "MutableString" { const alloc = std.heap.page_allocator; var str = try MutableString.initCopy(alloc, "hello"); - expect(str.eql("hello")); + try expect(str.eql("hello")); } test "MutableString.ensureValidIdentifier" { |