package jdk.nashorn.internal.runtime.regexp.joni;
import jdk.nashorn.internal.runtime.regexp.joni.constants.AnchorType;
import jdk.nashorn.internal.runtime.regexp.joni.constants.RegexState;
import jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages;
import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
@SuppressWarnings("javadoc")
public final class Regex implements RegexState {
int[] code;
int codeLength;
boolean stackNeeded;
Object[] operands;
int operandLength;
int numMem;
int numRepeat;
int numNullCheck;
int captureHistory;
int btMemStart;
int btMemEnd;
int stackPopLevel;
int[] repeatRangeLo;
int[] repeatRangeHi;
WarnCallback warnings;
MatcherFactory factory;
protected Analyser analyser;
int options;
final int caseFoldFlag;
SearchAlgorithm searchAlgorithm;
int thresholdLength;
int anchor;
int anchorDmin;
int anchorDmax;
int subAnchor;
char[] exact;
int exactP;
int exactEnd;
byte[] map;
int[] intMap;
int[] intMapBackward;
int dMin;
int dMax;
char[][] templates;
int templateNum;
public Regex(final CharSequence cs) {
this(cs.toString());
}
public Regex(final String str) {
this(str.toCharArray(), 0, str.length(), 0);
}
public Regex(final char[] chars) {
this(chars, 0, chars.length, 0);
}
public Regex(final char[] chars, final int p, final int end) {
this(chars, p, end, 0);
}
public Regex(final char[] chars, final int p, final int end, final int option) {
this(chars, p, end, option, Syntax.RUBY, WarnCallback.DEFAULT);
}
public Regex(final char[] chars, final int p, final int end, final int option, final Syntax syntax) {
this(chars, p, end, option, Config.ENC_CASE_FOLD_DEFAULT, syntax, WarnCallback.DEFAULT);
}
public Regex(final char[]chars, final int p, final int end, final int option, final WarnCallback warnings) {
this(chars, p, end, option, Syntax.RUBY, warnings);
}
public Regex(final char[] chars, final int p, final int end, final int option, final Syntax syntax, final WarnCallback warnings) {
this(chars, p, end, option, Config.ENC_CASE_FOLD_DEFAULT, syntax, warnings);
}
public Regex(final char[] chars, final int p, final int end, final int optionp, final int caseFoldFlag, final Syntax syntax, final WarnCallback warnings) {
int option = optionp;
if ((option & (Option.DONT_CAPTURE_GROUP | Option.CAPTURE_GROUP)) ==
(Option.DONT_CAPTURE_GROUP | Option.CAPTURE_GROUP)) {
throw new ValueException(ErrorMessages.ERR_INVALID_COMBINATION_OF_OPTIONS);
}
if ((option & Option.NEGATE_SINGLELINE) != 0) {
option |= syntax.options;
option &= ~Option.SINGLELINE;
} else {
option |= syntax.options;
}
this.options = option;
this.caseFoldFlag = caseFoldFlag;
this.warnings = warnings;
new Analyser(new ScanEnvironment(this, syntax), chars, p, end).compile();
this.warnings = null;
}
public Matcher matcher(final char[] chars) {
return matcher(chars, 0, chars.length);
}
public Matcher matcher(final char[] chars, final int p, final int end) {
return factory.create(this, chars, p, end);
}
public WarnCallback getWarnings() {
return warnings;
}
public int numberOfCaptures() {
return numMem;
}
void setupBMSkipMap() {
final char[] chars = exact;
final int p = exactP;
final int end = exactEnd;
final int len = end - p;
if (len < Config.CHAR_TABLE_SIZE) {
if (map == null) {
map = new byte[Config.CHAR_TABLE_SIZE];
}
for (int i=0; i<Config.CHAR_TABLE_SIZE; i++) {
map[i] = (byte)len;
}
for (int i=0; i<len-1; i++)
{
map[chars[p + i] & 0xff] = (byte)(len - 1 -i);
}
} else {
if (intMap == null) {
intMap = new int[Config.CHAR_TABLE_SIZE];
}
for (int i=0; i<len-1; i++)
{
intMap[chars[p + i] & 0xff] = len - 1 - i;
}
}
}
void setExactInfo(final OptExactInfo e) {
if (e.length == 0) {
return;
}
exact = e.chars;
exactP = 0;
exactEnd = e.length;
if (e.ignoreCase) {
searchAlgorithm = new SearchAlgorithm.SLOW_IC(this);
} else {
if (e.length >= 2) {
setupBMSkipMap();
searchAlgorithm = SearchAlgorithm.BM;
} else {
searchAlgorithm = SearchAlgorithm.SLOW;
}
}
dMin = e.mmd.min;
dMax = e.mmd.max;
if (dMin != MinMaxLen.INFINITE_DISTANCE) {
thresholdLength = dMin + (exactEnd - exactP);
}
}
void setOptimizeMapInfo(final OptMapInfo m) {
map = m.map;
searchAlgorithm = SearchAlgorithm.MAP;
dMin = m.mmd.min;
dMax = m.mmd.max;
if (dMin != MinMaxLen.INFINITE_DISTANCE) {
thresholdLength = dMin + 1;
}
}
void setSubAnchor(final OptAnchorInfo anc) {
subAnchor |= anc.leftAnchor & AnchorType.BEGIN_LINE;
subAnchor |= anc.rightAnchor & AnchorType.END_LINE;
}
void clearOptimizeInfo() {
searchAlgorithm = SearchAlgorithm.NONE;
anchor = 0;
anchorDmax = 0;
anchorDmin = 0;
subAnchor = 0;
exact = null;
exactP = exactEnd = 0;
}
public String optimizeInfoToString() {
final StringBuilder s = new StringBuilder();
s.append("optimize: ").append(searchAlgorithm.getName()).append("\n");
s.append(" anchor: ").append(OptAnchorInfo.anchorToString(anchor));
if ((anchor & AnchorType.END_BUF_MASK) != 0) {
s.append(MinMaxLen.distanceRangeToString(anchorDmin, anchorDmax));
}
s.append("\n");
if (searchAlgorithm != SearchAlgorithm.NONE) {
s.append(" sub anchor: ").append(OptAnchorInfo.anchorToString(subAnchor)).append("\n");
}
s.append("dmin: ").append(dMin).append(" dmax: ").append(dMax).append("\n");
s.append("threshold length: ").append(thresholdLength).append("\n");
if (exact != null) {
s.append("exact: [").append(exact, exactP, exactEnd - exactP).append("]: length: ").append(exactEnd - exactP).append("\n");
} else if (searchAlgorithm == SearchAlgorithm.MAP) {
int n=0;
for (int i=0; i<Config.CHAR_TABLE_SIZE; i++) {
if (map[i] != 0) {
n++;
}
}
s.append("map: n = ").append(n).append("\n");
if (n > 0) {
int c=0;
s.append("[");
for (int i=0; i<Config.CHAR_TABLE_SIZE; i++) {
if (map[i] != 0) {
if (c > 0) {
s.append(", ");
}
c++;
s.append((char)i);
}
}
s.append("]\n");
}
}
return s.toString();
}
public int getOptions() {
return options;
}
public String dumpTree() {
return analyser == null ? null : analyser.root.toString();
}
public String dumpByteCode() {
return new ByteCodePrinter(this).byteCodeListToString();
}
}