-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: feat: continue translate methods of legacy parser
- Loading branch information
1 parent
c13963c
commit 9e2f59c
Showing
6 changed files
with
192 additions
and
73 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
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
81 changes: 81 additions & 0 deletions
81
core/src/main/java/com/rexcantor64/triton/utils/ParserUtils.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,81 @@ | ||
package com.rexcantor64.triton.utils; | ||
|
||
import com.rexcantor64.triton.language.parser.TranslationConfiguration; | ||
import lombok.val; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Methods used in implementations of {@link com.rexcantor64.triton.api.language.MessageParser}. | ||
* | ||
* @since 4.0.0 | ||
*/ | ||
public class ParserUtils { | ||
|
||
/** | ||
* Find the indexes of all root "[pattern][/pattern]" tags in the given string. | ||
* <p> | ||
* Only the root tags are included, that is, nested tags are ignored. | ||
* For example, <code>[pattern][pattern][/pattern][/pattern]</code> would only | ||
* return the indexes for the outer tags. | ||
* <p> | ||
* Each array in the returned list corresponds to a different set of opening and closing tags, | ||
* and has size 4. | ||
* Indexes have the following meaning: | ||
* <ul> | ||
* <li>0: the first character of the opening tag</li> | ||
* <li>1: the character after the last character of the closing tag</li> | ||
* <li>2: the character after the last character of the opening tag</li> | ||
* <li>3: the first character of the closing tag</li> | ||
* </ul> | ||
* | ||
* @param input The string to search for opening and closing tags. | ||
* @param pattern The tags to search for (i.e. "lang" will search for "[lang]" and "[/lang]"). | ||
* @return A list of indexes of all the found tags, as specified by the method description. | ||
*/ | ||
public static List<Integer[]> getPatternIndexArray(String input, String pattern) { | ||
List<Integer[]> result = new ArrayList<>(); | ||
int start = -1; | ||
int openedAmount = 0; | ||
|
||
for (int i = 0; i < input.length(); i++) { | ||
char currentChar = input.charAt(i); | ||
if (currentChar == '[' && input.length() > i + pattern.length() + 1 && input.substring(i + 1, | ||
i + 2 + pattern.length()).equals(pattern + "]")) { | ||
if (start == -1) start = i; | ||
openedAmount++; | ||
i += 1 + pattern.length(); | ||
} else if (currentChar == '[' && input.length() > i + pattern.length() + 2 && input.substring(i + 1, | ||
i + 3 + pattern.length()).equals("/" + pattern + "]")) { | ||
openedAmount--; | ||
if (openedAmount == 0) { | ||
result.add(new Integer[]{start, i + 3 + pattern.length(), start + pattern.length() + 2, i}); | ||
start = -1; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Removes legacy <code>[args][/args]</code> tags from (the end of) translation keys. | ||
* Since v4.0.0, these tags are no longer needed and are therefore deprecated. | ||
* For backwards compatibility, ignore them. | ||
* | ||
* @param key The key, potentially ending in <code>[args]</code>, <code>[/args]</code>, or both. | ||
* @param configuration The settings being applied while translating the placeholder with this key. | ||
* @return The key with the <code>[args][/args]</code> removed. | ||
*/ | ||
public static String normalizeTranslationKey(String key, TranslationConfiguration<?> configuration) { | ||
val syntax = configuration.getFeatureSyntax().getArgs(); | ||
// The [args] tag is optional since v4.0.0, so strip it if it's present | ||
if (key.endsWith("[/" + syntax + "]")) { | ||
key = key.substring(0, key.length() - syntax.length() - 3); | ||
} | ||
if (key.endsWith("[" + configuration.getFeatureSyntax().getArgs() + "]")) { | ||
key = key.substring(0, key.length() - syntax.length() - 2); | ||
} | ||
return key; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
core/src/test/java/com/rexcantor64/triton/utils/ParserUtilsTest.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,29 @@ | ||
package com.rexcantor64.triton.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ParserUtilsTest { | ||
|
||
@Test | ||
public void testGetPatternIndexArray() { | ||
String input = "Lorem ipsum [tag]dolor [tag]sit[/tag] amet[/tag], [tag2]consectetur[/tag2] [tag]adipiscing elit[/tag]. Nullam posuere."; | ||
|
||
List<Integer[]> result = ParserUtils.getPatternIndexArray(input, "tag"); | ||
|
||
List<Integer[]> expected = Arrays.asList( | ||
new Integer[]{12, 48, 17, 42}, | ||
new Integer[]{75, 101, 80, 95} | ||
); | ||
|
||
assertEquals(expected.size(), result.size()); | ||
for (int i = 0; i < expected.size(); i++) { | ||
assertArrayEquals(expected.get(i), result.get(i)); | ||
} | ||
} | ||
} |
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