diff --git a/lphy/src/main/java/lphy/evolution/traits/CharSetBlock.java b/lphy/src/main/java/lphy/evolution/traits/CharSetBlock.java index 4e2ae5b23..4ccb9b3ee 100644 --- a/lphy/src/main/java/lphy/evolution/traits/CharSetBlock.java +++ b/lphy/src/main/java/lphy/evolution/traits/CharSetBlock.java @@ -45,7 +45,8 @@ public String toString() { public static class Utils { // 1 458-659 3-629\3 662-.\3 - private final static String CHARSET_REGX = "^([0-9]+)$|^([0-9]+)\\-([0-9]+)(\\\\[0-9]+)*$|^([0-9]+)\\-(\\.)(\\\\[0-9]+)*$"; + private final static String CHARSET_REGX = + "^([0-9]+|[0-9]+\\-[0-9]+|[0-9]+\\-[0-9]+\\\\[0-9]+|[0-9]+\\-\\.\\\\[0-9]+)( ([0-9]+|[0-9]+\\-[0-9]+|[0-9]+\\-[0-9]+\\\\[0-9]+|[0-9]+\\-\\.\\\\[0-9]+))*$"; public static boolean isValid(String str) { return Pattern.matches(CHARSET_REGX, str); diff --git a/lphy/src/test/java/lphy/evolution/traits/CharSetBlockTest.java b/lphy/src/test/java/lphy/evolution/traits/CharSetBlockTest.java new file mode 100644 index 000000000..310105ad4 --- /dev/null +++ b/lphy/src/test/java/lphy/evolution/traits/CharSetBlockTest.java @@ -0,0 +1,61 @@ +package lphy.evolution.traits; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Walter Xie + */ +class CharSetBlockTest { + + + + @Test + void testParseCharSet() { + String str = "3-629\\3 1-629\\3 2-629\\3"; + assertTrue(CharSetBlock.Utils.isValid(str)); + + List charSetBlocks = CharSetBlock.Utils.getCharSetBlocks(str); + assertEquals(charSetBlocks.size(), 3); + + for (CharSetBlock block : charSetBlocks) { + System.out.println(block); + } + assertEquals(charSetBlocks.get(0).toString(), "CharSet{from=3, to=629, every=3}"); + assertEquals(charSetBlocks.get(1).toString(), "CharSet{from=1, to=629, every=3}"); + assertEquals(charSetBlocks.get(2).toString(), "CharSet{from=2, to=629, every=3}"); + + str = "1 458-659 897-898"; + assertTrue(CharSetBlock.Utils.isValid(str)); + + charSetBlocks = CharSetBlock.Utils.getCharSetBlocks(str); + assertEquals(charSetBlocks.size(), 3); + + assertEquals(charSetBlocks.get(0).toString(), "CharSet{from=1, to=1, every=1}"); + assertEquals(charSetBlocks.get(1).toString(), "CharSet{from=458, to=659, every=1}"); + assertEquals(charSetBlocks.get(2).toString(), "CharSet{from=897, to=898, every=1}"); + + str = "1 458-659 897-898"; + assertTrue(CharSetBlock.Utils.isValid(str)); + + charSetBlocks = CharSetBlock.Utils.getCharSetBlocks(str); + assertEquals(charSetBlocks.size(), 3); + + assertEquals(charSetBlocks.get(0).toString(), "CharSet{from=1, to=1, every=1}"); + assertEquals(charSetBlocks.get(1).toString(), "CharSet{from=458, to=659, every=1}"); + assertEquals(charSetBlocks.get(2).toString(), "CharSet{from=897, to=898, every=1}"); + + str = "4-457\\3 662-.\\3"; + assertTrue(CharSetBlock.Utils.isValid(str)); + + charSetBlocks = CharSetBlock.Utils.getCharSetBlocks(str); + assertEquals(charSetBlocks.size(), 2); + + assertEquals(charSetBlocks.get(0).toString(), "CharSet{from=4, to=457, every=3}"); + assertEquals(charSetBlocks.get(1).toString(), "CharSet{from=662, to=-1, every=3}"); + } +} \ No newline at end of file