|
| 1 | +/* |
| 2 | + Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 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 | +#include "paddle/strings/stringpiece.h" |
| 18 | + |
| 19 | +// #include <stddef.h> |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <iosfwd> |
| 24 | + |
| 25 | +namespace paddle { |
| 26 | + |
| 27 | +StringPiece::StringPiece() : data_(NULL), size_(0) {} |
| 28 | + |
| 29 | +StringPiece::StringPiece(const char* d, size_t n) : data_(d), size_(n) { |
| 30 | + if (d == NULL && n != 0) |
| 31 | + throw std::invalid_argument( |
| 32 | + "StringPiece requires len to be 0 for NULL data"); |
| 33 | +} |
| 34 | + |
| 35 | +StringPiece::StringPiece(const char* s) : data_(s) { |
| 36 | + size_ = (s == NULL) ? 0 : strlen(s); |
| 37 | +} |
| 38 | + |
| 39 | +StringPiece::StringPiece(const std::string& s) |
| 40 | + : data_(s.data()), size_(s.size()) {} |
| 41 | + |
| 42 | +int Compare(StringPiece a, StringPiece b) { |
| 43 | + const size_t min_len = (a.len() < b.len()) ? a.len() : b.len(); |
| 44 | + int r = memcmp(a.data(), b.data(), min_len); |
| 45 | + if (r == 0) { |
| 46 | + if (a.len() < b.len()) |
| 47 | + return -1; |
| 48 | + else if (a.len() > b.len()) |
| 49 | + return 1; |
| 50 | + } |
| 51 | + return r; |
| 52 | +} |
| 53 | + |
| 54 | +bool operator==(StringPiece x, StringPiece y) { |
| 55 | + return ((x.len() == y.len()) && |
| 56 | + (x.data() == y.data() || memcmp(x.data(), y.data(), x.len()) == 0)); |
| 57 | +} |
| 58 | + |
| 59 | +bool operator!=(StringPiece x, StringPiece y) { return !(x == y); } |
| 60 | + |
| 61 | +bool operator<(StringPiece x, StringPiece y) { return Compare(x, y) < 0; } |
| 62 | +bool operator>(StringPiece x, StringPiece y) { return Compare(x, y) > 0; } |
| 63 | + |
| 64 | +bool operator<=(StringPiece x, StringPiece y) { return Compare(x, y) <= 0; } |
| 65 | +bool operator>=(StringPiece x, StringPiece y) { return Compare(x, y) >= 0; } |
| 66 | + |
| 67 | +bool HasPrefix(StringPiece s, StringPiece x) { |
| 68 | + return ((s.len() >= x.len()) && (memcmp(s.data(), x.data(), x.len()) == 0)); |
| 69 | +} |
| 70 | + |
| 71 | +bool HasSuffix(StringPiece s, StringPiece x) { |
| 72 | + return ((s.len() >= x.len()) && |
| 73 | + (memcmp(s.data() + (s.len() - x.len()), x.data(), x.len()) == 0)); |
| 74 | +} |
| 75 | + |
| 76 | +StringPiece SkipPrefix(StringPiece s, size_t n) { |
| 77 | + if (n > s.len()) |
| 78 | + throw std::invalid_argument("Skip distance larger than StringPiece length"); |
| 79 | + return StringPiece(s.data() + n, s.len() - n); |
| 80 | +} |
| 81 | + |
| 82 | +StringPiece SkipSuffix(StringPiece s, size_t n) { |
| 83 | + if (n > s.len()) |
| 84 | + throw std::invalid_argument("Skip distance larger than StringPiece length"); |
| 85 | + return StringPiece(s.data(), s.len() - n); |
| 86 | +} |
| 87 | + |
| 88 | +StringPiece TrimPrefix(StringPiece s, StringPiece x) { |
| 89 | + return HasPrefix(s, x) ? SkipPrefix(s, x.len()) : s; |
| 90 | +} |
| 91 | + |
| 92 | +StringPiece TrimSuffix(StringPiece s, StringPiece x) { |
| 93 | + return HasSuffix(s, x) ? SkipSuffix(s, x.len()) : s; |
| 94 | +} |
| 95 | + |
| 96 | +bool Contains(StringPiece s, StringPiece sub) { |
| 97 | + return std::search(s.begin(), s.end(), sub.begin(), sub.end()) != s.end(); |
| 98 | +} |
| 99 | + |
| 100 | +size_t Index(StringPiece s, StringPiece sub) { |
| 101 | + auto e = std::search(s.begin(), s.end(), sub.begin(), sub.end()); |
| 102 | + return e != s.end() ? e - s.data() : StringPiece::npos; |
| 103 | +} |
| 104 | + |
| 105 | +size_t Find(StringPiece s, char c, size_t pos) { |
| 106 | + if (pos >= s.len()) { |
| 107 | + return StringPiece::npos; |
| 108 | + } |
| 109 | + const char* result = |
| 110 | + reinterpret_cast<const char*>(memchr(s.data() + pos, c, s.len() - pos)); |
| 111 | + return result != nullptr ? result - s.data() : StringPiece::npos; |
| 112 | +} |
| 113 | + |
| 114 | +size_t RFind(StringPiece s, char c, size_t pos) { |
| 115 | + if (s.len() == 0) return StringPiece::npos; |
| 116 | + for (const char* p = s.data() + std::min(pos, s.len() - 1); p >= s.data(); |
| 117 | + p--) { |
| 118 | + if (*p == c) { |
| 119 | + return p - s.data(); |
| 120 | + } |
| 121 | + } |
| 122 | + return StringPiece::npos; |
| 123 | +} |
| 124 | + |
| 125 | +StringPiece SubStr(StringPiece s, size_t pos, size_t n) { |
| 126 | + if (pos > s.len()) pos = s.len(); |
| 127 | + if (n > s.len() - pos) n = s.len() - pos; |
| 128 | + return StringPiece(s.data() + pos, n); |
| 129 | +} |
| 130 | + |
| 131 | +std::ostream& operator<<(std::ostream& o, StringPiece piece) { |
| 132 | + return o << piece.ToString(); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace paddle |
0 commit comments