• 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 #ifndef TC3_STD_STRING_IMPORT
18 #define TC3_STD_STRING_IMPORT
19 #include <string>
20 
21 namespace libtextclassifier3 {
22 using string = std::string;
23 template <class CharT, class Traits = std::char_traits<CharT>,
24           class Allocator = std::allocator<CharT> >
25 using basic_string = std::basic_string<CharT, Traits, Allocator>;
26 }  // namespace libtextclassifier3
27 #endif
28 #ifndef NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_STRINGPIECE_H_
29 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_STRINGPIECE_H_
30 
31 #include <stddef.h>
32 #include <string.h>
33 
34 #include <ostream>
35 #include <string>
36 
37 namespace libtextclassifier3 {
38 namespace mobile {
39 
40 // Read-only "view" of a piece of data.  Does not own the underlying data.
41 class StringPiece {
42  public:
StringPiece()43   StringPiece() : StringPiece(nullptr, 0) {}
44 
StringPiece(const char * str)45   StringPiece(const char *str)  // NOLINT
46       : start_(str), size_(strlen(str)) {}
47 
StringPiece(const char * start,size_t size)48   StringPiece(const char *start, size_t size) : start_(start), size_(size) {}
49 
50   // Intentionally no "explicit" keyword: in function calls, we want strings to
51   // be converted to StringPiece implicitly.
StringPiece(const string & s)52   StringPiece(const string &s)  // NOLINT
53       : StringPiece(s.data(), s.size()) {}
54 
StringPiece(const string & s,int offset,int len)55   StringPiece(const string &s, int offset, int len)
56       : StringPiece(s.data() + offset, len) {}
57 
58   char operator[](size_t i) const { return start_[i]; }
59 
60   // Returns start address of underlying data.
data()61   const char *data() const { return start_; }
62 
63   // Returns number of bytes of underlying data.
size()64   size_t size() const { return size_; }
65 
66   // Returns true if this StringPiece does not refer to any characters.
empty()67   bool empty() const { return size() == 0; }
68 
69   template <typename A>
70   explicit operator basic_string<char, std::char_traits<char>, A>() const {
71     if (!data()) return {};
72     return basic_string<char, std::char_traits<char>, A>(data(), size());
73   }
74 
75  private:
76   const char *start_;  // Not owned.
77   size_t size_;
78 };
79 
80 inline std::ostream &operator<<(std::ostream &out, StringPiece sp) {
81   return out.write(sp.data(), sp.size());
82 }
83 
84 }  // namespace mobile
85 }  // namespace nlp_saft
86 
87 #endif  // NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_STRINGPIECE_H_
88