diff options
Diffstat (limited to 'src/emscripten-optimizer/istring.h')
-rw-r--r-- | src/emscripten-optimizer/istring.h | 109 |
1 files changed, 54 insertions, 55 deletions
diff --git a/src/emscripten-optimizer/istring.h b/src/emscripten-optimizer/istring.h index 5c3b094c3..320a3e590 100644 --- a/src/emscripten-optimizer/istring.h +++ b/src/emscripten-optimizer/istring.h @@ -14,20 +14,21 @@ * limitations under the License. */ -// Interned String type, 100% interned on creation. Comparisons are always just a pointer comparison +// Interned String type, 100% interned on creation. Comparisons are always just +// a pointer comparison #ifndef wasm_istring_h #define wasm_istring_h -#include <unordered_set> -#include <unordered_map> #include <set> +#include <unordered_map> +#include <unordered_set> -#include <string.h> +#include <assert.h> #include <stdint.h> -#include <stdlib.h> #include <stdio.h> -#include <assert.h> +#include <stdlib.h> +#include <string.h> #include "support/threads.h" #include "support/utilities.h" @@ -35,9 +36,10 @@ namespace cashew { struct IString { - const char *str = nullptr; + const char* str = nullptr; - static size_t hash_c(const char *str) { // see http://www.cse.yorku.ca/~oz/hash.html + static size_t + hash_c(const char* str) { // see http://www.cse.yorku.ca/~oz/hash.html unsigned int hash = 5381; int c; while ((c = *str++)) { @@ -46,27 +48,27 @@ struct IString { return (size_t)hash; } - class CStringHash : public std::hash<const char *> { + class CStringHash : public std::hash<const char*> { public: - size_t operator()(const char *str) const { - return IString::hash_c(str); - } + size_t operator()(const char* str) const { return IString::hash_c(str); } }; - class CStringEqual : public std::equal_to<const char *> { + class CStringEqual : public std::equal_to<const char*> { public: - bool operator()(const char *x, const char *y) const { + bool operator()(const char* x, const char* y) const { return strcmp(x, y) == 0; } }; IString() = default; - IString(const char *s, bool reuse=true) { // if reuse=true, then input is assumed to remain alive; not copied + // if reuse=true, then input is assumed to remain alive; not copied + IString(const char* s, bool reuse = true) { assert(s); set(s, reuse); } - void set(const char *s, bool reuse=true) { - typedef std::unordered_set<const char *, CStringHash, CStringEqual> StringSet; + void set(const char* s, bool reuse = true) { + typedef std::unordered_set<const char*, CStringHash, CStringEqual> + StringSet; // one global store of strings per thread, we must not access this // in parallel thread_local static StringSet strings; @@ -79,8 +81,8 @@ struct IString { // exactly once static std::mutex mutex; std::unique_lock<std::mutex> lock(mutex); - // a single global set contains the actual strings, so we allocate each one - // exactly once. + // a single global set contains the actual strings, so we allocate each + // one exactly once. static StringSet globalStrings; auto globalExisting = globalStrings.find(s); if (globalExisting == globalStrings.end()) { @@ -103,56 +105,51 @@ struct IString { str = s; } - void set(const IString &s) { - str = s.str; - } + void set(const IString& s) { str = s.str; } - void clear() { - str = nullptr; - } + void clear() { str = nullptr; } bool operator==(const IString& other) const { - //assert((str == other.str) == !strcmp(str, other.str)); + // assert((str == other.str) == !strcmp(str, other.str)); return str == other.str; // fast! } bool operator!=(const IString& other) const { - //assert((str == other.str) == !strcmp(str, other.str)); + // assert((str == other.str) == !strcmp(str, other.str)); return str != other.str; // fast! } bool operator<(const IString& other) const { return strcmp(str ? str : "", other.str ? other.str : "") < 0; } - char operator[](int x) const { - return str[x]; - } + char operator[](int x) const { return str[x]; } bool operator!() const { // no string, or empty string return !str || str[0] == 0; } - const char *c_str() const { return str; } - bool equals(const char *other) const { return !strcmp(str, other); } + const char* c_str() const { return str; } + bool equals(const char* other) const { return !strcmp(str, other); } - bool is() const { return str != nullptr; } + bool is() const { return str != nullptr; } bool isNull() const { return str == nullptr; } - const char* stripPrefix(const char *prefix) const { - const char *ptr = str; + const char* stripPrefix(const char* prefix) const { + const char* ptr = str; while (true) { - if (*prefix == 0) return ptr; - if (*ptr == 0) return nullptr; - if (*ptr++ != *prefix++) return nullptr; + if (*prefix == 0) + return ptr; + if (*ptr == 0) + return nullptr; + if (*ptr++ != *prefix++) + return nullptr; } } - bool startsWith(const char *prefix) const { + bool startsWith(const char* prefix) const { return stripPrefix(prefix) != nullptr; } - size_t size() const { - return str ? strlen(str) : 0; - } + size_t size() const { return str ? strlen(str) : 0; } }; } // namespace cashew @@ -161,13 +158,16 @@ struct IString { namespace std { -template<> struct hash<cashew::IString> : public unary_function<cashew::IString, size_t> { +template<> +struct hash<cashew::IString> : public unary_function<cashew::IString, size_t> { size_t operator()(const cashew::IString& str) const { return std::hash<size_t>{}(size_t(str.str)); } }; -template<> struct equal_to<cashew::IString> : public binary_function<cashew::IString, cashew::IString, bool> { +template<> +struct equal_to<cashew::IString> + : public binary_function<cashew::IString, cashew::IString, bool> { bool operator()(const cashew::IString& x, const cashew::IString& y) const { return x == y; } @@ -181,32 +181,31 @@ namespace cashew { class IStringSet : public std::unordered_set<IString> { std::vector<char> data; + public: IStringSet() = default; - IStringSet(const char *init) { // comma-delimited list + IStringSet(const char* init) { // comma-delimited list int size = strlen(init) + 1; data.resize(size); - char *curr = &data[0]; + char* curr = &data[0]; strncpy(curr, init, size); while (1) { - char *end = strchr(curr, ' '); - if (end) *end = 0; + char* end = strchr(curr, ' '); + if (end) + *end = 0; insert(curr); - if (!end) break; + if (!end) + break; curr = end + 1; } } - bool has(const IString& str) { - return count(str) > 0; - } + bool has(const IString& str) { return count(str) > 0; } }; class IOrderedStringSet : public std::set<IString> { public: - bool has(const IString& str) { - return count(str) > 0; - } + bool has(const IString& str) { return count(str) > 0; } }; } // namespace cashew |