Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes #1986. String#slice on multibyte chars raise Exception
  • Loading branch information
enebo committed Sep 25, 2014
1 parent 87bf291 commit 01eebe6
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions core/src/main/java/org/jruby/util/StringSupport.java
Expand Up @@ -357,24 +357,12 @@ public static int preciseCodePoint(Encoding enc, byte[]bytes, int p, int end) {
}

@SuppressWarnings("deprecation")
public static int utf8Nth(byte[]bytes, int p, int end, int n) {
if (UNSAFE != null) {
if (n > LONG_SIZE * 2) {
int ep = ~LOWBITS & (p + LOWBITS);
while (p < ep) {
if ((bytes[p++] & 0xc0 /*utf8 lead byte*/) != 0x80) n--;
}
Unsafe us = (Unsafe)UNSAFE;
int eend = ~LOWBITS & end;
do {
n -= countUtf8LeadBytes(us.getLong(bytes, (long)(OFFSET + p)));
p += LONG_SIZE;
} while (p < eend && n >= LONG_SIZE);
}
}
while (p < end) {
public static int utf8Nth(byte[]bytes, int p, int e, int nth) {
// FIXME: Missing our UNSAFE impl because it was doing the wrong thing: See GH #1986
while (p < e) {
if ((bytes[p] & 0xc0 /*utf8 lead byte*/) != 0x80) {
if (n-- == 0) break;
if (nth == 0) break;
nth--;
}
p++;
}
Expand Down

0 comments on commit 01eebe6

Please sign in to comment.