41struct [[deprecated(
"Use std::string_view instead")]]
StringRef {
45 StringRef(
const char *str,
size_t l) : p(str), len(l) {}
46 StringRef(
const char *str) : p(str), len(str ? strlen(str) : 0) {}
47 StringRef(
const std::string &str) : p(str.data()), len(str.size()) {}
62 explicit operator bool()
const {
return p != 0; }
64 bool operator==(
const StringRef &a)
const {
65 return p ? (a.p && len == a.len && (!len || !memcmp(p, a.p, len))) : !a.p;
67 bool operator!=(
const StringRef &a)
const {
return !operator==(a); }
68 bool operator==(
const std::string &a)
const {
return operator==(
StringRef(a)); }
69 bool operator==(
const char *a)
const {
70 return p ? (a && (!len || !strncmp(p, a, len)) && !a[len]) : !a;
72 bool operator==(
cstring a)
const {
return operator==(a.c_str()); }
74 bool operator!=(T a)
const {
77 bool isNullOrEmpty()
const {
return p == 0 || len == 0; }
80 if (!p)
return a.p ? -1 : 0;
82 int rv = memcmp(p, a.p, std::min(len, a.len));
83 if (!rv && len != a.len) rv = len < a.len ? -1 : 1;
86 int compare(
const std::string &a)
const {
return compare(
StringRef(a)); }
87 int compare(
const char *a)
const {
88 if (!p)
return a ? -1 : 0;
90 int rv = strncmp(p, a, len);
91 if (!rv && a[len]) rv = -1;
94 int compare(
cstring a)
const {
return compare(a.c_str()); }
96 bool operator<(T a)
const {
97 return compare(a) < 0;
100 bool operator<=(T a)
const {
101 return compare(a) <= 0;
104 bool operator>(T a)
const {
105 return compare(a) > 0;
108 bool operator>=(T a)
const {
109 return compare(a) >= 0;
112 explicit operator std::string()
const {
return std::string(p, len); }
113 operator cstring()
const {
return std::string(p, len); }
114 cstring toString()
const {
return std::string(p, len); }
115 std::string string()
const {
return std::string(p, len); }
151 char operator[](
size_t i)
const {
return i < len ? p[i] : 0; }
152 char operator*()
const {
return len ? *p : 0; }
158 StringRef &trim(
const char *white =
" \t\r\n") {
159 while (len > 0 && strchr(white, *p)) {
163 while (len > 0 && strchr(white, p[len - 1])) {
170 while (len > 0 && p[len - 1] ==
'\r') {
176 StringRef trim(
const char *white =
" \t\r\n")
const {
181 const char *begin()
const {
return p; }
182 const char *end()
const {
return p + len; }
183 const char *find(
char ch)
const {
184 return p ?
static_cast<const char *
>(memchr(p, ch, len)) : p;
186 const char *findlast(
char ch)
const {
187 return p ?
static_cast<const char *
>(memrchr(p, ch, len)) : p;
189 const char *find(
const char *set)
const {
191 size_t off = strcspn(p, set);
192 return off >= len ? 0 : p + off;
195 if (sub.len < 1)
return p;
196 const char *s = begin(), *e = end();
197 while (s < e && (s =
static_cast<const char *
>(memchr(s, *sub.p, e - s)))) {
198 if (sub.len > (
size_t)(e - s))
return nullptr;
199 if (!memcmp(s, sub.p, sub.len))
return s;
210 StringRef substr(
size_t start,
size_t length)
const {
211 if (len <= start)
return 0;
212 if (len <= length)
return StringRef(p + start, len - start);
216 Split split(
char)
const;
217 Split split(
const char *)
const;
292 Split(
StringRef r,
const char *s,
const char *p) : rest(r), set(s), ptr(p) {}
295 Split begin()
const {
return *
this; }
297 StringRef operator*()
const {
return ptr ? rest.before(ptr) : rest; }
298 Split &operator++() {
300 rest = rest.after(ptr + 1);
301 ptr = set ? rest.find(set) : rest.find(*ptr);
307 bool operator==(
const Split &a)
const {
return rest == a.rest && ptr == a.ptr; }
308 bool operator!=(
const Split &a)
const {
return rest != a.rest || ptr != a.ptr; }