Skip to content

Commit

Permalink
[Truffle] - Makes sure to always keep the string encoding after call:…
Browse files Browse the repository at this point in the history
… chomp, upcase, downcase, capitalize.

Extracts the string operations to a class StringNodesHelper so we can
reuse it in the methods with !
  • Loading branch information
lucasallan committed Dec 23, 2014
1 parent 25fdaa3 commit 226c52d
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions core/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
Expand Up @@ -327,20 +327,13 @@ public ChompNode(ChompNode prev) {
@Specialization
public RubyString chomp(RubyString string, UndefinedPlaceholder undefined) {
notDesignedForCompilation();
return string.getContext().makeString(string.toString().trim());
return string.getContext().makeString(StringNodesHelper.chomp(string));
}

@Specialization
public RubyString chompWithString(RubyString string, RubyString stringToChomp) {
notDesignedForCompilation();

String tempString = string.toString();

if (tempString.endsWith(stringToChomp.toString())) {
tempString = tempString.substring(0, tempString.length() - stringToChomp.toString().length());
}

return getContext().makeString(tempString);
return getContext().makeString(StringNodesHelper.chompWithString(string, stringToChomp));
}

}
Expand All @@ -360,7 +353,7 @@ public ChompBangNode(ChompBangNode prev) {
public RubyString chompBang(RubyString string) {
notDesignedForCompilation();

string.set(ByteList.create(string.toString().trim()));
string.set(StringNodesHelper.chomp(string));
return string;
}
}
Expand Down Expand Up @@ -440,8 +433,7 @@ public DowncaseNode(DowncaseNode prev) {
@Specialization
public RubyString downcase(RubyString string) {
notDesignedForCompilation();
ByteList newByteList = ByteList.create(string.toString().toLowerCase());
newByteList.setEncoding(string.getBytes().getEncoding());
ByteList newByteList = StringNodesHelper.downcase(string);

return string.getContext().makeString(newByteList);
}
Expand All @@ -462,8 +454,7 @@ public DowncaseBangNode(DowncaseBangNode prev) {
public RubyBasicObject downcase(RubyString string) {
notDesignedForCompilation();

ByteList newByteList = ByteList.create(string.toString().toLowerCase());
newByteList.setEncoding(string.getBytes().getEncoding());
ByteList newByteList = StringNodesHelper.downcase(string);

if (newByteList.equals(string.getBytes())) {
return getContext().getCoreLibrary().getNilObject();
Expand Down Expand Up @@ -1181,8 +1172,7 @@ public UpcaseNode(UpcaseNode prev) {
@Specialization
public RubyString upcase(RubyString string) {
notDesignedForCompilation();
ByteList byteListString = ByteList.create(string.toString().toUpperCase());
byteListString.setEncoding(string.getBytes().getEncoding());
final ByteList byteListString = StringNodesHelper.upcase(string);

return string.getContext().makeString(byteListString);
}
Expand All @@ -1203,10 +1193,9 @@ public UpcaseBangNode(UpcaseBangNode prev) {
@Specialization
public RubyString upcaseBang(RubyString string) {
notDesignedForCompilation();
ByteList byteListString = ByteList.create(string.toString().toUpperCase());
byteListString.setEncoding(string.getBytes().getEncoding());

final ByteList byteListString = StringNodesHelper.upcase(string);
string.set(byteListString);

return string;
}
}
Expand All @@ -1229,10 +1218,7 @@ public RubyString capitalizeBang(RubyString string) {
if (javaString.isEmpty()) {
return string;
} else {
String head = javaString.substring(0, 1).toUpperCase();
String tail = javaString.substring(1, javaString.length()).toLowerCase();
ByteList byteListString = ByteList.create(head + tail);
byteListString.setEncoding(string.getBytes().getEncoding());
final ByteList byteListString = StringNodesHelper.capitalize(string);

string.set(byteListString);
return string;
Expand All @@ -1255,14 +1241,11 @@ public CapitalizeNode(CapitalizeNode prev) {
public RubyString capitalize(RubyString string) {
notDesignedForCompilation();
String javaString = string.toString();

if (javaString.isEmpty()) {
return string;
} else {
String head = javaString.substring(0, 1).toUpperCase();
String tail = javaString.substring(1, javaString.length()).toLowerCase();
ByteList byteListString = ByteList.create(head + tail);
byteListString.setEncoding(string.getBytes().getEncoding());

final ByteList byteListString = StringNodesHelper.capitalize(string);
return string.getContext().makeString(byteListString);
}
}
Expand Down Expand Up @@ -1317,4 +1300,50 @@ public RubyString chr(RubyString string) {
}
}

static class StringNodesHelper {

public static ByteList capitalize(RubyString string) {
String javaString = string.toString();
String head = javaString.substring(0, 1).toUpperCase();
String tail = javaString.substring(1, javaString.length()).toLowerCase();
ByteList byteListString = ByteList.create(head + tail);
byteListString.setEncoding(string.getBytes().getEncoding());
return byteListString;
}

public static ByteList upcase(RubyString string) {
ByteList byteListString = ByteList.create(string.toString().toUpperCase());
byteListString.setEncoding(string.getBytes().getEncoding());
return byteListString;
}

public static ByteList downcase(RubyString string) {
ByteList newByteList = ByteList.create(string.toString().toLowerCase());
newByteList.setEncoding(string.getBytes().getEncoding());

return newByteList;
}

public static ByteList chomp(RubyString string) {
ByteList byteListString = ByteList.create(string.toString().trim());
byteListString.setEncoding(string.getBytes().getEncoding());

return byteListString;
}

public static ByteList chompWithString(RubyString string, RubyString stringToChomp) {

String tempString = string.toString();

if (tempString.endsWith(stringToChomp.toString())) {
tempString = tempString.substring(0, tempString.length() - stringToChomp.toString().length());
}

ByteList byteList = ByteList.create(tempString);
byteList.setEncoding(string.getBytes().getEncoding());

return byteList;
}
}

}

0 comments on commit 226c52d

Please sign in to comment.