blob: 6736497593904a872baf5ee347f1f3cbe7373450 [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"
26
27namespace art {
28namespace mirror {
29
Mingyao Yang98d1cc82014-05-15 17:02:16 -070030inline uint32_t String::ClassSize() {
31 uint32_t vtable_entries = Object::kVTableLength + 51;
32 return Class::ComputeClassSize(true, vtable_entries, 1, 1, 2);
33}
34
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070035inline CharArray* String::GetCharArray() {
36 return GetFieldObject<CharArray>(ValueOffset());
37}
38
39inline int32_t String::GetLength() {
40 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_));
41 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
42 return result;
43}
44
45inline void String::SetArray(CharArray* new_array) {
46 // Array is invariant so use non-transactional mode. Also disable check as we may run inside
47 // a transaction.
48 DCHECK(new_array != NULL);
49 SetFieldObject<false, false>(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array);
50}
51
52inline String* String::Intern() {
53 return Runtime::Current()->GetInternTable()->InternWeak(this);
54}
55
56inline uint16_t String::CharAt(int32_t index) {
57 // TODO: do we need this? Equals is the only caller, and could
58 // bounds check itself.
59 DCHECK_GE(count_, 0); // ensures the unsigned comparison is safe.
60 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(count_))) {
61 Thread* self = Thread::Current();
62 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
63 self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
64 "length=%i; index=%i", count_, index);
65 return 0;
66 }
67 return GetCharArray()->Get(index + GetOffset());
68}
69
70} // namespace mirror
71} // namespace art
72
73#endif // ART_RUNTIME_MIRROR_STRING_INL_H_