• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include "utils/zlib/zlib_regex.h"
18 
19 #include <memory>
20 
21 #include "utils/base/logging.h"
22 #include "utils/flatbuffers.h"
23 #include "utils/utf8/unicodetext.h"
24 
25 namespace libtextclassifier3 {
26 
UncompressMakeRegexPattern(const UniLib & unilib,const flatbuffers::String * uncompressed_pattern,const CompressedBuffer * compressed_pattern,bool lazy_compile_regex,ZlibDecompressor * decompressor,std::string * result_pattern_text)27 std::unique_ptr<UniLib::RegexPattern> UncompressMakeRegexPattern(
28     const UniLib& unilib, const flatbuffers::String* uncompressed_pattern,
29     const CompressedBuffer* compressed_pattern, bool lazy_compile_regex,
30     ZlibDecompressor* decompressor, std::string* result_pattern_text) {
31   UnicodeText unicode_regex_pattern;
32   std::string decompressed_pattern;
33   if (compressed_pattern != nullptr &&
34       compressed_pattern->buffer() != nullptr) {
35     if (decompressor == nullptr ||
36         !decompressor->MaybeDecompress(compressed_pattern,
37                                        &decompressed_pattern)) {
38       TC3_LOG(ERROR) << "Cannot decompress pattern.";
39       return nullptr;
40     }
41     unicode_regex_pattern =
42         UTF8ToUnicodeText(decompressed_pattern.data(),
43                           decompressed_pattern.size(), /*do_copy=*/false);
44   } else {
45     if (uncompressed_pattern == nullptr) {
46       TC3_LOG(ERROR) << "Cannot load uncompressed pattern.";
47       return nullptr;
48     }
49     unicode_regex_pattern =
50         UTF8ToUnicodeText(uncompressed_pattern->c_str(),
51                           uncompressed_pattern->Length(), /*do_copy=*/false);
52   }
53 
54   if (result_pattern_text != nullptr) {
55     *result_pattern_text = unicode_regex_pattern.ToUTF8String();
56   }
57 
58   std::unique_ptr<UniLib::RegexPattern> regex_pattern;
59   if (lazy_compile_regex) {
60     regex_pattern = unilib.CreateLazyRegexPattern(unicode_regex_pattern);
61   } else {
62     regex_pattern = unilib.CreateRegexPattern(unicode_regex_pattern);
63   }
64 
65   if (!regex_pattern) {
66     TC3_LOG(ERROR) << "Could not create pattern: "
67                    << unicode_regex_pattern.ToUTF8String();
68   }
69   return regex_pattern;
70 }
71 
72 }  // namespace libtextclassifier3
73