Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/src/main/java/com/github/simy4/coregex/core/Coregex.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
public abstract class Coregex implements Serializable {

private static final long serialVersionUID = 1L;
private static final Lazy<Coregex> EMPTY =
new Lazy<>(() -> Set.builder().build().quantify(0, 0, Quantified.Type.GREEDY));

/**
* Constructs {@link Coregex} from provided {@link Pattern} instance.
Expand Down Expand Up @@ -89,7 +87,7 @@ public static Coregex any(int flags) {
* @return predefined constructor for empty regex.
*/
public static Coregex empty() {
return EMPTY.get();
return Quantified.EMPTY;
}

/**
Expand Down Expand Up @@ -601,6 +599,8 @@ public static final class Quantified extends Coregex {

private static final long serialVersionUID = 1L;

static final Quantified EMPTY = new Quantified(Set.EMPTY, 0, 0, Type.GREEDY);

private final Coregex quantified;
private final int min;
private final int max;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ private void setItem(Set.Builder set, Context ctx) {
} else {
Set metachar = metachar(ctx);
if (metachar.single()) {
rangeOrSingle(set, (char) metachar.sample(0L).orElse(0), ctx);
rangeOrSingle(set, (char) metachar.sample(0L), ctx);
} else {
set.union(metachar);
}
Expand Down Expand Up @@ -510,7 +510,8 @@ private int flags(Context ctx) {

/*
* <pre>{@code
* metachar ::= '\', 't' | 'r' | 'n' | 'd' | 'D' | 'w' | 'W' | 's' | 'p', '{', ? posix ?, '}' | 'S' | single
* metachar ::= '\', 't' | 'r' | 'n' | 'f' | 'a' | 'e' | 'c', ? char ? | 'd' | 'D' | 'w' | 'W'
* | 's' | 'S' | 'h' | 'H' | 'v' | 'V' | 'p', '{', ? posix ?, '}' | single
* }</pre>
*/
@SuppressWarnings("fallthrough")
Expand Down Expand Up @@ -549,11 +550,50 @@ private Set metachar(Context ctx) {
break;
case 's':
ctx.match('s');
metachar.set('\r', '\n', '\t', '\f', ' ');
metachar.set(' ', '\t', '\n', '\u000B', '\f', '\r');
break;
case 'S':
ctx.match('S');
metachar.set('\r', '\n', '\t', '\f', ' ').negate();
metachar.set(' ', '\t', '\n', '\u000B', '\f', '\r').negate();
break;
case 'h':
ctx.match('h');
metachar
.set(' ', '\t', '\u00A0', '\u1680', '\u180E', '\u202F', '\u205F', '\u3000')
.range('\u2000', '\u200A');
break;
case 'H':
ctx.match('H');
metachar
.set(' ', '\t', '\u00A0', '\u1680', '\u180E', '\u202F', '\u205F', '\u3000')
.range('\u2000', '\u200A')
.negate();
break;
case 'v':
ctx.match('v');
metachar.set('\n', '\u000B', '\f', '\r', '\u0085', '\u2028', '\u2029');
break;
case 'V':
ctx.match('V');
metachar.set('\n', '\u000B', '\f', '\r', '\u0085', '\u2028', '\u2029').negate();
break;
case 'f':
ctx.match('f');
metachar.single('\f');
break;
case 'a':
ctx.match('a');
metachar.single('\u0007');
break;
case 'e':
ctx.match('e');
metachar.single('\u001B');
break;
case 'c':
ctx.match('c');
char control = ctx.peek();
ctx.match(control);
metachar.single((char) (control ^ 0x40));
break;
// handling predefined predicates from java.util.regex.CharPredicates
case 'p':
Expand All @@ -562,16 +602,20 @@ private Set metachar(Context ctx) {
String posix = ctx.span('}');
switch (posix) {
case "all":
metachar.range(Character.MIN_VALUE, Character.MIN_SURROGATE);
metachar.matching(any -> true, "\\p{all}");
break;
case "Lower":
case "javaLowerCase":
metachar.range('a', 'z');
break;
case "javaLowerCase":
metachar.matching(Character::isLowerCase, "\\p{javaLowerCase}");
break;
case "Upper":
case "javaUpperCase":
metachar.range('A', 'Z');
break;
case "javaUpperCase":
metachar.matching(Character::isUpperCase, "\\p{javaUpperCase}");
break;
case "ASCII":
metachar.range(Character.MIN_VALUE, (char) 0x7F);
break;
Expand Down Expand Up @@ -602,9 +646,13 @@ private Set metachar(Context ctx) {
break;

case "Blank":
metachar.set(' ', '\t');
break;
case "Space":
metachar.set(' ', '\t', '\n', '\u000B', '\f', '\r');
break;
case "javaWhitespace":
metachar.set(' ', '\t');
metachar.matching(Character::isWhitespace, "\\p{javaWhitespace}");
break;
default:
throw ctx.unsupported("posix char class \\" + posix);
Expand Down
55 changes: 0 additions & 55 deletions core/src/main/java/com/github/simy4/coregex/core/Lazy.java

This file was deleted.

124 changes: 71 additions & 53 deletions core/src/main/java/com/github/simy4/coregex/core/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.io.Serializable;
import java.util.BitSet;
import java.util.OptionalInt;
import java.util.function.IntPredicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;

Expand All @@ -36,41 +36,39 @@ public final class Set extends Coregex implements Serializable {

private static final long serialVersionUID = 1L;

static final Lazy<Set> ALL =
new Lazy<>(
() -> {
BitSet chars = new BitSet(Character.MIN_SURROGATE);
chars.set(Character.MIN_VALUE, Character.MIN_SURROGATE);
chars.clear('\r');
chars.clear('\n');
chars.clear('\u0085');
chars.clear('\u2028');
chars.clear('\u2029');
return new Set(chars, ".");
});
static final Lazy<Set> UNIX_LINES =
new Lazy<>(
() -> {
BitSet chars = new BitSet(Character.MIN_SURROGATE);
chars.set(Character.MIN_VALUE, Character.MIN_SURROGATE);
chars.clear('\n');
return new Set(chars, ".");
});
static final Lazy<Set> DOTALL =
new Lazy<>(
() -> {
BitSet chars = new BitSet(Character.MIN_SURROGATE);
chars.set(Character.MIN_VALUE, Character.MIN_SURROGATE);
return new Set(chars, ".");
});
static final Set EMPTY = new Set(new BitSet(0), "");

/*
* All chars except surrogates, which only exist in pairs: supplementary code points are out of
* scope. Every set, including negated ones, is a subset of this one.
*/
static final Set DOTALL;
static final Set UNIX_LINES;
static final Set ALL;

static {
BitSet dotAll = new BitSet(Character.MAX_VALUE + 1);
dotAll.set(Character.MIN_VALUE, Character.MAX_VALUE + 1);
dotAll.clear(Character.MIN_SURROGATE, Character.MAX_SURROGATE + 1);
DOTALL = new Set(dotAll, ".");
BitSet unixLines = (BitSet) dotAll.clone();
unixLines.clear('\n');
UNIX_LINES = new Set(unixLines, ".");
BitSet all = (BitSet) unixLines.clone();
all.clear('\r');
all.clear('\u0085');
all.clear('\u2028');
all.clear('\u2029');
ALL = new Set(all, ".");
}

public static Set any(int flags) {
if (0 != (flags & Pattern.DOTALL)) {
return DOTALL.get();
return DOTALL;
} else if (0 != (flags & Pattern.UNIX_LINES)) {
return UNIX_LINES.get();
return UNIX_LINES;
} else {
return ALL.get();
return ALL;
}
}

Expand Down Expand Up @@ -134,9 +132,9 @@ private Set(BitSet chars, String description) {

@Override
void generate(Context ctx) {
OptionalInt sample = sample(ctx.rng.nextLong());
if (sample.isPresent()) {
ctx.append((char) sample.getAsInt());
int sample = sample(ctx.rng.nextLong());
if (0 <= sample) {
ctx.append((char) sample);
}
}

Expand Down Expand Up @@ -170,16 +168,19 @@ public Stream<Set> shrink() {
return builder.build();
}

OptionalInt sample(long seed) {
/**
* @return sampled char or -1 if this set is empty.
*/
int sample(long seed) {
if (chars.isEmpty()) {
return OptionalInt.empty();
return -1;
}
long skip = Math.abs(seed % chars.cardinality());
int sample = chars.nextSetBit(0);
while (skip-- > 0) {
sample = chars.nextSetBit(sample + 1);
}
return OptionalInt.of(sample);
return sample;
}

@Override
Expand Down Expand Up @@ -235,13 +236,8 @@ public Builder range(char start, char end) {
}
chars.set(start, end + 1);
if (0 != (flags & Pattern.CASE_INSENSITIVE)) {
for (char ch = start; ch <= end; ch++) {
if (Character.isLowerCase(ch)) {
chars.set(Character.toUpperCase(ch));
}
if (Character.isUpperCase(ch)) {
chars.set(Character.toLowerCase(ch));
}
for (int ch = start; ch <= end; ch++) {
foldCase((char) ch);
}
}
description.append(start).append('-').append(end);
Expand Down Expand Up @@ -295,26 +291,48 @@ public Builder intersect(Set set) {
*/
public Builder single(char ch) {
chars.set(ch);
if (0 != (flags & Pattern.CASE_INSENSITIVE)) {
if (Character.isLowerCase(ch)) {
chars.set(Character.toUpperCase(ch));
}
if (Character.isUpperCase(ch)) {
chars.set(Character.toLowerCase(ch));
foldCase(ch);
description.append(ch);
return this;
}

/*
* Adds all chars matching given predicate, i.e. Character::isLowerCase.
*/
Builder matching(IntPredicate predicate, String description) {
for (int ch = DOTALL.chars.nextSetBit(0); ch >= 0; ch = DOTALL.chars.nextSetBit(ch + 1)) {
if (predicate.test(ch)) {
chars.set(ch);
foldCase((char) ch);
}
}
description.append(ch);
this.description.append(description);
return this;
}

/*
* Same as Pattern: CASE_INSENSITIVE alone only folds US-ASCII, UNICODE_CASE (implied by
* UNICODE_CHARACTER_CLASS) folds the rest.
*/
private void foldCase(char ch) {
if (0 == (flags & Pattern.CASE_INSENSITIVE)
|| (ch > 0x7F
&& 0 == (flags & (Pattern.UNICODE_CASE | Pattern.UNICODE_CHARACTER_CLASS)))) {
return;
}
chars.set(Character.toUpperCase(ch));
chars.set(Character.toLowerCase(ch));
}

/**
* Negates this set.
*
* @return this builder instance
*/
public Builder negate() {
chars.flip(Character.MIN_VALUE, Character.MIN_SURROGATE);
chars.and(any(flags).chars);
// negation is not affected by DOTALL or UNIX_LINES: [^a] matches line terminators
chars.flip(Character.MIN_VALUE, Character.MAX_VALUE + 1);
chars.and(DOTALL.chars);
description.insert(0, '^');
return this;
}
Expand Down
Loading
Loading