Skip to content

Commit

Permalink
noice
Browse files Browse the repository at this point in the history
  • Loading branch information
original-brownbear committed Mar 4, 2024
1 parent 5d1d75a commit a01f84f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Reader create(Reader tokenStream) {
}

// source => target
private static Pattern rulePattern = Pattern.compile("(.*)\\s*=>\\s*(.*)\\s*$");
static final Pattern rulePattern = Pattern.compile("(.*)\\s*=>\\s*(.*)\\s*$");

/**
* parses a list of MappingCharFilter style rules into a normalize char map
Expand All @@ -58,14 +58,12 @@ private void parseRules(List<String> rules, NormalizeCharMap.Builder map) {
}
String lhs = parseString(m.group(1).trim());
String rhs = parseString(m.group(2).trim());
if (lhs == null || rhs == null) throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal mapping.");
map.add(lhs, rhs);
}
}

char[] out = new char[256];

private String parseString(String s) {
static String parseString(String s) {
char[] out = new char[256];
int readPos = 0;
int len = s.length();
int writePos = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.lucene.analysis.miscellaneous.WordDelimiterFilter.CATENATE_ALL;
import static org.apache.lucene.analysis.miscellaneous.WordDelimiterFilter.CATENATE_NUMBERS;
Expand Down Expand Up @@ -102,20 +101,17 @@ public static int getFlag(int flag, Settings settings, String key, boolean defau
return 0;
}

// source => type
private static final Pattern typePattern = Pattern.compile("(.*)\\s*=>\\s*(.*)\\s*$");

/**
* parses a list of MappingCharFilter style rules into a custom byte[] type table
*/
static byte[] parseTypes(Collection<String> rules) {
SortedMap<Character, Byte> typeMap = new TreeMap<>();
for (String rule : rules) {
Matcher m = typePattern.matcher(rule);
Matcher m = MappingCharFilterFactory.rulePattern.matcher(rule);
if (m.find() == false) {
throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]");
}
String lhs = parseString(m.group(1).trim());
String lhs = MappingCharFilterFactory.parseString(m.group(1).trim());
Byte rhs = parseType(m.group(2).trim());
if (lhs.length() != 1) throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Only a single character is allowed.");
if (rhs == null) throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal type.");
Expand Down Expand Up @@ -143,35 +139,6 @@ private static Byte parseType(String s) {
else return null;
}

private static String parseString(String s) {
char[] out = new char[256];
int readPos = 0;
int len = s.length();
int writePos = 0;
while (readPos < len) {
char c = s.charAt(readPos++);
if (c == '\\') {
if (readPos >= len) throw new RuntimeException("Invalid escaped char in [" + s + "]");
c = s.charAt(readPos++);
switch (c) {
case '\\' -> c = '\\';
case 'n' -> c = '\n';
case 't' -> c = '\t';
case 'r' -> c = '\r';
case 'b' -> c = '\b';
case 'f' -> c = '\f';
case 'u' -> {
if (readPos + 3 >= len) throw new RuntimeException("Invalid escaped char in [" + s + "]");
c = (char) Integer.parseInt(s.substring(readPos, readPos + 4), 16);
readPos += 4;
}
}
}
out[writePos++] = c;
}
return new String(out, 0, writePos);
}

@Override
public boolean breaksFastVectorHighlighter() {
return true;
Expand Down

0 comments on commit a01f84f

Please sign in to comment.