forked from YahooArchive/http-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-view.h
61 lines (48 loc) · 1.27 KB
/
string-view.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright (c) 2015, Yahoo Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
#ifndef STRING_VIEW_H
#define STRING_VIEW_H
#include <string>
#include <cstring>
namespace util {
struct StringView {
const char * pointer;
size_t length;
StringView(void) : pointer(NULL),
length(1) { }
StringView(const char * const p, const size_t l) :
pointer(p), length(p == NULL ? 0 : l) {
ASSERT(p == NULL || l > 0);
ASSERT(p == NULL || strlen(pointer) >= length);
}
operator const char * (void) const {
return pointer;
}
bool operator == (const StringView & s) const {
return length == s.length
&& memcmp(pointer, s.pointer, length);
}
std::string str(void) const {
if (pointer == NULL) {
return std::string();
}
return std::string(pointer, length);
}
bool exists(void) const {
return pointer != NULL || length == 0;
}
};
struct StringViewLess {
bool operator () (const StringView & a,
const StringView & b) const {
return a.length < b.length
|| (a.length == b.length
&& memcmp(static_cast< const char * >(a),
static_cast< const char * >(b), a.length) < 0);
}
};
} //end of util namespace
#endif //STRING_VIEW_H