Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_STRING_H_ |
| 18 | #define ART_RUNTIME_MIRROR_STRING_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "class.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | struct StringClassOffsets; |
| 26 | struct StringOffsets; |
| 27 | class StringPiece; |
| 28 | |
| 29 | namespace mirror { |
| 30 | |
| 31 | // C++ mirror of java.lang.String |
| 32 | class MANAGED String : public Object { |
| 33 | public: |
| 34 | static MemberOffset CountOffset() { |
| 35 | return OFFSET_OF_OBJECT_MEMBER(String, count_); |
| 36 | } |
| 37 | |
| 38 | static MemberOffset ValueOffset() { |
| 39 | return OFFSET_OF_OBJECT_MEMBER(String, array_); |
| 40 | } |
| 41 | |
| 42 | static MemberOffset OffsetOffset() { |
| 43 | return OFFSET_OF_OBJECT_MEMBER(String, offset_); |
| 44 | } |
| 45 | |
| 46 | const CharArray* GetCharArray() const; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 47 | CharArray* GetCharArray(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | |
| 49 | int32_t GetOffset() const { |
| 50 | int32_t result = GetField32(OffsetOffset(), false); |
| 51 | DCHECK_LE(0, result); |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | int32_t GetLength() const; |
| 56 | |
| 57 | int32_t GetHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 58 | |
| 59 | void ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 60 | |
| 61 | int32_t GetUtfLength() const; |
| 62 | |
| 63 | uint16_t CharAt(int32_t index) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 64 | |
| 65 | String* Intern() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 66 | |
| 67 | static String* AllocFromUtf16(Thread* self, |
| 68 | int32_t utf16_length, |
| 69 | const uint16_t* utf16_data_in, |
| 70 | int32_t hash_code = 0) |
| 71 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 72 | |
| 73 | static String* AllocFromModifiedUtf8(Thread* self, const char* utf) |
| 74 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 75 | |
| 76 | static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 77 | const char* utf8_data_in) |
| 78 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 79 | |
| 80 | static String* Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) |
| 81 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 82 | |
| 83 | static String* Alloc(Thread* self, Class* java_lang_String, CharArray* array) |
| 84 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 85 | |
| 86 | bool Equals(const char* modified_utf8) const |
| 87 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 88 | |
| 89 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 90 | bool Equals(const StringPiece& modified_utf8) const |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 92 | |
| 93 | bool Equals(const String* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 94 | |
| 95 | // Compare UTF-16 code point values not in a locale-sensitive manner |
| 96 | int Compare(int32_t utf16_length, const char* utf8_data_in); |
| 97 | |
| 98 | // TODO: do we need this overload? give it a more intention-revealing name. |
| 99 | bool Equals(const uint16_t* that_chars, int32_t that_offset, |
| 100 | int32_t that_length) const |
| 101 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 102 | |
| 103 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 104 | std::string ToModifiedUtf8() const; |
| 105 | |
| 106 | int32_t FastIndexOf(int32_t ch, int32_t start) const; |
| 107 | |
| 108 | int32_t CompareTo(String* other) const; |
| 109 | |
| 110 | static Class* GetJavaLangString() { |
| 111 | DCHECK(java_lang_String_ != NULL); |
| 112 | return java_lang_String_; |
| 113 | } |
| 114 | |
| 115 | static void SetClass(Class* java_lang_String); |
| 116 | static void ResetClass(); |
| 117 | |
| 118 | private: |
| 119 | void SetHashCode(int32_t new_hash_code) { |
| 120 | DCHECK_EQ(0u, |
| 121 | GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false)); |
| 122 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), |
| 123 | new_hash_code, false); |
| 124 | } |
| 125 | |
| 126 | void SetCount(int32_t new_count) { |
| 127 | DCHECK_LE(0, new_count); |
| 128 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false); |
| 129 | } |
| 130 | |
| 131 | void SetOffset(int32_t new_offset) { |
| 132 | DCHECK_LE(0, new_offset); |
| 133 | DCHECK_GE(GetLength(), new_offset); |
| 134 | SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false); |
| 135 | } |
| 136 | |
| 137 | void SetArray(CharArray* new_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 138 | |
| 139 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 140 | CharArray* array_; |
| 141 | |
| 142 | int32_t count_; |
| 143 | |
| 144 | uint32_t hash_code_; |
| 145 | |
| 146 | int32_t offset_; |
| 147 | |
| 148 | static Class* java_lang_String_; |
| 149 | |
| 150 | friend struct art::StringOffsets; // for verifying offset information |
| 151 | FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount |
| 152 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
| 153 | }; |
| 154 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 155 | class MANAGED StringClass : public Class { |
| 156 | private: |
| 157 | CharArray* ASCII_; |
| 158 | Object* CASE_INSENSITIVE_ORDER_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 159 | int64_t serialVersionUID_; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 160 | uint32_t REPLACEMENT_CHAR_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | friend struct art::StringClassOffsets; // for verifying offset information |
| 162 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass); |
| 163 | }; |
| 164 | |
| 165 | } // namespace mirror |
| 166 | } // namespace art |
| 167 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 168 | #endif // ART_RUNTIME_MIRROR_STRING_H_ |