Skip to content

Commit

Permalink
conditional String/StringBuffer patch (Charlie)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@2055 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
enebo committed Jun 6, 2006
1 parent 1691419 commit 185c54e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/org/jruby/IRuby.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public interface IRuby {
public IRubyObject eval(Node node);

public RubyClass getObject();

public RubyClass getString();

/** Returns the "true" instance from the instance pool.
* @return The "true" instance.
Expand Down
8 changes: 7 additions & 1 deletion src/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public final class Ruby implements IRuby {
private RubyBoolean trueObject;
private RubyBoolean falseObject;
private RubyClass objectClass;
private StringMetaClass stringClass;
private RubyClass systemCallError = null;
private RubyModule errnoModule = null;
private IRubyObject topSelf;
Expand Down Expand Up @@ -217,6 +218,10 @@ public IRubyObject eval(Node node) {
public RubyClass getObject() {
return objectClass;
}

public RubyClass getString() {
return stringClass;
}

/** Returns the "true" instance from the instance pool.
* @return The "true" instance.
Expand Down Expand Up @@ -450,7 +455,8 @@ private void initCoreClasses() {
RubyBoolean.createTrueClass(this);
RubyComparable.createComparable(this);
defineModule("Enumerable"); // Impl: src/builtin/enumerable.rb
new StringMetaClass(this).initializeClass();
stringClass = new StringMetaClass(this);
stringClass.initializeClass();
new SymbolMetaClass(this).initializeClass();
RubyThreadGroup.createThreadGroupClass(this);
RubyThread.createThreadClass(this);
Expand Down
81 changes: 46 additions & 35 deletions src/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class RubyString extends RubyObject {
private static final String encoding = "iso8859-1";

private StringBuffer value;
private CharSequence chars;

public static abstract class StringMethod extends DirectInvocationMethod {
public StringMethod(RubyModule implementationClass, Arity arity, Visibility visibility) {
Expand All @@ -73,24 +74,25 @@ public IRubyObject internalCall(IRuby runtime, IRubyObject receiver, RubyModule
};

// @see IRuby.newString(...)
private RubyString(IRuby runtime, String value) {
this(runtime, runtime.getClass("String"), value);
private RubyString(IRuby runtime, CharSequence value) {
this(runtime, runtime.getString(), value);
}

private RubyString(IRuby runtime, RubyClass rubyClass, String value) {
private RubyString(IRuby runtime, RubyClass rubyClass, CharSequence value) {
super(runtime, rubyClass);

assert value != null;

this.setValue(new StringBuffer(value));
// defer creation of StringBuffer until needed
chars = value;
}

public Class getJavaClass() {
return String.class;
}

public String toString() {
return value.toString();
return getValue().toString();
}

public static String bytesToString(byte[] bytes) {
Expand Down Expand Up @@ -182,7 +184,7 @@ public RubySymbol to_sym() {
* This method should be used to satisfy RCR #38.
*
*/
public RubyString newString(String s) {
public RubyString newString(CharSequence s) {
return new RubyString(getRuntime(), getType(), s);
}

Expand Down Expand Up @@ -216,14 +218,9 @@ public IRubyObject rbClone() {
newObject.setFrozen(isFrozen());
return newObject;
}

private RubyString cat(StringBuffer str) {
getValue().append(str);
return this;
}

public RubyString cat(String str) {
getValue().append(str);
public RubyString cat(CharSequence str) {
getMutableValue().append(str);
return this;
}

Expand All @@ -248,7 +245,7 @@ public RubyString reverse() {
}

public RubyString reverse_bang() {
getValue().reverse();
getMutableValue().reverse();
return this;
}

Expand Down Expand Up @@ -391,7 +388,7 @@ public RubyString swapcase() {
*
*/
public IRubyObject swapcase_bang() {
StringBuffer string = getValue();
StringBuffer string = getMutableValue();
int length = string.length();
boolean changesMade = false;

Expand Down Expand Up @@ -431,7 +428,7 @@ public IRubyObject insert(IRubyObject indexArg, IRubyObject stringArg) {

String insert = stringArg.convertToString().toString();

getValue().insert(index, insert);
getMutableValue().insert(index, insert);

return this;
}
Expand Down Expand Up @@ -675,7 +672,7 @@ private IRubyObject index(IRubyObject[] args, boolean reverse) {
}
} else if (args[0] instanceof RubyString) {
String sub = ((RubyString) args[0]).toString();
pos = reverse ? getValue().lastIndexOf(sub, pos) : getValue().indexOf(sub, pos);
pos = reverse ? getMutableValue().lastIndexOf(sub, pos) : getMutableValue().indexOf(sub, pos);
} else if (args[0] instanceof RubyFixnum) {
char c = (char) ((RubyFixnum) args[0]).getLongValue();
pos = reverse ? toString().lastIndexOf(c, pos) : toString().indexOf(c, pos);
Expand All @@ -699,7 +696,7 @@ private IRubyObject substr(int beg, int len) {
}
}
int end = Math.min(length, beg + len);
return newString(getValue().substring(beg, end)).infectBy(this);
return newString(getMutableValue().substring(beg, end)).infectBy(this);
}

/* rb_str_replace */
Expand All @@ -708,8 +705,8 @@ private IRubyObject replace(int beg, int len, RubyString replaceWith) {
len = getValue().length() - beg;
}

getValue().delete(beg, beg + len);
getValue().insert(beg, replaceWith.toString());
getMutableValue().delete(beg, beg + len);
getMutableValue().insert(beg, replaceWith.toString());

return infectBy(replaceWith);
}
Expand Down Expand Up @@ -783,7 +780,7 @@ public IRubyObject aset(IRubyObject[] args) {
throw getRuntime().newIndexError("string index out of bounds");
}
if (args[1] instanceof RubyFixnum) {
getValue().setCharAt(idx, (char) RubyNumeric.fix2int(args[1]));
getMutableValue().setCharAt(idx, (char) RubyNumeric.fix2int(args[1]));
} else {
replace(idx, 1, stringValue(args[1]));
}
Expand Down Expand Up @@ -837,7 +834,7 @@ public IRubyObject succ_bang() {
return this;
}

StringBuffer sbuf = getValue();
StringBuffer sbuf = getMutableValue();
boolean alnumSeen = false;
int pos = -1;
char c = 0;
Expand Down Expand Up @@ -916,7 +913,7 @@ public RubyBoolean include(IRubyObject obj) {
return getRuntime().newBoolean(toString().indexOf(c) != -1);
}
String str = stringValue(obj).toString();
return getRuntime().newBoolean(getValue().indexOf(str) != -1);
return getRuntime().newBoolean(getMutableValue().indexOf(str) != -1);
}

/** rb_str_to_i
Expand Down Expand Up @@ -1073,7 +1070,7 @@ public IRubyObject chop_bang() {
}
}

getValue().delete(end, getValue().length());
getMutableValue().delete(end, getValue().length());
return this;
}

Expand Down Expand Up @@ -1126,7 +1123,7 @@ public IRubyObject chomp_bang(IRubyObject[] args) {
return getRuntime().getNil();
}

getValue().delete(end - removeCount + 1, getValue().length());
getMutableValue().delete(end - removeCount + 1, getValue().length());
return this;
}

Expand All @@ -1143,13 +1140,13 @@ public IRubyObject chomp_bang(IRubyObject[] args) {
return getRuntime().getNil();
}

getValue().delete(end - removeCount + 1, getValue().length());
getMutableValue().delete(end - removeCount + 1, getValue().length());
return this;
}

// Uncommon case of str.chomp!("xxx")
if (toString().endsWith(separator)) {
getValue().delete(getValue().length() - separator.length(), getValue().length());
getMutableValue().delete(getValue().length() - separator.length(), getValue().length());
return this;
}
return getRuntime().getNil();
Expand All @@ -1165,7 +1162,7 @@ public IRubyObject lstrip() {
}
}

return newString(getValue().substring(i));
return newString(getValue().subSequence(i, getValue().length()));
}

public IRubyObject lstrip_bang() {
Expand All @@ -1188,7 +1185,7 @@ public IRubyObject rstrip() {
}
}

return newString(getValue().substring(0, i + 1));
return newString(getValue().subSequence(0, i + 1));
}

public IRubyObject rstrip_bang() {
Expand Down Expand Up @@ -1345,7 +1342,7 @@ private StringBuffer getSqueeze(IRubyObject[] args) {

int strLen = getValue().length();
if (strLen <= 1) {
return getValue();
return getMutableValue();
}
StringBuffer sbuf = new StringBuffer(strLen);
char c1 = getValue().charAt(0);
Expand Down Expand Up @@ -1548,16 +1545,30 @@ public RubyArray unpack(IRubyObject obj) {
*
* @param value The new java.lang.String this RubyString should encapsulate
*/
public void setValue(StringBuffer value) {
this.value = value;
}
public void setValue(CharSequence value) {
this.chars = value;
this.value = null;
}

/**
* Mutator for internal string representation.
*
* @param value The new java.lang.String this RubyString should encapsulate
*/
public void setMutableValue(StringBuffer value) {
this.chars = this.value = value;
}

/**
* Accessor for internal string representation.
*
* @return The java.lang.String this RubyString encapsulates.
*/
public StringBuffer getValue() {
return value;
public StringBuffer getMutableValue() {
return value != null ? value : (StringBuffer)(chars = value = new StringBuffer(chars.toString()));
}

public CharSequence getValue() {
return chars;
}
}
4 changes: 4 additions & 0 deletions test/org/jruby/test/BaseMockRuby.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public IRubyObject eval(Node node) {
public RubyClass getObject() {
throw new MockException();
}

public RubyClass getString() {
throw new MockException();
}

public RubyBoolean getTrue() {
throw new MockException();
Expand Down
4 changes: 4 additions & 0 deletions test/org/jruby/test/MockInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public RubyClass getClass(String name) {
public RubyClass getObject() {
return null;
}

public RubyClass getString() {
return null;
}

public JavaSupport getJavaSupport() {
return new MockJavaSupport(this);
Expand Down

0 comments on commit 185c54e

Please sign in to comment.