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 #include "src/sksl/SkSLDefines.h" 12 #include <cstring> 13 #include <stdarg.h> 14 #include <string> 15 16 namespace SkSL { 17 18 // Represents a (not necessarily null-terminated) slice of a string. 19 struct StringFragment { StringFragmentStringFragment20 StringFragment() 21 : fChars("") 22 , fLength(0) {} 23 StringFragmentStringFragment24 StringFragment(const char* chars) 25 : fChars(chars) 26 , fLength(strlen(chars)) {} 27 StringFragmentStringFragment28 StringFragment(const char* chars, size_t length) 29 : fChars(chars) 30 , fLength(length) {} 31 32 char operator[](size_t idx) const { 33 return fChars[idx]; 34 } 35 36 bool operator==(const char* s) const; 37 bool operator!=(const char* s) const; 38 bool operator==(StringFragment s) const; 39 bool operator!=(StringFragment s) const; 40 bool operator<(StringFragment s) const; 41 42 const char* fChars; 43 size_t fLength; 44 }; 45 46 bool operator==(const char* s1, StringFragment s2); 47 48 bool operator!=(const char* s1, StringFragment s2); 49 50 class SK_API String : public std::string { 51 public: 52 String() = default; 53 String(const String&) = default; 54 String(String&&) = default; 55 String& operator=(const String&) = default; 56 String& operator=(String&&) = default; 57 String(const char * s)58 String(const char* s) 59 : INHERITED(s) {} 60 String(const char * s,size_t size)61 String(const char* s, size_t size) 62 : INHERITED(s, size) {} 63 String(StringFragment s)64 String(StringFragment s) 65 : INHERITED(s.fChars, s.fLength) {} 66 67 static String printf(const char* fmt, ...); 68 69 void appendf(const char* fmt, ...); 70 // For API compatibility with SkString's reset (vs. std:string's clear) 71 void reset(); 72 // For API compatibility with SkString's findLastOf(vs. find_last_of -> size_t) 73 int findLastOf(const char c) const; 74 75 void vappendf(const char* fmt, va_list va); 76 77 bool startsWith(const char* s) const; 78 bool endsWith(const char* s) const; 79 80 int find(const char* substring, int fromPos = 0) const; 81 int find(const String& substring, int fromPos = 0) const; 82 83 String operator+(const char* s) const; 84 String operator+(const String& s) const; 85 String operator+(StringFragment s) const; 86 String& operator+=(char c); 87 String& operator+=(const char* s); 88 String& operator+=(const String& s); 89 String& operator+=(StringFragment s); 90 bool operator==(const char* s) const; 91 bool operator!=(const char* s) const; 92 bool operator==(const String& s) const; 93 bool operator!=(const String& s) const; 94 friend String operator+(const char* s1, const String& s2); 95 friend bool operator==(const char* s1, const String& s2); 96 friend bool operator!=(const char* s1, const String& s2); 97 98 private: 99 typedef std::string INHERITED; 100 }; 101 102 String operator+(const char* s1, const String& s2); 103 bool operator!=(const char* s1, const String& s2); 104 105 String to_string(double value); 106 107 String to_string(int32_t value); 108 109 String to_string(uint32_t value); 110 111 String to_string(int64_t value); 112 113 String to_string(uint64_t value); 114 115 SKSL_INT stoi(const String& s); 116 117 SKSL_FLOAT stod(const String& s); 118 119 long stol(const String& s); 120 121 } // namespace SkSL 122 123 namespace std { 124 template<> struct hash<SkSL::StringFragment> { 125 size_t operator()(const SkSL::StringFragment& s) const { 126 size_t result = 0; 127 for (size_t i = 0; i < s.fLength; ++i) { 128 result = result * 101 + s.fChars[i]; 129 } 130 return result; 131 } 132 }; 133 134 template<> struct hash<SkSL::String> { 135 size_t operator()(const SkSL::String& s) const { 136 return hash<std::string>{}(s); 137 } 138 }; 139 } // namespace std 140 141 #endif 142