StringPiece.h
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Modifications copyright 2017 Universite catholique de Louvain (UCL), Belgium
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==============================================================================*/
17 
18 // StringPiece is a simple structure containing a pointer into some external
19 // storage and a size. The user of a StringPiece must ensure that the slice
20 // is not used after the corresponding external storage has been
21 // deallocated.
22 //
23 // Multiple threads can invoke const methods on a StringPiece without
24 // external synchronization, but if any of the threads may call a
25 // non-const method, all threads accessing the same StringPiece must use
26 // external synchronization.
27 
28 #pragma once
29 
30 #include <cassert>
31 #include <cstddef>
32 #include <cstring>
33 #include <iosfwd>
34 #include <string>
35 
36 namespace momemta {
37 
38 class StringPiece {
39 public:
40  typedef size_t size_type;
41 
42  // Create an empty slice.
43  StringPiece() : data_(""), size_(0) {}
44 
45  // Create a slice that refers to d[0,n-1].
46  StringPiece(const char *d, size_t n) : data_(d), size_(n) {}
47 
48  // Create a slice that refers to the contents of "s"
49  StringPiece(const std::string& s) : data_(s.data()), size_(s.size()) {}
50 
51  // Create a slice that refers to s[0,strlen(s)-1]
52  StringPiece(const char *s) : data_(s), size_(strlen(s)) {}
53 
54  void set(const void *data, size_t len) {
55  data_ = reinterpret_cast<const char *>(data);
56  size_ = len;
57  }
58 
59  // Return a pointer to the beginning of the referenced data
60  const char *data() const { return data_; }
61 
62  // Return the length (in bytes) of the referenced data
63  size_t size() const { return size_; }
64 
65  // Return true iff the length of the referenced data is zero
66  bool empty() const { return size_ == 0; }
67 
68  typedef const char *const_iterator;
69  typedef const char *iterator;
70  iterator begin() const { return data_; }
71  iterator end() const { return data_ + size_; }
72 
73  static const size_t npos;
74 
75  // Return the ith byte in the referenced data.
76  // REQUIRES: n < size()
77  char operator[](size_t n) const {
78  assert(n < size());
79  return data_[n];
80  }
81 
82  // Change this slice to refer to an empty array
83  void clear() {
84  data_ = "";
85  size_ = 0;
86  }
87 
88  // Drop the first "n" bytes from this slice.
89  void remove_prefix(size_t n) {
90  assert(n <= size());
91  data_ += n;
92  size_ -= n;
93  }
94 
95  void remove_suffix(size_t n) {
96  assert(size_ >= n);
97  size_ -= n;
98  }
99 
100  size_t find(char c, size_t pos = 0) const;
101  size_t rfind(char c, size_t pos = npos) const;
102  bool contains(StringPiece s) const;
103 
104  // Checks whether StringPiece starts with x and if so advances the beginning
105  // of it to past the match. It's basically a shortcut for starts_with
106  // followed by remove_prefix.
107  bool Consume(StringPiece x) {
108  if (starts_with(x)) {
109  remove_prefix(x.size_);
110  return true;
111  }
112  return false;
113  }
114 
115  StringPiece substr(size_t pos, size_t n = npos) const;
116 
117  struct Hasher {
118  size_t operator()(StringPiece arg) const;
119  };
120 
121  // Return a string that contains the copy of the referenced data.
122  std::string ToString() const { return std::string(data_, size_); }
123 
124  // Three-way comparison. Returns value:
125  // < 0 iff "*this" < "b",
126  // == 0 iff "*this" == "b",
127  // > 0 iff "*this" > "b"
128  int compare(StringPiece b) const;
129 
130  // Return true iff "x" is a prefix of "*this"
131  bool starts_with(StringPiece x) const {
132  return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0));
133  }
134  // Return true iff "x" is a suffix of "*this"
135  bool ends_with(StringPiece x) const {
136  return ((size_ >= x.size_) &&
137  (memcmp(data_ + (size_ - x.size_), x.data_, x.size_) == 0));
138  }
139 
140 private:
141  const char *data_;
142  size_t size_;
143 
144  // Intentionally copyable
145 };
146 
147 inline bool operator==(StringPiece x, StringPiece y) {
148  return ((x.size() == y.size()) &&
149  (memcmp(x.data(), y.data(), x.size()) == 0));
150 }
151 
152 inline bool operator!=(StringPiece x, StringPiece y) { return !(x == y); }
153 
154 inline bool operator<(StringPiece x, StringPiece y) { return x.compare(y) < 0; }
155 inline bool operator>(StringPiece x, StringPiece y) { return x.compare(y) > 0; }
156 inline bool operator<=(StringPiece x, StringPiece y) {
157  return x.compare(y) <= 0;
158 }
159 inline bool operator>=(StringPiece x, StringPiece y) {
160  return x.compare(y) >= 0;
161 }
162 
163 inline int StringPiece::compare(StringPiece b) const {
164  const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
165  int r = memcmp(data_, b.data_, min_len);
166  if (r == 0) {
167  if (size_ < b.size_)
168  r = -1;
169  else if (size_ > b.size_)
170  r = +1;
171  }
172  return r;
173 }
174 
175 // allow StringPiece to be logged
176 extern std::ostream& operator<<(std::ostream& o, momemta::StringPiece piece);
177 
178 } // namespace momemta
Definition: Graph.h:21