1 /* 2 * Copyright (C) 2019 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 package com.android.icu.util.regex; 18 19 import libcore.util.NativeAllocationRegistry; 20 21 /** 22 * Provide an entry point to use ICU4C icu::RegexPattern. 23 * 24 * @hide 25 */ 26 @libcore.api.IntraCoreApi 27 public class PatternNative { 28 29 private static final NativeAllocationRegistry REGISTRY = NativeAllocationRegistry 30 .createMalloced(PatternNative.class.getClassLoader(), getNativeFinalizer()); 31 32 @dalvik.annotation.optimization.ReachabilitySensitive 33 private final long address; 34 35 /** 36 * Create an {@link PatternNative} with a regular expression string. 37 * 38 * @param pattern the regular expression to be compiled 39 * @param flags a bit set of {@link java.util.regex.Pattern#UNIX_LINES}, 40 * {@link java.util.regex.Pattern#CASE_INSENSITIVE}, 41 * {@link java.util.regex.Pattern#COMMENTS}, 42 * {@link java.util.regex.Pattern#MULTILINE} and 43 * {@link java.util.regex.Pattern#DOTALL}. 44 * 45 * @hide 46 */ 47 @libcore.api.IntraCoreApi create(String pattern, int flags)48 public static PatternNative create(String pattern, int flags) { 49 return new PatternNative(pattern, flags); 50 } 51 PatternNative(String pattern, int flags)52 private PatternNative(String pattern, int flags) { 53 address = compileImpl(pattern, flags); 54 REGISTRY.registerNativeAllocation(this, address); 55 } 56 getMatchedGroupIndex(String groupName)57 /* package */ int getMatchedGroupIndex(String groupName) { 58 return getMatchedGroupIndexImpl(address, groupName); 59 } 60 openMatcher()61 /* package */ long openMatcher() { 62 return openMatcherImpl(address); 63 } 64 65 /** 66 * @return native address of the native allocation. 67 */ compileImpl(String pattern, int flags)68 private static native long compileImpl(String pattern, int flags); 69 70 /** 71 * @return address of a native function of type <code>void f(void* nativePtr)</code> 72 * used to free this kind of native allocation 73 */ getNativeFinalizer()74 private static native long getNativeFinalizer(); 75 76 /** 77 * @param addr the NativePattern.address 78 * @return native address of matcher implementation 79 */ openMatcherImpl(long addr)80 private static native long openMatcherImpl(long addr); 81 82 /** 83 * @param groupName The name of a named-capturing group 84 * @return the index of the named-capturing group 85 */ getMatchedGroupIndexImpl(long addr, String groupName)86 private static native int getMatchedGroupIndexImpl(long addr, String groupName); 87 88 } 89