• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_SUPPORT_CUSTOM_OPS_KERNEL_SENTENCEPIECE_UTILS_H_
17 #define TENSORFLOW_LITE_SUPPORT_CUSTOM_OPS_KERNEL_SENTENCEPIECE_UTILS_H_
18 
19 #include <ostream>
20 #include <string>
21 
22 namespace tflite {
23 namespace ops {
24 namespace custom {
25 namespace sentencepiece {
26 
27 // AOSP and WASM doesn't support string_view,
28 // we put here a minimal re-implementation.
29 namespace utils {
30 
31 class string_view {
32  public:
string_view(const std::string & s)33   explicit string_view(const std::string& s)
34       : str_(s.data()), len_(s.length()) {}
string_view(const char * str,int len)35   string_view(const char* str, int len) : str_(str), len_(len) {}
36   // A constructor from c string.
string_view(const char * s)37   explicit string_view(const char* s) : str_(s), len_(strlen(s)) {}
38 
length()39   int length() const { return len_; }
data()40   const char* data() const { return str_; }
empty()41   bool empty() const { return len_ == 0; }
at(int i)42   unsigned char at(int i) const { return str_[i]; }
43 
44  private:
45   const char* str_ = nullptr;
46   const int len_ = 0;
47 };
48 
49 inline std::ostream& operator<<(std::ostream& os, const string_view& sv) {
50   os << std::string(sv.data(), sv.length());
51   return os;
52 }
53 inline bool operator==(const string_view& view1, const string_view& view2) {
54   if (view1.length() != view2.length()) {
55     return false;
56   }
57   return memcmp(view1.data(), view2.data(), view1.length()) == 0;
58 }
59 
60 }  // namespace utils
61 }  // namespace sentencepiece
62 }  // namespace custom
63 }  // namespace ops
64 }  // namespace tflite
65 
66 #endif  // TENSORFLOW_LITE_SUPPORT_CUSTOM_OPS_KERNEL_SENTENCEPIECE_UTILS_H_
67