Skip to content

Commit

Permalink
Merge pull request #727 from tychobrailleur/string_concat
Browse files Browse the repository at this point in the history
Fix string concatenation with a number as per rubyspec behaviour.
  • Loading branch information
headius committed May 21, 2013
2 parents 097c752 + f4bd113 commit ccd06ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 0 additions & 2 deletions spec/tags/1.9/ruby/core/string/concat_tags.txt
@@ -1,3 +1 @@
fails:String#concat with Integer returns a ASCII-8BIT string if self is US-ASCII and the argument is between 128-255 (inclusive)
fails:String#concat with Integer raises RangeError if the argument is an invalid codepoint for self's encoding
fails:String#concat when self is ASCII-8BIT and argument is US-ASCII uses ASCII-8BIT encoding
21 changes: 18 additions & 3 deletions src/org/jruby/RubyString.java
Expand Up @@ -69,6 +69,7 @@
import org.jcodings.EncodingDB;
import org.jcodings.ascii.AsciiTables;
import org.jcodings.constants.CharacterType;
import org.jcodings.exception.EncodingException;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jcodings.specific.UTF8Encoding;
Expand Down Expand Up @@ -2638,9 +2639,23 @@ public RubyString concat19(ThreadContext context, IRubyObject other) {

private RubyString concatNumeric(Ruby runtime, int c) {
Encoding enc = value.getEncoding();
int cl = codeLength(runtime, enc, c);
modify19(value.getRealSize() + cl);
enc.codeToMbc(c, value.getUnsafeBytes(), value.getBegin() + value.getRealSize());
int cl;

try {
cl = codeLength(runtime, enc, c);
modify19(value.getRealSize() + cl);

if (enc == USASCIIEncoding.INSTANCE) {
if (c > 0xff) runtime.newRangeError(c + " out of char range");
if (c > 0x79) {
value.setEncoding(ASCIIEncoding.INSTANCE);
enc = value.getEncoding();
}
}
enc.codeToMbc(c, value.getUnsafeBytes(), value.getBegin() + value.getRealSize());
} catch (EncodingException e) {
throw runtime.newRangeError(c + " out of char range");
}
value.setRealSize(value.getRealSize() + cl);
return this;
}
Expand Down

0 comments on commit ccd06ac

Please sign in to comment.