blob: 14d7de2ffb3077bf02f4baeb4ec604bc8d9474b9 [file] [log] [blame]
Ian Rogersb0fa5dc2014-04-28 16:47:08 -07001/*
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
17#ifndef ART_RUNTIME_MIRROR_STRING_INL_H_
18#define ART_RUNTIME_MIRROR_STRING_INL_H_
19
20#include "array.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "class.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "intern_table.h"
23#include "runtime.h"
24#include "string.h"
25#include "thread.h"
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070026#include "utf.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027
28namespace art {
29namespace mirror {
30
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031inline uint32_t String::ClassSize() {
32 uint32_t vtable_entries = Object::kVTableLength + 51;
Fred Shih37f05ef2014-07-16 18:38:08 -070033 return Class::ComputeClassSize(true, vtable_entries, 0, 1, 0, 1, 2);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034}
35
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070036inline CharArray* String::GetCharArray() {
37 return GetFieldObject<CharArray>(ValueOffset());
38}
39
40inline int32_t String::GetLength() {
41 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_));
42 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
43 return result;
44}
45
46inline void String::SetArray(CharArray* new_array) {
47 // Array is invariant so use non-transactional mode. Also disable check as we may run inside
48 // a transaction.
49 DCHECK(new_array != NULL);
50 SetFieldObject<false, false>(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array);
51}
52
53inline String* String::Intern() {
54 return Runtime::Current()->GetInternTable()->InternWeak(this);
55}
56
57inline uint16_t String::CharAt(int32_t index) {
58 // TODO: do we need this? Equals is the only caller, and could
59 // bounds check itself.
60 DCHECK_GE(count_, 0); // ensures the unsigned comparison is safe.
61 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(count_))) {
62 Thread* self = Thread::Current();
63 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
64 self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
65 "length=%i; index=%i", count_, index);
66 return 0;
67 }
68 return GetCharArray()->Get(index + GetOffset());
69}
70
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070071inline int32_t String::GetHashCode() {
72 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
73 if (UNLIKELY(result == 0)) {
74 result = ComputeHashCode();
75 }
76 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
77 << ToModifiedUtf8() << " " << result;
78 return result;
79}
80
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070081} // namespace mirror
82} // namespace art
83
84#endif // ART_RUNTIME_MIRROR_STRING_INL_H_