Skip to content

Commit

Permalink
Fix bug with MutableString.ensureValidIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Oct 24, 2021
1 parent a0a2fa9 commit 309298f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ bun-test-scratch
misctools/fetch

src/deps/libiconv
src/deps/openssl
src/deps/openssl
src/tests.zig
26 changes: 15 additions & 11 deletions src/string_mutable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 309298f

Please sign in to comment.