piece.h 3.4 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Y
Yi Wang 已提交
14 15 16

#pragma once

L
liaogang 已提交
17
#include <ostream>
L
liaogang 已提交
18
#include <string>
Y
Yi Wang 已提交
19 20

namespace paddle {
21
namespace string {
Y
Yi Wang 已提交
22

23
// Piece points into a std::string object but doesn't own the
Y
Yi Wang 已提交
24
// string.  It is for efficient access to strings.  Like Go's string
25
// type.  Not that Piece doesn't mutate the underlying string,
Y
Yi Wang 已提交
26
// so it is thread-safe given that the underlying string doesn't
27
// change.  Because Piece contains a little data members, and
Y
Yi Wang 已提交
28
// its syntax is simple as it doesn't own/manage the string, it is
29 30
// cheap to construct Pieces and pass them around.
class Piece {
Y
Yi Wang 已提交
31
public:
Y
Yi Wang 已提交
32 33 34
  static const size_t npos = static_cast<size_t>(-1);

  // We provide non-explicit singleton constructors so users can
35
  // pass in a "const char*" or a "string" wherever a "Piece"
L
liaogang 已提交
36
  // is expected.  These constructors ensure that if data_ is NULL,
Y
Yi Wang 已提交
37
  // size_ is 0.
38 39
  Piece();
  Piece(const char* d, size_t n);
Y
Yi Wang 已提交
40 41
  Piece(const char* d);         // NOLINT: accept C string into Piece.
  Piece(const std::string& s);  // NOLINT: accept C++ string into Piece.
Y
Yi Wang 已提交
42 43

  const char* data() const { return data_; }
Y
Yi Wang 已提交
44
  size_t len() const { return size_; }
Y
Yi Wang 已提交
45

Y
Yi Wang 已提交
46
  char operator[](size_t n) const;
Y
Yi Wang 已提交
47

48
  // Piece doesn't own the string, so both iterator and const
Y
Yi Wang 已提交
49 50 51 52 53 54 55 56 57
  // iterator are const char* indeed.
  typedef const char* const_iterator;
  typedef const char* iterator;
  iterator begin() const { return data_; }
  iterator end() const { return data_ + size_; }

  // Return a string that contains the copy of the referenced data.
  std::string ToString() const { return std::string(data_, size_); }

Y
Yi Wang 已提交
58
private:
Y
Yi Wang 已提交
59 60 61 62 63 64
  const char* data_;
  size_t size_;

  // Intentionally copyable
};

65
int Compare(Piece a, Piece b);
Y
Yi Wang 已提交
66

67 68 69 70 71 72
bool operator==(Piece x, Piece y);
bool operator!=(Piece x, Piece y);
bool operator<(Piece x, Piece y);
bool operator>(Piece x, Piece y);
bool operator<=(Piece x, Piece y);
bool operator>=(Piece x, Piece y);
Y
Yi Wang 已提交
73

74 75
bool HasPrefix(Piece s, Piece prefix);
bool HasSuffix(Piece s, Piece suffix);
Y
Yi Wang 已提交
76

77 78
Piece SkipPrefix(Piece s, size_t n);
Piece SkipSuffix(Piece s, size_t n);
Y
Yi Wang 已提交
79 80

// Skip the prefix (or suffix) if it matches with the string.
81 82
Piece TrimPrefix(Piece s, Piece prefix);
Piece TrimSuffix(Piece s, Piece suffix);
Y
Yi Wang 已提交
83

Y
Yi Wang 已提交
84 85
// Returns if s contains sub.  Any s except for empty s contains an
// empty sub.
86
bool Contains(Piece s, Piece sub);
Y
Yi Wang 已提交
87

Y
Yi Wang 已提交
88 89 90
// Return the first occurrence of sub in s, or npos.  If both s and
// sub is empty, it returns npos; otherwise, if only sub is empty, it
// returns 0.
91
size_t Index(Piece s, Piece sub);
Y
Yi Wang 已提交
92 93

// Return the first occurrence of c in s[pos:end], or npos.
94
size_t Find(Piece s, char c, size_t pos);
Y
Yi Wang 已提交
95 96

// Search range is [0..pos] inclusive.  If pos == npos, search everything.
97
size_t RFind(Piece s, char c, size_t pos);
Y
Yi Wang 已提交
98

99
Piece SubStr(Piece s, size_t pos, size_t n);
Y
Yi Wang 已提交
100

101 102
// allow Piece to be logged
std::ostream& operator<<(std::ostream& o, Piece piece);
Y
Yi Wang 已提交
103

104
}  // namespace string
Y
Yi Wang 已提交
105
}  // namespace paddle