Skip to content

Commit

Permalink
US-ASCII (newUsAsciiString) specialized string construction routines …
Browse files Browse the repository at this point in the history
…since they're used quite a bit in 1.9.

git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@8233 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
lopex committed Dec 1, 2008
1 parent 52faf19 commit cbd3bb5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/org/jruby/RubyEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ public static IRubyObject name_list(ThreadContext context, IRubyObject recv) {
while (i.hasNext()) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)i.next());
result.append(RubyString.newStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
result.append(RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
}
i = service.getAliases().entryIterator();
while (i.hasNext()) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)i.next());
result.append(RubyString.newStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
result.append(RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
}
return result;
}
Expand All @@ -196,8 +196,8 @@ public static IRubyObject aliases(ThreadContext context, IRubyObject recv) {
while (i.hasNext()) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)i.next());
IRubyObject alias = RubyString.newStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context);
IRubyObject name = RubyString.newStringShared(runtime,
IRubyObject alias = RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context);
IRubyObject name = RubyString.newUsAsciiStringShared(runtime,
((RubyEncoding)list[e.value.getIndex()]).name).freeze(context);
result.fastASet(alias, name);
}
Expand Down Expand Up @@ -230,20 +230,19 @@ public static IRubyObject _load(ThreadContext context, IRubyObject recv, IRubyOb
@JRubyMethod(name = {"to_s", "name"})
public IRubyObject to_s(ThreadContext context) {
// TODO: rb_usascii_str_new2
return RubyString.newStringShared(context.getRuntime(), name);
return RubyString.newUsAsciiStringShared(context.getRuntime(), name);
}

@JRubyMethod(name = "inspect")
public IRubyObject inspect(ThreadContext context) {
// TODO: rb_usascii_str_new2
ByteList bytes = new ByteList();
bytes.append("#<Encoding:".getBytes());
bytes.append(name);
if (isDummy) bytes.append(" (dummy)".getBytes());
bytes.append('>');
return RubyString.newString(context.getRuntime(), bytes);
return RubyString.newUsAsciiStringNoCopy(context.getRuntime(), bytes);
}

@SuppressWarnings("unchecked")
@JRubyMethod(name = "names")
public IRubyObject names(ThreadContext context) {
Expand All @@ -258,15 +257,15 @@ public IRubyObject names(ThreadContext context) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)i.next());
if (e.value == entry) {
result.append(RubyString.newStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
result.append(RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
}
}
i = service.getAliases().entryIterator();
while (i.hasNext()) {
CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)i.next());
if (e.value == entry) {
result.append(RubyString.newStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
result.append(RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
}
}
return result;
Expand Down
36 changes: 36 additions & 0 deletions src/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.jcodings.Encoding;
import org.jcodings.EncodingDB.Entry;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
Expand Down Expand Up @@ -289,6 +290,17 @@ public RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, boolean obj
assert value != null;
this.value = value;
}

protected RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, Encoding enc, int cr) {
this(runtime, rubyClass, value);
value.encoding = enc;
flags |= cr;
}

protected RubyString(Ruby runtime, RubyClass rubyClass, ByteList value, Encoding enc) {
this(runtime, rubyClass, value);
value.encoding = enc;
}

// Deprecated String construction routines
/** Create a new String which uses the same Ruby runtime and the same
Expand Down Expand Up @@ -434,6 +446,30 @@ public static RubyString newEmptyString(Ruby runtime, Encoding enc) {
return newEmptyString(runtime, runtime.getString(), enc);
}

public static RubyString newStringNoCopy(Ruby runtime, RubyClass clazz, ByteList bytes, Encoding enc, int cr) {
return new RubyString(runtime, clazz, bytes, enc, cr);
}

public static RubyString newStringNoCopy(Ruby runtime, ByteList bytes, Encoding enc, int cr) {
return newStringNoCopy(runtime, runtime.getString(), bytes, enc, cr);
}

public static RubyString newUsAsciiStringNoCopy(Ruby runtime, ByteList bytes) {
return newStringNoCopy(runtime, bytes, USASCIIEncoding.INSTANCE, CR_7BIT);
}

public static RubyString newUsAsciiStringShared(Ruby runtime, ByteList bytes) {
RubyString str = newStringNoCopy(runtime, bytes, USASCIIEncoding.INSTANCE, CR_7BIT);
str.shareLevel = SHARE_LEVEL_BYTELIST;
return str;
}

public static RubyString newUsAsciiStringShared(Ruby runtime, byte[] bytes, int start, int length) {
byte[] copy = new byte[length];
System.arraycopy(bytes, start, copy, 0, length);
return newUsAsciiStringShared(runtime, new ByteList(copy, false));
}

@Override
public int getNativeTypeIndex() {
return ClassIndex.STRING;
Expand Down

0 comments on commit cbd3bb5

Please sign in to comment.