Skip to content

Commit

Permalink
dedup casemap calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lopex committed Apr 9, 2018
1 parent 408fc81 commit b0af815
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
45 changes: 15 additions & 30 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -1793,6 +1793,17 @@ public IRubyObject upcase_bang19(ThreadContext context) {
return upcase_bang19(context, RubyObject.NULL_ARRAY);
}

private int caseMap(Ruby runtime, int flags, Encoding enc) {
IntHolder flagsP = new IntHolder();
flagsP.value = flags;
if ((flags & Config.CASE_ASCII_ONLY) != 0) {
StringSupport.asciiOnlyCaseMap(runtime, value, flagsP, enc);
} else {
value = StringSupport.caseMap(runtime, value, flagsP, enc);
}
return flagsP.value;
}

@JRubyMethod(name = "upcase", rest = true)
public RubyString upcase19(ThreadContext context, IRubyObject[] args) {
RubyString str = strDup(context.runtime);
Expand Down Expand Up @@ -1820,13 +1831,8 @@ public IRubyObject upcase_bang19(ThreadContext context, IRubyObject[] args) {
}
s++;
}
} else if ((flags & Config.CASE_ASCII_ONLY) != 0) {
flags = StringSupport.asciiOnlyCaseMap(runtime, this, flags, enc);
} else {
IntHolder flagsP = new IntHolder();
flagsP.value = flags;
value = StringSupport.caseMap(runtime, value, flagsP);
flags = flagsP.value;
flags = caseMap(runtime, flags, enc);
}

return ((flags & Config.CASE_MODIFIED) != 0) ? this : context.nil;
Expand Down Expand Up @@ -1881,13 +1887,8 @@ public IRubyObject downcase_bang(ThreadContext context, IRubyObject[] args) {
}
s++;
}
} else if ((flags & Config.CASE_ASCII_ONLY) != 0) {
flags = StringSupport.asciiOnlyCaseMap(runtime, this, flags, enc);
} else {
IntHolder flagsP = new IntHolder();
flagsP.value = flags;
value = StringSupport.caseMap(runtime, value, flagsP);
flags = flagsP.value;
flags = caseMap(runtime, flags, enc);
}

return ((flags & Config.CASE_MODIFIED) != 0) ? this : context.nil;
Expand Down Expand Up @@ -1926,15 +1927,7 @@ public IRubyObject swapcase_bang19(ThreadContext context, IRubyObject[] args) {
modifyAndKeepCodeRange();
Encoding enc = checkDummyEncoding();

if ((flags & Config.CASE_ASCII_ONLY) != 0) {
StringSupport.asciiOnlyCaseMap(runtime, this, flags, enc);
} else {
IntHolder flagsP = new IntHolder();
flagsP.value = flags;
value = StringSupport.caseMap(runtime, value, flagsP);
flags = flagsP.value;
}

flags = caseMap(runtime, flags, enc);
return ((flags & Config.CASE_MODIFIED) != 0) ? this : context.nil;
}

Expand Down Expand Up @@ -1977,15 +1970,7 @@ public IRubyObject capitalize_bang19(ThreadContext context, IRubyObject[] args)

modifyAndKeepCodeRange();

if ((flags & Config.CASE_ASCII_ONLY) != 0) {
StringSupport.asciiOnlyCaseMap(runtime, this, flags, enc);
} else {
IntHolder flagsP = new IntHolder();
flagsP.value = flags;
value = StringSupport.caseMap(runtime, value, flagsP);
flags = flagsP.value;
}

flags = caseMap(runtime, flags, enc);
return ((flags & Config.CASE_MODIFIED) != 0) ? this : context.nil;
}

Expand Down
17 changes: 6 additions & 11 deletions core/src/main/java/org/jruby/util/StringSupport.java
Expand Up @@ -2449,23 +2449,22 @@ public static int checkCaseOptions(Ruby runtime, IRubyObject[]args, int flags) {

private static final class MappingBuffer {
MappingBuffer next;
byte[] bytes;
final byte[] bytes;
int used;

MappingBuffer() {
bytes = null;
}

MappingBuffer(int size) {
bytes = new byte[size];
}

}

private static final int CASE_MAPPING_ADDITIONAL_LENGTH = 20;

public static ByteList caseMap(Ruby runtime, ByteList src, IntHolder flags) {
public static ByteList caseMap(Ruby runtime, ByteList src, IntHolder flags, Encoding enc) {
IntHolder pp = new IntHolder();
Encoding enc = src.getEncoding();
pp.value = src.getBegin();
int end = src.getRealSize() + pp.value;
byte[]bytes = src.getUnsafeBytes();
Expand Down Expand Up @@ -2501,20 +2500,16 @@ public static ByteList caseMap(Ruby runtime, ByteList src, IntHolder flags) {
return tgt;
}

public static int asciiOnlyCaseMap(Ruby runtime, RubyString source, int flags, Encoding enc) {
ByteList value = source.getByteList();
if (value.getRealSize() == 0) return flags;
public static void asciiOnlyCaseMap(Ruby runtime, ByteList value, IntHolder flags, Encoding enc) {
if (value.getRealSize() == 0) return;
int s = value.getBegin();
int end = s + value.getRealSize();
byte[]bytes = value.getUnsafeBytes();

IntHolder flagsP = new IntHolder();
flagsP.value = flags;
IntHolder pp = new IntHolder();
pp.value = s;
int len = ASCIIEncoding.INSTANCE.caseMap(flagsP, bytes, pp, end, bytes, s, end);
int len = ASCIIEncoding.INSTANCE.caseMap(flags, bytes, pp, end, bytes, s, end);
if (len < 0) throw runtime.newArgumentError("input string invalid");
return flagsP.value;
}

public static int encCoderangeClean(int cr) {
Expand Down

0 comments on commit b0af815

Please sign in to comment.