1 /* 2 * Copyright 2017 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SKSL_STRING 9 #define SKSL_STRING 10 11 12 #ifdef SKSL_STANDALONE 13 #define SKSL_STRING_BASE std::string 14 #include <string> 15 #else 16 #define SKSL_STRING_BASE SkString 17 #include "SkString.h" 18 #endif 19 20 namespace SkSL { 21 22 class String : public SKSL_STRING_BASE { 23 public: 24 String() = default; 25 String(const String&) = default; 26 String(String&&) = default; 27 String& operator=(const String&) = default; 28 String& operator=(String&&) = default; 29 30 #ifndef SKSL_STANDALONE String(const SkString & s)31 String(const SkString& s) 32 : INHERITED(s) {} 33 #endif 34 String(const char * s)35 String(const char* s) 36 : INHERITED(s) {} 37 String(const char * s,size_t size)38 String(const char* s, size_t size) 39 : INHERITED(s, size) {} 40 41 static String printf(const char* fmt, ...); 42 43 #ifdef SKSL_STANDALONE 44 void appendf(const char* fmt, ...); 45 #endif 46 void vappendf(const char* fmt, va_list va); 47 48 bool startsWith(const char* s) const; 49 bool endsWith(const char* s) const; 50 51 String operator+(const char* s) const; 52 String operator+(const String& s) const; 53 bool operator==(const char* s) const; 54 bool operator!=(const char* s) const; 55 bool operator==(const String& s) const; 56 bool operator!=(const String& s) const; 57 friend String operator+(const char* s1, const String& s2); 58 friend bool operator==(const char* s1, const String& s2); 59 friend bool operator!=(const char* s1, const String& s2); 60 61 private: 62 typedef SKSL_STRING_BASE INHERITED; 63 }; 64 65 String operator+(const char* s1, const String& s2); 66 bool operator!=(const char* s1, const String& s2); 67 68 String to_string(double value); 69 70 String to_string(int32_t value); 71 72 String to_string(uint32_t value); 73 74 String to_string(int64_t value); 75 76 String to_string(uint64_t value); 77 78 int stoi(String s); 79 80 double stod(String s); 81 82 long stol(String s); 83 84 } // namespace 85 86 #ifdef SKSL_STANDALONE 87 namespace std { 88 template<> struct hash<SkSL::String> { 89 size_t operator()(const SkSL::String& s) const { 90 return hash<std::string>{}(s); 91 } 92 }; 93 } // namespace 94 #else 95 #include "SkOpts.h" 96 namespace std { 97 template<> struct hash<SkSL::String> { 98 size_t operator()(const SkSL::String& s) const { 99 return SkOpts::hash_fn(s.c_str(), s.size(), 0); 100 } 101 }; 102 } // namespace 103 #endif // SKIA_STANDALONE 104 105 #endif 106