java_lang_String.cc revision 0512f02dd6623c0870c11fbf3274d7462f732136
1bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes/*
2bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Copyright (C) 2008 The Android Open Source Project
3bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
4bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * you may not use this file except in compliance with the License.
6bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * You may obtain a copy of the License at
7bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
8bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
10bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Unless required by applicable law or agreed to in writing, software
11bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * See the License for the specific language governing permissions and
14bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * limitations under the License.
15bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes */
16bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
17bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#include "jni_internal.h"
18bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#include "object.h"
19bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
20bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
21bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
22bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#ifdef HAVE__MEMCMP16
23bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes// "count" is in 16-bit units.
24bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesextern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
25bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#define MemCmp16 __memcmp16
26bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#else
27bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesuint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
28bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  for (size_t i = 0; i < count; i++) {
29bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    if (s0[i] != s1[i]) {
30bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes      return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
31bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    }
32bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
33bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  return 0;
34bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}
35bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes#endif
36bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
37bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesnamespace art {
38bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
390512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
40b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom  ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
41bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  String* lhs = Decode<String*>(env, javaThis);
42bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  String* rhs = Decode<String*>(env, javaRhs);
43bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
44bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (rhs == NULL) {
45bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "rhs == null");
46bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    return -1;
47bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
48bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
49bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // Quick test for comparison of a string with itself.
50bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (lhs == rhs) {
51bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    return 0;
52bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
53bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
54bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // TODO: is this still true?
55bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
56bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // because the interpreter converts the characters to 32-bit integers
57bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // *without* sign extension before it subtracts them (which makes some
58bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // sense since "char" is unsigned).  So what we get is the result of
59bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
60bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  int lhsCount = lhs->GetLength();
61bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  int rhsCount = rhs->GetLength();
62bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  int countDiff = lhsCount - rhsCount;
63bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  int minCount = (countDiff < 0) ? lhsCount : rhsCount;
64bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
65bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
66bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
67bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (otherRes != 0) {
68bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    return otherRes;
69bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
70bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  return countDiff;
71bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}
72bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
730512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jboolean String_equals(JNIEnv* env, jobject javaThis, jobject javaRhs) {
74bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  String* lhs = Decode<String*>(env, javaThis);
75bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  String* rhs = Decode<String*>(env, javaRhs);
76bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
77bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // Quick test for comparison of a string with itself.
78bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (lhs == rhs) {
79bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    return JNI_TRUE;
80bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
81bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
82bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // if (!(rhs instanceof String)) return false.
83bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (rhs == NULL || lhs->GetClass() != rhs->GetClass()) {
84bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    return JNI_FALSE;
85bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
86bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
87bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // Quick length check.
88bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  int lhsCount = lhs->GetLength();
89bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  int rhsCount = rhs->GetLength();
90bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (lhsCount != rhsCount) {
91bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    return JNI_FALSE;
92bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
93bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
94bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // You may, at this point, be tempted to pull out the hashCode fields
95bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // and compare them.  If both fields have been initialized, and they
96bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // are not equal, we can return false immediately.
97bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  //
98bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // However, the hashCode field is often not set.  If it is set,
99bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // there's an excellent chance that the String is being used as a key
100bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // in a hashed data structure (e.g. HashMap).  That data structure has
101bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // already made the comparison and determined that the hashes are equal,
102bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // making a check here redundant.
103bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  //
104bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // It's not clear that checking the hashes will be a win in "typical"
105bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  // use cases.  We err on the side of simplicity and ignore them.
106bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
107bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
108bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
109bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  return (MemCmp16(lhsChars, rhsChars, lhsCount) == 0) ? JNI_TRUE : JNI_FALSE;
110bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}
111bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
112bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes/*
113bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * public int indexOf(int c, int start)
114bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
115bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Scan forward through the string for a matching character.
116bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * The character must be <= 0xffff; this method does not handle supplementary
117bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * characters.
118bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
119bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Determine the index of the first character matching "ch".  The string
120bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * to search is described by "chars", "offset", and "count".
121bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
122bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * The character must be <= 0xffff. Supplementary characters are handled in
123bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Java.
124bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
125bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * The "start" parameter must be clamped to [0..count].
126bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes *
127bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes * Returns -1 if no match is found.
128bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes */
1290512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jint String_fastIndexOf(JNIEnv* env, jobject javaThis, jint ch, jint start) {
130bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  String* s = Decode<String*>(env, javaThis);
131bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
132bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
133bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (start < 0) {
134bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    start = 0;
135bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
136bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
137bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  /* 16-bit loop, slightly better on ARM */
138bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  const uint16_t* ptr = chars + start;
139bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  const uint16_t* endPtr = chars + s->GetLength();
140bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  while (ptr < endPtr) {
141bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    if (*ptr++ == ch) {
142bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes      return (ptr-1) - chars;
143bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    }
144bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
145bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
146bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  return -1;
147bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}
148bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
1490512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jstring String_intern(JNIEnv* env, jobject javaThis) {
150c74255fffb035001304c9a058a2e730a5a1a9604Brian Carlstrom  String* s = Decode<String*>(env, javaThis);
151c74255fffb035001304c9a058a2e730a5a1a9604Brian Carlstrom  String* result = s->Intern();
152bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  return AddLocalReference<jstring>(env, result);
153bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}
154bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
1550512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic JNINativeMethod gMethods[] = {
156bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
157bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  NATIVE_METHOD(String, equals, "(Ljava/lang/Object;)Z"),
158bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  NATIVE_METHOD(String, fastIndexOf, "(II)I"),
159bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
160bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes};
161bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
162bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesvoid register_java_lang_String(JNIEnv* env) {
163bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  jniRegisterNativeMethods(env, "java/lang/String", gMethods, NELEM(gMethods));
164bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}
165bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
166bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}  // namespace art
167