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: fc0fe19c377f
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a7c6602fa479
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Mar 13, 2015

  1. Copy the full SHA
    e9a219f View commit details
  2. 3
    Copy the full SHA
    a7c6602 View commit details
24 changes: 2 additions & 22 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -4148,39 +4148,19 @@ public IRubyObject chop_bang(ThreadContext context) {
public IRubyObject chop19(ThreadContext context) {
Ruby runtime = context.runtime;
if (value.getRealSize() == 0) return newEmptyString(runtime, getMetaClass(), value.getEncoding()).infectBy(this);
return makeShared19(runtime, 0, choppedLength19(runtime));
return makeShared19(runtime, 0, StringSupport.choppedLength19(this, runtime));
}

@JRubyMethod(name = "chop!")
public IRubyObject chop_bang19(ThreadContext context) {
modifyCheck();
Ruby runtime = context.runtime;
if (value.getRealSize() == 0) return runtime.getNil();
view(0, choppedLength19(runtime));
view(0, StringSupport.choppedLength19(this, runtime));
if (getCodeRange() != CR_7BIT) clearCodeRange();
return this;
}

private int choppedLength19(Ruby runtime) {
int p = value.getBegin();
int end = p + value.getRealSize();

if (p > end) return 0;
byte bytes[] = value.getUnsafeBytes();
Encoding enc = value.getEncoding();

int s = enc.prevCharHead(bytes, p, end, end);
if (s == -1) return 0;
if (s > p && codePoint(runtime, enc, bytes, s, end) == '\n') {
int s2 = enc.prevCharHead(bytes, p, s, end);
if (s2 != -1 && codePoint(runtime, enc, bytes, s2, end) == '\r') s = s2;
}
return s - p;
}

/** rb_str_chop
*
*/
public RubyString chomp(ThreadContext context) {
return chomp19(context);
}
21 changes: 21 additions & 0 deletions core/src/main/java/org/jruby/util/StringSupport.java
Original file line number Diff line number Diff line change
@@ -1324,4 +1324,25 @@ public static CodeRangeable delete_bangCommon19(CodeRangeable rubyString, Ruby r

return modify ? rubyString : null;
}

/**
* rb_str_chop
*/
public static int choppedLength19(CodeRangeable rubyString, Ruby runtime) {
final ByteList value = rubyString.getByteList();
int p = value.getBegin();
int end = p + value.getRealSize();

if (p > end) return 0;
byte bytes[] = value.getUnsafeBytes();
Encoding enc = value.getEncoding();

int s = enc.prevCharHead(bytes, p, end, end);
if (s == -1) return 0;
if (s > p && codePoint(runtime, enc, bytes, s, end) == '\n') {
int s2 = enc.prevCharHead(bytes, p, s, end);
if (s2 != -1 && codePoint(runtime, enc, bytes, s2, end) == '\r') s = s2;
}
return s - p;
}
}
24 changes: 0 additions & 24 deletions spec/truffle/tags/core/string/chop_tags.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
fails:String#chop removes the final character
fails:String#chop removes the final carriage return
fails:String#chop removes the final newline
fails:String#chop removes the final carriage return, newline
fails:String#chop removes the carrige return, newline if they are the only characters
fails:String#chop does not remove more than the final carriage return, newline
fails:String#chop removes a multi-byte character
fails:String#chop removes the final carriage return, newline from a multibyte String
fails:String#chop removes the final carriage return, newline from a non-ASCII String
fails:String#chop returns an empty string when applied to an empty string
fails:String#chop returns a new string when applied to an empty string
fails:String#chop taints result when self is tainted
fails:String#chop untrusts result when self is untrusted
fails:String#chop returns subclass instances when called on a subclass
fails:String#chop! removes the final character
fails:String#chop! removes the final carriage return
fails:String#chop! removes the final newline
fails:String#chop! removes the final carriage return, newline
fails:String#chop! removes the carrige return, newline if they are the only characters
fails:String#chop! does not remove more than the final carriage return, newline
fails:String#chop! removes a multi-byte character
fails:String#chop! removes the final carriage return, newline from a multibyte String
fails:String#chop! removes the final carriage return, newline from a non-ASCII String
fails:String#chop! returns self if modifications were made
fails:String#chop! returns nil when called on an empty string
fails:String#chop! raises a RuntimeError on a frozen instance that is modified
fails:String#chop! raises a RuntimeError on a frozen instance that would not be modified
Original file line number Diff line number Diff line change
@@ -696,6 +696,42 @@ public RubyString chompBangWithString(VirtualFrame frame, RubyString string, Obj
}
}

@CoreMethod(names = "chop!", raiseIfFrozenSelf = true)
public abstract static class ChopBangNode extends CoreMethodNode {

public ChopBangNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ChopBangNode(ChopBangNode prev) {
super(prev);
}

@Specialization
public Object chopBang(RubyString string) {
notDesignedForCompilation();

if (string.length() == 0) {
return getContext().getCoreLibrary().getNilObject();
}

final int newLength = choppedLength(string);

string.getByteList().view(0, newLength);

if (string.getCodeRange() != StringSupport.CR_7BIT) {
string.clearCodeRange();
}

return string;
}

@TruffleBoundary
private int choppedLength(RubyString string) {
return StringSupport.choppedLength19(string, getContext().getRuntime());
}
}

@CoreMethod(names = "count", argumentsAsArray = true)
public abstract static class CountNode extends CoreMethodNode {

5 changes: 5 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/string.rb
Original file line number Diff line number Diff line change
@@ -67,6 +67,11 @@ def chomp(separator=$/)
str.chomp!(separator) || str
end

def chop
str = dup
str.chop! || str
end

def delete(*strings)
str = dup
str.delete!(*strings) || str