Skip to content

Commit

Permalink
Merge pull request jruby#6562 from kares/java-string-split
Browse files Browse the repository at this point in the history
[feat] support RubyString#split for Java (exts)
  • Loading branch information
kares authored Mar 3, 2021
2 parents c4ae884 + ba13384 commit 6199049
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyRational.java
Original file line number Diff line number Diff line change
Expand Up @@ -1495,11 +1495,11 @@ static IRubyObject[] str_to_r_internal(final ThreadContext context, final RubySt
IRubyObject de = match.at(3);
IRubyObject re = match.post_match(context);

RubyArray a = nu.split(RubyRegexp.newDummyRegexp(runtime, Numeric.RationalPatterns.an_e_pat), context, false);
RubyArray a = nu.split(context, RubyRegexp.newDummyRegexp(runtime, Numeric.RationalPatterns.an_e_pat), false);
RubyString ifp = (RubyString)a.eltInternal(0);
IRubyObject exp = a.size() != 2 ? nil : a.eltInternal(1);

a = ifp.split(RubyRegexp.newDummyRegexp(runtime, Numeric.RationalPatterns.a_dot_pat), context, false);
a = ifp.split(context, RubyRegexp.newDummyRegexp(runtime, Numeric.RationalPatterns.a_dot_pat), false);
IRubyObject ip = a.eltInternal(0);
IRubyObject fp = a.size() != 2 ? nil : a.eltInternal(1);

Expand Down
49 changes: 48 additions & 1 deletion core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -4374,7 +4374,54 @@ public RubyArray split19(IRubyObject spat, ThreadContext context, boolean useBac
return splitCommon(context, spat, false, value.realSize(), 0, useBackref);
}

final RubyArray split(IRubyObject spat, ThreadContext context, boolean useBackref) {
/**
* Split for ext (Java) callers (does not write $~).
* @param delimiter
* @return splited entries
*/
public RubyArray split(RubyRegexp delimiter) {
return doSplit(delimiter, 0);
}

/**
* Split for ext (Java) callers (does not write $~).
* @param delimiter
* @param limit
* @return splited entries
*/
public RubyArray split(RubyRegexp delimiter, int limit) {
return doSplit(delimiter, limit);
}

/**
* Split for ext (Java) callers (does not write $~).
* @param delimiter
* @return splited entries
*/
public RubyArray split(RubyString delimiter) {
return doSplit(delimiter, 0);
}

/**
* Split for ext (Java) callers (does not write $~).
* @param delimiter
* @param limit
* @return splited entries
*/
public RubyArray split(RubyString delimiter, int limit) {
return doSplit(delimiter, limit);
}

private RubyArray doSplit(IRubyObject delimiter, final int limit) {
ThreadContext context = getRuntime().getCurrentContext();
if (limit == 1) {
Ruby runtime = context.runtime;
return isEmpty() ? runtime.newEmptyArray() : runtime.newArray(this.strDup(runtime));
}
return splitCommon(context, delimiter, limit > 0, limit, 1, false);
}

final RubyArray split(ThreadContext context, RubyRegexp spat, boolean useBackref) {
return splitCommon(context, spat, false, value.realSize(), 0, useBackref);
}

Expand Down
24 changes: 24 additions & 0 deletions core/src/test/java/org/jruby/test/TestRubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@

import org.jcodings.specific.UTF8Encoding;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.exceptions.RaiseException;
import org.jruby.util.ByteList;

/**
* Test case for functionality in RubyArray
Expand All @@ -56,4 +60,24 @@ public void testNewUnicodeString() throws Exception {
RubyString str = RubyString.newUnicodeString(runtime, "hello");
assertEquals(UTF8Encoding.INSTANCE, str.getByteList().getEncoding());
}

public void testSplit() throws RaiseException {
RubyString str = RubyString.newString(runtime, "JRuby is so awesome!");
RubyArray res = str.split(runtime.newString(" "));
assertEquals(4, res.size());
assertEquals("JRuby", res.get(0));
res = str.split(runtime.newString(" "), 2);
assertEquals(2, res.size());
assertEquals("JRuby", res.get(0));
assertEquals("is so awesome!", res.get(1));

RubyRegexp pat = RubyRegexp.newRegexp(runtime, ByteList.create("[ie]s"));
res = str.split(pat);
assertEquals(3, res.size());
assertEquals("JRuby ", res.get(0));
assertEquals(" so aw", res.get(1));
assertEquals("ome!", res.get(2));
res = str.split(pat, 4);
assertEquals(3, res.size());
}
}

0 comments on commit 6199049

Please sign in to comment.