StringPiece.cc
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 #include <strings/StringPiece.h>
19 
20 #include <algorithm>
21 #include <iostream>
22 
23 namespace momemta {
24 
25 std::ostream& operator<<(std::ostream& o, StringPiece piece) {
26  o.write(piece.data(), piece.size());
27  return o;
28 }
29 
30 bool StringPiece::contains(StringPiece s) const {
31  return std::search(begin(), end(), s.begin(), s.end()) != end();
32 }
33 
34 size_t StringPiece::find(char c, size_t pos) const {
35  if (pos >= size_) {
36  return npos;
37  }
38  const char* result =
39  reinterpret_cast<const char*>(memchr(data_ + pos, c, size_ - pos));
40  return result != NULL ? result - data_ : npos;
41 }
42 
43 // Search range is [0..pos] inclusive. If pos == npos, search everything.
44 size_t StringPiece::rfind(char c, size_t pos) const {
45  if (size_ == 0) return npos;
46  for (const char* p = data_ + std::min(pos, size_ - 1); p >= data_; p--) {
47  if (*p == c) {
48  return p - data_;
49  }
50  }
51  return npos;
52 }
53 
54 StringPiece StringPiece::substr(size_t pos, size_t n) const {
55  if (pos > size_) pos = size_;
56  if (n > size_ - pos) n = size_ - pos;
57  return StringPiece(data_ + pos, n);
58 }
59 
60 const StringPiece::size_type StringPiece::npos = size_type(-1);
61 
62 } // namespace momemta
Definition: Graph.h:21