MCPcopy Create free account
hub / github.com/BaseXdb/basex / Escape

Class Escape

basex-core/src/main/java/org/basex/query/util/regex/Escape.java:11–448  ·  view source on GitHub ↗

Escape sequence. @author BaseX Team, BSD License @author Leo Woerteler

Source from the content-addressed store, hash-verified

9 * @author Leo Woerteler
10 */
11public final class Escape extends RegExp {
12 /** Comparator for int ranges. */
13 private static final Comparator<int[]> CMP = Comparator.comparingInt(o -> o[0]);
14 /** Character classes. */
15 private static final Map<String, CharRange[]> MAP = new HashMap<>();
16
17 /** Initial name characters. */
18 private static final String INITIAL;
19 /** Everything except initial name characters. */
20 private static final String NOT_INITIAL;
21 /** Name characters. */
22 private static final String CHAR;
23 /** Everything except name characters. */
24 private static final String NOT_CHAR;
25 /** Word characters (anything but punctuation, separators and "other"; XQuery/XSD {@code \w}). */
26 public static final String WORD = "[^\\p{P}\\p{Z}\\p{C}]";
27 /** Non-word characters (XQuery/XSD {@code \W}). */
28 public static final String NOT_WORD = "[\\p{P}\\p{Z}\\p{C}]";
29 /** Digits. */
30 private static final String DIGIT = "\\p{Nd}";
31 /** Everything except digits. */
32 private static final String NOT_DIGIT = "\\P{Nd}";
33
34 /** Image. */
35 private final String img;
36
37 /**
38 * Constructor.
39 * @param img image string
40 */
41 private Escape(final String img) {
42 this.img = img;
43 }
44
45 /**
46 * Creates a regular expression from the given escape sequence.
47 * @param esc escape sequence
48 * @return regular expression
49 */
50 public static RegExp get(final String esc) {
51 if(esc.startsWith("\\p{Is") || esc.startsWith("\\P{Is")) {
52 final CharRange[] rng = MAP.get(esc);
53 return rng != null ? new CharClass(new CharGroup(rng), null) : null;
54 }
55 return new Escape(switch(esc.charAt(1)) {
56 case 'i' -> '[' + INITIAL + ']';
57 case 'I' -> '[' + NOT_INITIAL + ']';
58 case 'c' -> '[' + CHAR + ']';
59 case 'C' -> '[' + NOT_CHAR + ']';
60 case 'd' -> DIGIT;
61 case 'D' -> NOT_DIGIT;
62 case 'w' -> WORD;
63 case 'W' -> NOT_WORD;
64 default -> esc;
65 });
66 }
67
68 /**

Callers

nothing calls this directly

Calls 6

mergeMethod · 0.95
serializeMethod · 0.95
invertMethod · 0.95
addMethod · 0.95
forEachMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected