diff options
author | Ethan Nicholas <ethannicholas@google.com> | 2017-09-07 15:44:01 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-09-11 16:17:00 +0000 |
commit | c576e93d174f3106e072a2f506bca3990b541265 (patch) | |
tree | d4a410200aa71183c95643535b440bec919f2e18 /src/sksl/SkSLString.h | |
parent | a2bdf005f3c706065d1aa93f319f4b73932721d4 (diff) |
Switch to the new SkSL lexer.
This completely replaces flex with a new in-house lexical analyzer generator,
which we have done for performance and memory usage reasons. Flex requires us
to copy strings every time we need the text of a token, whereas this new lexer
allows us to handle strings as a (non-null-terminated) pointer and length
everywhere, eliminating most string copies.
Bug: skia:
Change-Id: I2add26efc9e20cb699520e82abcf713af3968aca
Reviewed-on: https://skia-review.googlesource.com/39780
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Diffstat (limited to 'src/sksl/SkSLString.h')
-rw-r--r-- | src/sksl/SkSLString.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/sksl/SkSLString.h b/src/sksl/SkSLString.h index 93bd7a5773..9cfdce4768 100644 --- a/src/sksl/SkSLString.h +++ b/src/sksl/SkSLString.h @@ -8,6 +8,8 @@ #ifndef SKSL_STRING #define SKSL_STRING +#include <cstring> + #define SKSL_USE_STD_STRING #include <stdarg.h> @@ -22,6 +24,33 @@ namespace SkSL { +// Represents a (not necessarily null-terminated) slice of a string. +struct StringFragment { + StringFragment() + : fChars(nullptr) + , fLength(0) {} + + StringFragment(const char* chars) + : fChars(chars) + , fLength(strlen(chars)) {} + + StringFragment(const char* chars, size_t length) + : fChars(chars) + , fLength(length) {} + + char operator[](size_t idx) const { + return fChars[idx]; + } + + bool operator==(const char* s) const; + bool operator!=(const char* s) const; + bool operator==(StringFragment s) const; + bool operator!=(StringFragment s) const; + + const char* fChars; + size_t fLength; +}; + class String : public SKSL_STRING_BASE { public: String() = default; @@ -41,6 +70,9 @@ public: String(const char* s, size_t size) : INHERITED(s, size) {} + String(StringFragment s) + : INHERITED(s.fChars, s.fLength) {} + static String printf(const char* fmt, ...); #ifdef SKSL_USE_STD_STRING @@ -53,6 +85,11 @@ public: String operator+(const char* s) const; String operator+(const String& s) const; + String operator+(StringFragment s) const; + String& operator+=(char c); + String& operator+=(const char* s); + String& operator+=(const String& s); + String& operator+=(StringFragment s); bool operator==(const char* s) const; bool operator!=(const char* s) const; bool operator==(const String& s) const; @@ -86,6 +123,18 @@ long stol(const String& s); } // namespace +namespace std { + template<> struct hash<SkSL::StringFragment> { + size_t operator()(const SkSL::StringFragment& s) const { + size_t result = 0; + for (size_t i = 0; i < s.fLength; ++i) { + result = result * 101 + s.fChars[i]; + } + return result; + } + }; +} // namespace + #ifdef SKSL_USE_STD_STRING namespace std { template<> struct hash<SkSL::String> { |