Skip to content

Commit

Permalink
Lot's of String stuff, notably String#split (and other methods: scan,…
Browse files Browse the repository at this point in the history
… sub, gsub, tr, ljust rjust, center... and more).

Also partial work targeting JRUBY-688


git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@3650 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
lopex committed May 14, 2007
1 parent cd470f2 commit 28e2096
Show file tree
Hide file tree
Showing 4 changed files with 995 additions and 662 deletions.
4 changes: 2 additions & 2 deletions src/org/jruby/RubyMatchData.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public IRubyObject offset(RubyFixnum index) {
public abstract IRubyObject to_s();
public abstract IRubyObject doClone();

public static class JavaString extends RubyMatchData {
public static final class JavaString extends RubyMatchData {
private String original;
public JavaString(Ruby runtime, String original, Matcher matcher) {
super(runtime, matcher);
Expand Down Expand Up @@ -321,7 +321,7 @@ public IRubyObject doClone() {
}
}

public static class RString extends RubyMatchData {
public static final class RString extends RubyMatchData {
private RubyString original;
private int len;
private int[] start;
Expand Down
148 changes: 92 additions & 56 deletions src/org/jruby/RubyRegexp.java
Original file line number Diff line number Diff line change
Expand Up @@ -571,66 +571,102 @@ public IRubyObject match(String target, RubyString rtarget, int startPos) {
return getRuntime().getNil();
}

public void regsub(RubyString str, RubyMatchData match, ByteList sb) {
ByteList repl = str.getByteList();
int pos = 0;
int end = repl.length();
char c;
IRubyObject ins;
while (pos < end) {
c = (char)(repl.get(pos++) & 0xFF);
if (c == '\\' && pos < end) {
c = (char)(repl.get(pos++) & 0xFF);
switch (c) {
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
ins = match.group(c - '0');
break;
case '&' :
ins = match.group(0);
break;
case '`' :
ins = match.pre_match();
break;
case '\'' :
ins = match.post_match();
break;
case '+' :
ins = match_last(match);
break;
case '\\' :
sb.append(c);
continue;
default :
sb.append('\\');
sb.append(c);
continue;
}
if (!ins.isNil()) {
sb.append(((RubyString) ins).getByteList());
}
/** rb_reg_regsub
*
*/
public RubyString regsub(IRubyObject str, RubyString src, RubyMatchData match) {
ByteList val = null;

ByteList strList = str.convertToString().getByteList();
byte[]strBytes = strList.unsafeBytes();
int p, s;
p = s = strList.begin;
int e = s + strList.realSize;

while (s < e) {
int ss = s;
char c = (char)(strBytes[s++] & 0xFF);
if (c != '\\' || s == e) continue;

if (val == null) {
val = new ByteList(ss - p);
val.append(strBytes, p, ss - p);
} else {
sb.append(c);
val.append(strBytes, p, ss - p);
}

c = (char)(strBytes[s++] & 0xFF);
p = s;

int no;
Matcher mat = match.matcher;

ByteList srcList;
byte[]srcBytes;

switch (c) {
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
no = c - '0';
break;
case '&' :
no = 0;
break;
case '`' :
srcList = src.getByteList();
srcBytes = srcList.unsafeBytes();
val.append(srcBytes, srcList.begin, mat.start(0));
continue;
case '\'':
srcList = src.getByteList();
srcBytes = srcList.unsafeBytes();
val.append(srcBytes, srcList.begin + mat.end(0), srcList.length() - mat.end(0));
continue;

case '+' :
no = mat.groupCount() - 1;
while (!mat.isCaptured(no) && no > 0) no--;
if (no == 0) continue;
break;

case '\\':
val.append(strBytes, s - 1, 1);
continue;

default:
val.append(strBytes, s - 2, 2);
continue;
}

if (no >= 0) {
if (no >= mat.groupCount()) continue;
if (!mat.isCaptured(no)) continue;
srcList = src.getByteList();
srcBytes = srcList.unsafeBytes();
val.append(srcBytes, srcList.begin + mat.start(no), mat.end(no) - mat.start(no));
}

}
}

/** rb_reg_regsub
*
*/
public IRubyObject regsub(IRubyObject str, RubyMatchData match) {
RubyString str2 = str.asString();
ByteList sb = new ByteList(str2.getByteList().length()+30);
regsub(str2,match,sb);
return RubyString.newString(getRuntime(),sb);
if (p < e) {
if (val == null) {
val = new ByteList(e - p);
val.append(strBytes, p, e - p);
} else {
val.append(strBytes, p, e - p);
}
}

if (val == null) return (RubyString)str;
return RubyString.newString(getRuntime(), val);
}

/** rb_reg_init_copy
Expand Down
Loading

0 comments on commit 28e2096

Please sign in to comment.