blob: 315f7b19515d8a13a3402cd6d0f39c375aec7642 [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"
21#include "intern_table.h"
22#include "runtime.h"
23#include "string.h"
24#include "thread.h"
25
26namespace art {
27namespace mirror {
28
29inline CharArray* String::GetCharArray() {
30 return GetFieldObject<CharArray>(ValueOffset());
31}
32
33inline int32_t String::GetLength() {
34 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_));
35 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
36 return result;
37}
38
39inline void String::SetArray(CharArray* new_array) {
40 // Array is invariant so use non-transactional mode. Also disable check as we may run inside
41 // a transaction.
42 DCHECK(new_array != NULL);
43 SetFieldObject<false, false>(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array);
44}
45
46inline String* String::Intern() {
47 return Runtime::Current()->GetInternTable()->InternWeak(this);
48}
49
50inline uint16_t String::CharAt(int32_t index) {
51 // TODO: do we need this? Equals is the only caller, and could
52 // bounds check itself.
53 DCHECK_GE(count_, 0); // ensures the unsigned comparison is safe.
54 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(count_))) {
55 Thread* self = Thread::Current();
56 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
57 self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
58 "length=%i; index=%i", count_, index);
59 return 0;
60 }
61 return GetCharArray()->Get(index + GetOffset());
62}
63
64} // namespace mirror
65} // namespace art
66
67#endif // ART_RUNTIME_MIRROR_STRING_INL_H_