diff options
| author | 2012-04-06 17:23:54 -0700 | |
|---|---|---|
| committer | 2012-04-06 17:23:54 -0700 | |
| commit | 529bfef6b3152fcf244e806ea6822ee96ff3eb4f (patch) | |
| tree | 2810f8b25c102dd5748ab3f750d662153ac3b446 /src | |
| parent | f3388e6ef460384a87c85bff72c961ebcd11ce0a (diff) | |
Fix the behavior of java.lang.String.fastIndexOf when start > length().
Change-Id: I738cb98384b6c134b9a2dc1c25906f24724500df
Diffstat (limited to 'src')
| -rw-r--r-- | src/java_lang_String.cc | 40 |
1 files changed, 14 insertions, 26 deletions
diff --git a/src/java_lang_String.cc b/src/java_lang_String.cc index 0cde0f2e4d..17dbbe9447 100644 --- a/src/java_lang_String.cc +++ b/src/java_lang_String.cc @@ -70,37 +70,25 @@ static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) { return countDiff; } -/* - * public int indexOf(int c, int start) - * - * Scan forward through the string for a matching character. - * The character must be <= 0xffff; this method does not handle supplementary - * characters. - * - * Determine the index of the first character matching "ch". The string - * to search is described by "chars", "offset", and "count". - * - * The character must be <= 0xffff. Supplementary characters are handled in - * Java. - * - * The "start" parameter must be clamped to [0..count]. - * - * Returns -1 if no match is found. - */ -static jint String_fastIndexOf(JNIEnv* env, jobject javaThis, jint ch, jint start) { - String* s = Decode<String*>(env, javaThis); - const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset(); +static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) { + // This method does not handle supplementary characters. They're dealt with in managed code. + DCHECK_LE(ch, 0xffff); + String* s = Decode<String*>(env, java_this); + + jint count = s->GetLength(); if (start < 0) { start = 0; + } else if (start > count) { + start = count; } - /* 16-bit loop, slightly better on ARM */ - const uint16_t* ptr = chars + start; - const uint16_t* endPtr = chars + s->GetLength(); - while (ptr < endPtr) { - if (*ptr++ == ch) { - return (ptr-1) - chars; + const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset(); + const uint16_t* p = chars + start; + const uint16_t* end = chars + count; + while (p < end) { + if (*p++ == ch) { + return (p - 1) - chars; } } |