Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Truffle] - Implements String#swapcase
  • Loading branch information
lucasallan committed Feb 9, 2015
1 parent 9cfbea3 commit 4a5a146
Showing 1 changed file with 32 additions and 0 deletions.
Expand Up @@ -1329,6 +1329,38 @@ public RubyString rjust(RubyString string, int length, RubyString padding) {

}

@CoreMethod(names = "swapcase")
public abstract static class SwapcaseNode extends CoreMethodNode {
public SwapcaseNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public SwapcaseNode(SwapcaseNode prev) {
super(prev);
}

@Specialization
public RubyString swapcase(RubyString string) {
notDesignedForCompilation();

char[] charArray = string.toString().toCharArray();
StringBuilder newString = new StringBuilder();

for (int i = 0; i < charArray.length; i++) {
char current = charArray[i];

if (Character.isLowerCase(current)) {
newString.append(Character.toString(current).toUpperCase());
} else if (Character.isUpperCase(current)){
newString.append(Character.toString(current).toLowerCase());
} else {
newString.append(current);
}
}
return getContext().makeString(newString.toString(), string.getByteList().getEncoding());
}
}

@CoreMethod(names = "rstrip")
public abstract static class RStripNode extends CoreMethodNode {

Expand Down

0 comments on commit 4a5a146

Please sign in to comment.