Skip to content

Instantly share code, notes, and snippets.

@FROGGS
Created July 9, 2013 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FROGGS/5957075 to your computer and use it in GitHub Desktop.
Save FROGGS/5957075 to your computer and use it in GitHub Desktop.
Super Slow Support(tm) for 32-bit unicode codepoints in nqp::chars() and nqp::substr(). This increases rakudo's parse time from 57s to nearly ten minutes.
diff --git a/src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java b/src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
index c193007..e1d3418 100644
--- a/src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
+++ b/src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
@@ -2648,7 +2648,7 @@ public final class Ops {
/* String operations. */
public static long chars(String val) {
- return val.length();
+ return val.codePointCount(0, val.length());
}
public static String lc(String val) {
@@ -2656,7 +2656,7 @@ public final class Ops {
}
public static String uc(String val) {
- return val.toUpperCase();
+ return val.toUpperCase(); // maybe Character.toUpperCase(int codePoint) ? (but this might break ß/SS)
}
public static String x(String val, long count) {
@@ -2671,8 +2671,16 @@ public final class Ops {
return valA + valB;
}
+ /**
+ * Creates new String that contains just the given code point.
+ * Version that optimizes for BMP characters.
+ */
public static String chr(long val) {
- return (new StringBuffer()).append(Character.toChars((int)val)).toString();
+ if (Character.charCount((int)val) == 1) {
+ return String.valueOf((char) val);
+ } else {
+ return new String(Character.toChars((int)val));
+ }
}
public static String join(String delimiter, SixModelObject arr, ThreadContext tc) {
@@ -2751,11 +2759,13 @@ public final class Ops {
}
public static String substr2(String val, long offset) {
- if (offset >= val.length())
- return "";
- if (offset < 0)
- offset += val.length();
- return val.substring((int)offset);
+ int length = val.length();
+ if (offset >= length) return "";
+ if (offset == 0) return val;
+ length = val.codePointCount(0, val.length());
+ if (offset >= length) return "";
+ if (offset < 0) offset += length;
+ return val.substring( val.offsetByCodePoints(0, (int)offset) );
}
public static String substr3(String val, long offset, long length) {
@@ -2764,7 +2774,9 @@ public final class Ops {
int end = (int)(offset + length);
if (end > val.length())
end = val.length();
- return val.substring((int)offset, end);
+ offset = val.offsetByCodePoints(0, (int)offset);
+ end = val.offsetByCodePoints(0, end);
+ return val.substring(val.offsetByCodePoints(0, (int)offset), end);
}
public static long ordfirst(String str) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment