-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3361b1
commit bac8e8f
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef COMMON_STRING_PIECE_H | ||
#define COMMON_STRING_PIECE_H | ||
|
||
class Print; | ||
struct StringPiece { | ||
StringPiece(const char* s) : str(s), len(strlen(s)) {} | ||
StringPiece(const char* s, size_t l) : str(s), len(l) {} | ||
StringPiece(const char* begin, const char* end) : str(begin), len(end >= begin ? end - begin : 0) {} | ||
StringPiece() : str(0), len(0) {} | ||
int cmp(const StringPiece& other) const { | ||
const char* a = str; | ||
const char* b = other.str; | ||
size_t l = 0; | ||
while (true) { | ||
if (l == len && l == other.len) return 0; | ||
if (l == len) return -1; | ||
if (l == other.len) return 1; | ||
char A = toLower(*a); | ||
char B = toLower(*b); | ||
if (A != B) return A < B ? -1 : 1; | ||
a++; | ||
b++; | ||
l++; | ||
} | ||
} | ||
bool operator<(const StringPiece& other) const { return cmp(other) < 0; } | ||
bool operator>(const StringPiece& other) const { return cmp(other) > 0; } | ||
bool operator<=(const StringPiece& other) const { return cmp(other) <= 0; } | ||
bool operator>=(const StringPiece& other) const { return cmp(other) <= 0; } | ||
bool operator==(const StringPiece& other) const { return cmp(other) == 0; } | ||
bool operator!=(const StringPiece& other) const { return cmp(other) != 0; } | ||
char operator[](size_t x) const { return str[x]; } | ||
explicit operator bool() const { return str != nullptr && len != 0; } | ||
|
||
void printTo(Print& p); | ||
void paste(char* to) const { memcpy(to, str, len); } | ||
|
||
bool contains(char c) const { | ||
for (size_t i = 0; i < len; i++) { | ||
if (str[i] == c) return true; | ||
} | ||
return false; | ||
} | ||
|
||
const char* str; | ||
size_t len; | ||
}; | ||
|
||
#endif |