Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 16 | #ifndef ART_RUNTIME_MIRROR_STRING_INL_H_ |
| 17 | #define ART_RUNTIME_MIRROR_STRING_INL_H_ |
| 18 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "string.h" |
| 20 | |
| 21 | #include "android-base/stringprintf.h" |
| 22 | |
Andreas Gampe | fd63bbf | 2018-10-29 12:55:35 -0700 | [diff] [blame] | 23 | #include "class-inl.h" |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 24 | #include "common_throws.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 25 | #include "dex/utf.h" |
Andreas Gampe | 5a0430d | 2019-01-04 14:33:57 -0800 | [diff] [blame] | 26 | #include "runtime_globals.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace mirror { |
| 30 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 31 | inline uint32_t String::ClassSize(PointerSize pointer_size) { |
Alan Leung | 8f514ee | 2017-12-08 14:08:25 -0800 | [diff] [blame] | 32 | #ifdef USE_D8_DESUGAR |
| 33 | // Two lambdas in CharSequence: |
| 34 | // lambda$chars$0$CharSequence |
| 35 | // lambda$codePoints$1$CharSequence |
| 36 | // which were virtual functions in standalone desugar, becomes |
| 37 | // direct functions with D8 desugaring. |
| 38 | uint32_t vtable_entries = Object::kVTableLength + 54; |
| 39 | #else |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 40 | uint32_t vtable_entries = Object::kVTableLength + 56; |
Alan Leung | 8f514ee | 2017-12-08 14:08:25 -0800 | [diff] [blame] | 41 | #endif |
Narayan Kamath | 5d8fa8b | 2016-04-13 14:17:44 +0100 | [diff] [blame] | 42 | return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 1, 2, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 45 | inline uint16_t String::CharAt(int32_t index) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 46 | int32_t count = GetLength(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 47 | if (UNLIKELY((index < 0) || (index >= count))) { |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 48 | ThrowStringIndexOutOfBoundsException(index, count); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 49 | return 0; |
| 50 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 51 | if (IsCompressed()) { |
| 52 | return GetValueCompressed()[index]; |
| 53 | } else { |
| 54 | return GetValue()[index]; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | template <typename MemoryType> |
| 59 | int32_t String::FastIndexOf(MemoryType* chars, int32_t ch, int32_t start) { |
| 60 | const MemoryType* p = chars + start; |
| 61 | const MemoryType* end = chars + GetLength(); |
| 62 | while (p < end) { |
| 63 | if (*p++ == ch) { |
| 64 | return (p - 1) - chars; |
| 65 | } |
| 66 | } |
| 67 | return -1; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 70 | inline int32_t String::GetHashCode() { |
| 71 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); |
| 72 | if (UNLIKELY(result == 0)) { |
| 73 | result = ComputeHashCode(); |
| 74 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 75 | if (kIsDebugBuild) { |
| 76 | if (IsCompressed()) { |
| 77 | DCHECK(result != 0 || ComputeUtf16Hash(GetValueCompressed(), GetLength()) == 0) |
| 78 | << ToModifiedUtf8() << " " << result; |
| 79 | } else { |
| 80 | DCHECK(result != 0 || ComputeUtf16Hash(GetValue(), GetLength()) == 0) |
| 81 | << ToModifiedUtf8() << " " << result; |
| 82 | } |
| 83 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 84 | return result; |
| 85 | } |
| 86 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 87 | template<typename MemoryType> |
Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 88 | inline bool String::AllASCII(const MemoryType* chars, const int length) { |
Vladimir Marko | 16850ae | 2016-12-09 14:01:02 +0000 | [diff] [blame] | 89 | static_assert(std::is_unsigned<MemoryType>::value, "Expecting unsigned MemoryType"); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 90 | for (int i = 0; i < length; ++i) { |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 91 | if (!IsASCII(chars[i])) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
Vladimir Marko | e39f14f | 2017-02-10 15:44:25 +0000 | [diff] [blame] | 98 | inline bool String::DexFileStringAllASCII(const char* chars, const int length) { |
| 99 | // For strings from the dex file we just need to check that |
| 100 | // the terminating character is at the right position. |
| 101 | DCHECK_EQ(AllASCII(reinterpret_cast<const uint8_t*>(chars), length), chars[length] == 0); |
| 102 | return chars[length] == 0; |
| 103 | } |
| 104 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 105 | } // namespace mirror |
| 106 | } // namespace art |
| 107 | |
| 108 | #endif // ART_RUNTIME_MIRROR_STRING_INL_H_ |