Skip to content

Commit

Permalink
CharSet parsing is incorrect #411
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Aug 16, 2023
1 parent 8d44939 commit e97a535
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lphy/src/main/java/lphy/evolution/traits/CharSetBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
61 changes: 61 additions & 0 deletions lphy/src/test/java/lphy/evolution/traits/CharSetBlockTest.java
Original file line number Diff line number Diff line change
@@ -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<CharSetBlock> 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}");
}
}

0 comments on commit e97a535

Please sign in to comment.