Skip to content

Commit

Permalink
Moved RubyString#squeeze_bang helper methods to StringSupport.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Apr 2, 2015
1 parent 4bd7fc1 commit 16c2b33
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 58 deletions.
81 changes: 23 additions & 58 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -1120,7 +1120,7 @@ public IRubyObject op_plus19(ThreadContext context, IRubyObject _str) {
RubyString str = _str.convertToString();
Encoding enc = checkEncoding(str);
RubyString resultStr = newStringNoCopy(context.runtime, StringSupport.addByteLists(value, str.value),
enc, CodeRangeSupport.codeRangeAnd(getCodeRange(), str.getCodeRange()));
enc, CodeRangeSupport.codeRangeAnd(getCodeRange(), str.getCodeRange()));
resultStr.infectBy(flags | str.flags);
return resultStr;
}
Expand Down Expand Up @@ -3303,7 +3303,7 @@ public IRubyObject to_i19() {
@JRubyMethod(name = "to_i")
public IRubyObject to_i19(IRubyObject arg0) {
long base = checkBase(arg0);
return stringToInum19((int)base, false);
return stringToInum19((int) base, false);
}

private long checkBase(IRubyObject arg0) {
Expand Down Expand Up @@ -4650,26 +4650,6 @@ public IRubyObject squeeze_bang(ThreadContext context, IRubyObject[] args) {
return squeeze_bang19(context, args);
}

private IRubyObject squeezeCommon(Ruby runtime, boolean squeeze[]) {
int s = value.getBegin();
int t = s;
int send = s + value.getRealSize();
byte[]bytes = value.getUnsafeBytes();
int save = -1;

while (s < send) {
int c = bytes[s++] & 0xff;
if (c != save || !squeeze[c]) bytes[t++] = (byte)(save = c);
}

if (t - value.getBegin() != value.getRealSize()) { // modified
value.setRealSize(t - value.getBegin());
return this;
}

return runtime.getNil();
}

@JRubyMethod(name = "squeeze")
public IRubyObject squeeze19(ThreadContext context) {
RubyString str = strDup(context.runtime);
Expand Down Expand Up @@ -4703,10 +4683,16 @@ public IRubyObject squeeze_bang19(ThreadContext context) {

modifyAndKeepCodeRange();
if (singleByteOptimizable()) {
return squeezeCommon(runtime, squeeze); // 1.8
if (StringSupport.squeezeCommonSingleByte(value, squeeze) == null) {
return runtime.getNil();
}
} else {
return squeezeCommon19(runtime, squeeze, null, value.getEncoding(), false);
if (StringSupport.squeezeCommonMultiByte(runtime, value, squeeze, null, value.getEncoding(), false) == null) {
return runtime.getNil();
}
}

return this;
}

@JRubyMethod(name = "squeeze!")
Expand All @@ -4719,11 +4705,16 @@ public IRubyObject squeeze_bang19(ThreadContext context, IRubyObject arg) {

modifyAndKeepCodeRange();
if (singleByteOptimizable() && otherStr.singleByteOptimizable()) {
return squeezeCommon(runtime, squeeze); // 1.8
if(StringSupport.squeezeCommonSingleByte(value, squeeze) == null) {
return runtime.getNil();
}
} else {
return squeezeCommon19(runtime, squeeze, tables, value.getEncoding(), true);
if (StringSupport.squeezeCommonMultiByte(runtime, value, squeeze, tables, value.getEncoding(), true) == null) {
return runtime.getNil();
}
}

return this;
}

@JRubyMethod(name = "squeeze!", rest = true)
Expand All @@ -4749,42 +4740,16 @@ public IRubyObject squeeze_bang19(ThreadContext context, IRubyObject[] args) {

modifyAndKeepCodeRange();
if (singlebyte) {
return squeezeCommon(runtime, squeeze); // 1.8
if (StringSupport.squeezeCommonSingleByte(value, squeeze) == null) {
return runtime.getNil();
}
} else {
return squeezeCommon19(runtime, squeeze, tables, enc, true);
}
}

private IRubyObject squeezeCommon19(Ruby runtime, boolean squeeze[], StringSupport.TrTables tables, Encoding enc, boolean isArg) {
int s = value.getBegin();
int t = s;
int send = s + value.getRealSize();
byte[]bytes = value.getUnsafeBytes();
int save = -1;
int c;

while (s < send) {
if (enc.isAsciiCompatible() && (c = bytes[s] & 0xff) < 0x80) {
if (c != save || (isArg && !squeeze[c])) bytes[t++] = (byte)(save = c);
s++;
} else {
c = codePoint(runtime, enc, bytes, s, send);
int cl = codeLength(enc, c);
if (c != save || (isArg && !StringSupport.trFind(c, squeeze, tables))) {
if (t != s) enc.codeToMbc(c, bytes, t);
save = c;
t += cl;
}
s += cl;
if (StringSupport.squeezeCommonMultiByte(runtime, value, squeeze, tables, enc, true) == null) {
return runtime.getNil();
}
}

if (t - value.getBegin() != value.getRealSize()) { // modified
value.setRealSize(t - value.getBegin());
return this;
}

return runtime.getNil();
return this;
}

/** rb_str_tr / rb_str_tr_bang
Expand Down
52 changes: 52 additions & 0 deletions core/src/main/java/org/jruby/util/StringSupport.java
Expand Up @@ -1995,6 +1995,58 @@ public static int multiByteCasecmp(Encoding enc, ByteList value, ByteList otherV
return end - p > oend - op ? 1 : -1;
}

public static ByteList squeezeCommonSingleByte(ByteList value, boolean squeeze[]) {
int s = value.getBegin();
int t = s;
int send = s + value.getRealSize();
byte[]bytes = value.getUnsafeBytes();
int save = -1;

while (s < send) {
int c = bytes[s++] & 0xff;
if (c != save || !squeeze[c]) bytes[t++] = (byte)(save = c);
}

if (t - value.getBegin() != value.getRealSize()) { // modified
value.setRealSize(t - value.getBegin());
return value;
}

return null;
}

public static ByteList squeezeCommonMultiByte(Ruby runtime, ByteList value, boolean squeeze[], TrTables tables, Encoding enc, boolean isArg) {
int s = value.getBegin();
int t = s;
int send = s + value.getRealSize();
byte[]bytes = value.getUnsafeBytes();
int save = -1;
int c;

while (s < send) {
if (enc.isAsciiCompatible() && (c = bytes[s] & 0xff) < 0x80) {
if (c != save || (isArg && !squeeze[c])) bytes[t++] = (byte)(save = c);
s++;
} else {
c = codePoint(runtime, enc, bytes, s, send);
int cl = codeLength(enc, c);
if (c != save || (isArg && !trFind(c, squeeze, tables))) {
if (t != s) enc.codeToMbc(c, bytes, t);
save = c;
t += cl;
}
s += cl;
}
}

if (t - value.getBegin() != value.getRealSize()) { // modified
value.setRealSize(t - value.getBegin());
return value;
}

return null;
}

private static int rb_memsearch_ss(byte[] xsBytes, int xs, int m, byte[] ysBytes, int ys, int n) {
int y;

Expand Down

0 comments on commit 16c2b33

Please sign in to comment.