-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests and documentation. Improve edge case handling.
- Loading branch information
1 parent
166cd11
commit 6460110
Showing
58 changed files
with
2,006 additions
and
539 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/coditory/quark/i18n/ArgumentIndexExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.coditory.quark.i18n; | ||
|
||
import java.util.HashSet; | ||
import java.util.PrimitiveIterator; | ||
import java.util.Set; | ||
|
||
import static com.coditory.quark.i18n.Preconditions.expectNonNull; | ||
import static java.util.Collections.unmodifiableSet; | ||
|
||
final class ArgumentIndexExtractor { | ||
static Set<Integer> extractArgumentIndexes(String template) { | ||
expectNonNull(template, "template"); | ||
Set<Integer> result = new HashSet<>(); | ||
boolean escaped = false; | ||
int stack = 0; | ||
int prev = -1; | ||
for (PrimitiveIterator.OfInt it = template.codePoints().iterator(); it.hasNext(); ) { | ||
int c = it.next(); | ||
if (c == '\'') { | ||
escaped = true; | ||
} else if (c == '{' && it.hasNext() && (!escaped || stack > 0 && prev == '\'')) { | ||
if (prev == '\'') { | ||
stack = 0; | ||
escaped = false; | ||
} | ||
int number = extractIndex(it); | ||
if (number >= 0) { | ||
result.add(number); | ||
} | ||
} else if (escaped && c == '{') { | ||
stack += 1; | ||
} else if (escaped && c == '}') { | ||
stack = Math.max(0, stack - 1); | ||
if (stack == 0) { | ||
escaped = false; | ||
} | ||
} else if (escaped && stack == 0) { | ||
escaped = false; | ||
} | ||
prev = c; | ||
} | ||
return unmodifiableSet(result); | ||
} | ||
|
||
private static int extractIndex(PrimitiveIterator.OfInt iterator) { | ||
int c = iterator.next(); | ||
while (Character.isWhitespace(c)) { | ||
c = iterator.next(); | ||
} | ||
int number = 0; | ||
while (Character.isDigit(c)) { | ||
number = number * 10 + Character.digit(c, 10); | ||
c = iterator.next(); | ||
} | ||
while (Character.isWhitespace(c)) { | ||
c = iterator.next(); | ||
} | ||
return c == '}' || c == ',' ? number : -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.