Skip to content

Commit cfa52c8

Browse files
committed
Slightly optimize advanceCursor() methods
1 parent b04d0bd commit cfa52c8

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

rewrite-json/src/main/java/org/openrewrite/json/internal/JsonParserVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,11 @@ private Space prefix(Token token) {
275275
}
276276

277277
public int advanceCursor(int newCodePointIndex) {
278-
for (; codePointCursor < newCodePointIndex; codePointCursor++) {
279-
cursor = source.offsetByCodePoints(cursor, 1);
278+
if (newCodePointIndex <= codePointCursor) {
279+
return cursor;
280280
}
281+
cursor = source.offsetByCodePoints(cursor, newCodePointIndex - codePointCursor);
282+
codePointCursor = newCodePointIndex;
281283
return cursor;
282284
}
283285

rewrite-toml/src/main/java/org/openrewrite/toml/internal/TomlParserVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,11 @@ private Space prefix(Token token) {
441441
}
442442

443443
public int advanceCursor(int newCodePointIndex) {
444-
for (; codePointCursor < newCodePointIndex; codePointCursor++) {
445-
cursor = source.offsetByCodePoints(cursor, 1);
444+
if (newCodePointIndex <= codePointCursor) {
445+
return cursor;
446446
}
447+
cursor = source.offsetByCodePoints(cursor, newCodePointIndex - codePointCursor);
448+
codePointCursor = newCodePointIndex;
447449
return cursor;
448450
}
449451

rewrite-xml/src/main/java/org/openrewrite/xml/internal/XmlParserVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,11 @@ private String prefix(Token token) {
467467
*/
468468
@SuppressWarnings("UnusedReturnValue")
469469
private int advanceCursor(int newCodePointIndex) {
470-
for (; codePointCursor < newCodePointIndex; codePointCursor++) {
471-
cursor = source.offsetByCodePoints(cursor, 1);
470+
if (newCodePointIndex <= codePointCursor) {
471+
return cursor;
472472
}
473+
cursor = source.offsetByCodePoints(cursor, newCodePointIndex - codePointCursor);
474+
codePointCursor = newCodePointIndex;
473475
return cursor;
474476
}
475477

0 commit comments

Comments
 (0)