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