• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <nativehelper/JNIHelp.h>
20 #include <nativehelper/jni_macros.h>
21 
22 #include "unicode/parseerr.h"
23 #include "unicode/regex.h"
24 
25 /**
26  * Encapsulates an instance of ICU4C's RegexMatcher class along with a copy of
27  * the input it's currently operating on in the native heap.
28  *
29  * Rationale: We choose to make a copy here because it turns out to be a lot
30  * cheaper when a moving GC and/or string compression is enabled. This is
31  * because env->GetStringChars() always copies in this scenario. This becomes
32  * especially bad when the String in question is long and/or contains a large
33  * number of matches.
34  *
35  * Drawbacks: The native allocation associated with this class is no longer
36  * fixed size, but NativeAllocationRegistry should be able to determine
37  * the native heap size by mallinfo().
38  */
39 class MatcherState {
40 public:
41     MatcherState(icu::RegexMatcher* matcher);
42     ~MatcherState();
43 
44     bool updateInput(JNIEnv* env, jstring input);
45 
46     icu::RegexMatcher* matcher();
47 
48     UErrorCode& status();
49 
50     void updateOffsets(JNIEnv* env, jintArray javaOffsets);
51 
52 private:
53     std::unique_ptr<icu::RegexMatcher> mMatcher;
54     std::unique_ptr<UChar[]> mUChars;
55     UText* mUText;
56     UErrorCode mStatus;
57 
58     // Disallow copy and assignment.
59     MatcherState(const MatcherState&);
60     void operator=(const MatcherState&);
61 };