1 /*
2 * Copyright (C) 2010 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 #define LOG_TAG "MatcherState"
18
19 #include "IcuUtilities.h"
20 #include "MatcherState.h"
21
22 #include <android-base/logging.h>
23
24 #include <nativehelper/ScopedPrimitiveArray.h>
25 #include <nativehelper/ScopedStringChars.h>
26
27
MatcherState(icu::RegexMatcher * matcher)28 MatcherState::MatcherState(icu::RegexMatcher* matcher) :
29 mMatcher(matcher),
30 mUChars(nullptr),
31 mUText(nullptr),
32 mStatus(U_ZERO_ERROR) {
33 }
34
updateInput(JNIEnv * env,jstring input)35 bool MatcherState::updateInput(JNIEnv* env, jstring input) {
36 // First, close the UText struct, since we're about to allocate a new one.
37 if (mUText != nullptr) {
38 utext_close(mUText);
39 mUText = nullptr;
40 }
41
42 // Then delete the UChar* associated with the UText struct..
43 mUChars.reset(nullptr);
44
45 // TODO: We should investigate whether we can avoid an additional copy
46 // in the native heap when is_copy == JNI_TRUE. The problem with doing
47 // that is that we might call ReleaseStringChars with a different
48 // JNIEnv* on a different downcall. This is currently safe as
49 // implemented in ART, but is unlikely to be portable and the spec stays
50 // silent on the matter.
51 ScopedStringChars inputChars(env, input);
52 if (inputChars.get() == nullptr) {
53 // There will be an exception pending if we get here.
54 return false;
55 }
56
57 // Make a copy of |input| on the native heap. This copy will be live
58 // until the next call to updateInput or close.
59 mUChars.reset(new (std::nothrow) UChar[inputChars.size()]);
60 if (mUChars.get() == nullptr) {
61 env->ThrowNew(env->FindClass("Ljava/lang/OutOfMemoryError;"), "Out of memory");
62 return false;
63 }
64
65 static_assert(sizeof(UChar) == sizeof(jchar), "sizeof(Uchar) != sizeof(jchar)");
66 memcpy(mUChars.get(), inputChars.get(), inputChars.size() * sizeof(jchar));
67
68 // Reset any errors that might have occurred on previous patches.
69 mStatus = U_ZERO_ERROR;
70 mUText = utext_openUChars(nullptr, mUChars.get(), inputChars.size(), &mStatus);
71 if (mUText == nullptr) {
72 CHECK(maybeThrowIcuException(env, "utext_openUChars", mStatus));
73 return false;
74 }
75
76 // It is an error for ICU to have returned a non-null mUText but to
77 // still have indicated an error.
78 CHECK(U_SUCCESS(mStatus));
79
80 mMatcher->reset(mUText);
81 return true;
82 }
83
~MatcherState()84 MatcherState::~MatcherState() {
85 if (mUText != nullptr) {
86 utext_close(mUText);
87 }
88 }
89
matcher()90 icu::RegexMatcher* MatcherState::matcher() {
91 return mMatcher.get();
92 }
93
status()94 UErrorCode& MatcherState::status() {
95 return mStatus;
96 }
97
updateOffsets(JNIEnv * env,jintArray javaOffsets)98 void MatcherState::updateOffsets(JNIEnv* env, jintArray javaOffsets) {
99 ScopedIntArrayRW offsets(env, javaOffsets);
100 if (offsets.get() == NULL) {
101 return;
102 }
103
104 for (size_t i = 0, groupCount = mMatcher->groupCount(); i <= groupCount; ++i) {
105 offsets[2*i + 0] = mMatcher->start(i, mStatus);
106 offsets[2*i + 1] = mMatcher->end(i, mStatus);
107 }
108 }
109