Skip to content

Commit

Permalink
Escape substitutions into closing regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
danlucraft committed Apr 2, 2010
1 parent ea33408 commit 191ce05
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/com/redcareditor/mate/Parser.java
Expand Up @@ -642,13 +642,9 @@ public void handleCaptures(int lineIx, int length, String line,
}

public Rx makeClosingRegex(String line, Scope scope, Marker m) {
// System.out.printf("make_closing_regex\n");
// new_end = pattern.end.gsub(/\\([0-9]+)/) do
// md.captures.send(:[], $1.to_i-1)
// end
if (m.pattern instanceof DoublePattern && !m.isCloseScope) {
DoublePattern dp = (DoublePattern) m.pattern;
//stdout.printf("making closing regex: %s (%d)\n", dp.end_string, (int) dp.end_string.length);
// System.out.printf("making closing regex: %s\n", dp.endString);
Rx rx = Rx.createRx("\\\\(\\d+)");
Match match;
int pos = 0;
Expand All @@ -661,7 +657,7 @@ public Rx makeClosingRegex(String line, Scope scope, Marker m) {
int num = Integer.parseInt(numstr);
// System.out.printf("capture found: %d\n", num);
String capstr = line.substring(m.match.getCapture(num).start, m.match.getCapture(num).end);
src.append(capstr);
src.append(Rx.escape(capstr));
pos = match.getCapture(1).end;
}
if (found)
Expand Down
2 changes: 1 addition & 1 deletion src/com/redcareditor/mate/Scanner.java
Expand Up @@ -98,7 +98,7 @@ public Marker findNextMarker() {
// }
// assert(cachedMarkers.size() == 0);
Rx closingRegex = currentScope.closingRegex;
if (closingRegex != null) {
if (closingRegex != null && closingRegex.usable()) {
//logger.info(String.format("closing regex: '%s'", closingRegex.pattern));
Match match = closingRegex.search(this.line, this.position, this.lineLength);
if (match != null &&
Expand Down
71 changes: 70 additions & 1 deletion src/com/redcareditor/onig/Rx.java
Expand Up @@ -2,15 +2,19 @@

import java.io.UnsupportedEncodingException;

import org.jcodings.Encoding;
import org.jcodings.specific.UTF8Encoding;
import org.jcodings.specific.UTF16BEEncoding;
import org.jcodings.specific.UTF16BEEncoding;
import org.joni.Matcher;
import org.joni.Option;
import org.joni.Regex;
import org.joni.Region;
import org.joni.Syntax;
import org.joni.WarnCallback;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

/**
* wrapper class around the Joni Regex library which is a optimized port of
* Onigurama
Expand Down Expand Up @@ -45,6 +49,10 @@ private Rx(String pattern) {
}
}

public boolean usable() {
return (regex != null);
}

public Match search(String target, int start, int end) {
byte[] bytes;
try {
Expand Down Expand Up @@ -95,4 +103,65 @@ private Regex compileRegex(String pattern) {
public String toString() {
return pattern;
}


public static String escape(String aRegexFragment){
final StringBuilder result = new StringBuilder();

final StringCharacterIterator iterator = new StringCharacterIterator(aRegexFragment);
char character = iterator.current();
while (character != CharacterIterator.DONE ){
if (character == '.') {
result.append("\\.");
}
else if (character == '\\') {
result.append("\\\\");
}
else if (character == '?') {
result.append("\\?");
}
else if (character == '*') {
result.append("\\*");
}
else if (character == '+') {
result.append("\\+");
}
else if (character == '&') {
result.append("\\&");
}
else if (character == ':') {
result.append("\\:");
}
else if (character == '{') {
result.append("\\{");
}
else if (character == '}') {
result.append("\\}");
}
else if (character == '[') {
result.append("\\[");
}
else if (character == ']') {
result.append("\\]");
}
else if (character == '(') {
result.append("\\(");
}
else if (character == ')') {
result.append("\\)");
}
else if (character == '^') {
result.append("\\^");
}
else if (character == '$') {
result.append("\\$");
}
else {
result.append(character);
}
character = iterator.next();
}
return result.toString();
}

}

0 comments on commit 191ce05

Please sign in to comment.