Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fe237b97a9fd
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 60cb2266e014
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Jul 24, 2017

  1. Copy the full SHA
    ebbef48 View commit details
  2. Copy the full SHA
    60cb226 View commit details
Showing with 79 additions and 21 deletions.
  1. +16 −10 core/src/main/java/org/jruby/RubyString.java
  2. +63 −11 core/src/main/java/org/jruby/util/StringSupport.java
26 changes: 16 additions & 10 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -2907,10 +2907,9 @@ private IRubyObject rindexCommon(ThreadContext context, final IRubyObject sub, i
pos = StringSupport.offset(
value.getEncoding(), value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(),
pos, singleByteOptimizable());
if (regSub.length() > 0) {
pos = regSub.search(context, this, pos, true);
pos = subLength(pos);
}
pos = regSub.search(context, this, pos, true);
pos = subLength(pos);
if (pos >= 0) return RubyFixnum.newFixnum(context.runtime, pos);
} else if (sub instanceof RubyString) {
Encoding enc = checkEncoding((RubyString) sub);
pos = StringSupport.rindex(value,
@@ -5005,12 +5004,19 @@ public IRubyObject tr_s_bang19(ThreadContext context, IRubyObject src, IRubyObje
/** rb_str_each_line
*
*/
@JRubyMethod(name = "each_line")
public IRubyObject each_line(ThreadContext context, Block block) {
return each_lineCommon(context, context.runtime.getGlobalVariables().get("$/"), block);
return StringSupport.rbStrEnumerateLines(this, context, "each_line", context.runtime.getGlobalVariables().get("$/"), block, false);
}

@JRubyMethod(name = "each_line")
public IRubyObject each_line(ThreadContext context, IRubyObject arg, Block block) {
return each_lineCommon(context, arg, block);
return StringSupport.rbStrEnumerateLines(this, context, "each_line", arg, block, false);
}

@JRubyMethod(name = "each_line")
public IRubyObject each_line(ThreadContext context, IRubyObject arg, IRubyObject opts, Block block) {
return StringSupport.rbStrEnumerateLines(this, context, "each_line", arg, opts, block, false);
}

public IRubyObject each_lineCommon(ThreadContext context, IRubyObject sep, Block block) {
@@ -5061,14 +5067,14 @@ public IRubyObject each_lineCommon(ThreadContext context, IRubyObject sep, Block
return this;
}

@JRubyMethod(name = "each_line")
@Deprecated
public IRubyObject each_line19(ThreadContext context, Block block) {
return StringSupport.rbStrEnumerateLines(this, context, "each_line", context.runtime.getGlobalVariables().get("$/"), block, false);
return each_line(context, block);
}

@JRubyMethod(name = "each_line")
@Deprecated
public IRubyObject each_line19(ThreadContext context, IRubyObject arg, Block block) {
return StringSupport.rbStrEnumerateLines(this, context, "each_line", arg, block, false);
return each_line(context, arg, block);
}

public IRubyObject lines(ThreadContext context, Block block) {
74 changes: 63 additions & 11 deletions core/src/main/java/org/jruby/util/StringSupport.java
Original file line number Diff line number Diff line change
@@ -42,7 +42,9 @@
import org.jruby.RubyEncoding;
import org.jruby.RubyIO;
import org.jruby.RubyString;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.collections.IntHashMap;
@@ -1716,18 +1718,34 @@ public static boolean areComparableViaCodeRange(CodeRangeable string, CodeRangea
}

public static IRubyObject rbStrEnumerateLines(RubyString str, ThreadContext context, String name, IRubyObject arg, Block block, boolean wantarray) {
IRubyObject opts = ArgsUtil.getOptionsArg(context.runtime, arg);
if (opts.isNil()) {
return rbStrEnumerateLines(str, context, name, arg, context.nil, block, wantarray);
} else {
return rbStrEnumerateLines(str, context, name, context.runtime.getGlobalVariables().get("$/"), opts, block, wantarray);
}
}

public static IRubyObject rbStrEnumerateLines(RubyString str, ThreadContext context, String name, IRubyObject arg, IRubyObject opts, Block block, boolean wantarray) {
Ruby runtime = context.runtime;

Encoding enc;
IRubyObject line, rs, orig = str;
int ptr, pend, subptr, subend, rsptr, hit, adjusted;
int pos, len, rslen;
boolean paragraph_mode = false;
boolean rsnewline = false;
boolean chomp = false;

IRubyObject ary = null;

rs = arg;

if (!opts.isNil()) {
IRubyObject _chomp = ArgsUtil.extractKeywordArg(context, "chomp", opts);
chomp = _chomp != null || _chomp.isTrue();
}

if (block.isGiven()) {
if (wantarray) {
// this code should be live in 3.0
@@ -1744,7 +1762,7 @@ public static IRubyObject rbStrEnumerateLines(RubyString str, ThreadContext cont
if (wantarray) {
ary = runtime.newEmptyArray();
} else {
return enumeratorize(runtime, str, name, arg);
return enumeratorize(runtime, str, name, Helpers.arrayOf(arg, opts));
}
}

@@ -1767,29 +1785,35 @@ public static IRubyObject rbStrEnumerateLines(RubyString str, ThreadContext cont
rs = rs.convertToString();
rslen = ((RubyString)rs).size();

if (rs == context.runtime.getGlobalVariables().get("$/"))
IRubyObject defaultSep = context.runtime.getGlobalVariables().get("$/");
if (rs == defaultSep)
enc = str.getEncoding();
else
enc = str.checkEncoding((RubyString) rs);

byte[] rsbytes;
ByteList rsByteList = ((RubyString) rs).getByteList();
if (rslen == 0) {
rsbytes = RubyIO.PARAGRAPH_SEPARATOR.unsafeBytes();
rsptr = RubyIO.PARAGRAPH_SEPARATOR.begin();
rslen = 2;
paragraph_mode = true;
rsnewline = true;
} else {

rsbytes = ((RubyString)rs).getByteList().unsafeBytes();
rsptr = ((RubyString)rs).getByteList().begin();
rsbytes = rsByteList.unsafeBytes();
rsptr = rsByteList.begin();
if (rsByteList.length() == enc.minLength() &&
enc.isNewLine(rsbytes, rsptr, rsByteList.length())) {
rsnewline = true;
}
}

if ((rs == context.runtime.getGlobalVariables().get("$/") || paragraph_mode) && !enc.isAsciiCompatible()) {
if ((rs == defaultSep || paragraph_mode) && !enc.isAsciiCompatible()) {
rs = RubyString.newString(runtime, rsbytes, rsptr, rslen);
rs = EncodingUtils.rbStrEncode(context, rs, runtime.getEncodingService().convertEncodingToRubyEncoding(enc), 0, context.nil);
rsbytes = ((RubyString)rs).getByteList().unsafeBytes();
rsptr = ((RubyString)rs).getByteList().begin();
rslen = ((RubyString)rs).getByteList().realSize();
rsbytes = rsByteList.unsafeBytes();
rsptr = rsByteList.begin();
rslen = rsByteList.realSize();
}

while (subptr < pend) {
@@ -1803,21 +1827,38 @@ public static IRubyObject rbStrEnumerateLines(RubyString str, ThreadContext cont
}
subend = hit + rslen;
if (paragraph_mode) {
while (subend < pend && enc.isNewLine(strBytes, subend, pend)) {
int[] n = {0};
while (subend < pend) {
if (EncodingUtils.encAscget(strBytes, subend, pend, n, enc) != '\r') {
n[0] = 0;
}
if (!enc.isNewLine(strBytes, subend + n[0], pend)) break;
subend += n[0];
subend += enc.length(strBytes, subend, pend);
}
}
hit = subend;
if (chomp) {
if (rsnewline) {
subend = chomp_newline(strBytes, subptr, subend, enc);
} else {
subend -= rslen;
}
}
line = str.substr(runtime, subptr - ptr, subend - subptr);
if (wantarray) {
((RubyArray)ary).push(line);
} else {
block.yieldSpecific(context, line);
str.modifyCheck(strBytes, len);
}
subptr = subend;
subptr = hit;
}

if (subptr != pend) {
if (chomp && paragraph_mode) {
pend = chomp_newline(strBytes, subptr, pend, enc);
}
line = str.substr(runtime, subptr - ptr, pend - subptr);
if (wantarray) {
((RubyArray) ary).push(line);
@@ -1829,6 +1870,17 @@ public static IRubyObject rbStrEnumerateLines(RubyString str, ThreadContext cont
return wantarray ? ary : orig;
}

private static int chomp_newline(byte[] bytes, int p, int e, Encoding enc) {
int prev = enc.prevCharHead(bytes, p, e, e);
if (enc.isNewLine(bytes, prev, e)) {
e = prev;
prev = enc.prevCharHead(bytes, p, e, e);
if (prev != -1 && EncodingUtils.encAscget(bytes, prev, e, null, enc) == '\r')
e = prev;
}
return e;
}

public static int memsearch(byte[] xBytes, int x0, int m, byte[] yBytes, int y0, int n, Encoding enc) {
int x = x0, y = y0;