SkSLString.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include "src/sksl/SkSLString.h"
  8. #include "src/sksl/SkSLUtil.h"
  9. #include <algorithm>
  10. #include <errno.h>
  11. #include <limits.h>
  12. #include <locale>
  13. #include <sstream>
  14. #include <string>
  15. namespace SkSL {
  16. String String::printf(const char* fmt, ...) {
  17. va_list args;
  18. va_start(args, fmt);
  19. String result;
  20. result.vappendf(fmt, args);
  21. va_end(args);
  22. return result;
  23. }
  24. void String::appendf(const char* fmt, ...) {
  25. va_list args;
  26. va_start(args, fmt);
  27. this->vappendf(fmt, args);
  28. va_end(args);
  29. }
  30. void String::reset() {
  31. this->clear();
  32. }
  33. int String::findLastOf(const char c) const {
  34. // Rely on find_last_of and remap the output
  35. size_t index = this->find_last_of(c);
  36. return (index == std::string::npos ? -1 : index);
  37. }
  38. void String::vappendf(const char* fmt, va_list args) {
  39. #ifdef SKSL_BUILD_FOR_WIN
  40. #define VSNPRINTF _vsnprintf
  41. #else
  42. #define VSNPRINTF vsnprintf
  43. #endif
  44. #define BUFFER_SIZE 256
  45. char buffer[BUFFER_SIZE];
  46. va_list reuse;
  47. va_copy(reuse, args);
  48. size_t size = VSNPRINTF(buffer, BUFFER_SIZE, fmt, args);
  49. if (BUFFER_SIZE >= size) {
  50. this->append(buffer, size);
  51. } else {
  52. auto newBuffer = std::unique_ptr<char[]>(new char[size + 1]);
  53. VSNPRINTF(newBuffer.get(), size + 1, fmt, reuse);
  54. this->append(newBuffer.get(), size);
  55. }
  56. va_end(reuse);
  57. }
  58. bool String::startsWith(const char* s) const {
  59. return !strncmp(c_str(), s, strlen(s));
  60. }
  61. bool String::endsWith(const char* s) const {
  62. size_t len = strlen(s);
  63. if (size() < len) {
  64. return false;
  65. }
  66. return !strncmp(c_str() + size() - len, s, len);
  67. }
  68. int String::find(const String& substring, int fromPos) const {
  69. return find(substring.c_str(), fromPos);
  70. }
  71. int String::find(const char* substring, int fromPos) const {
  72. SkASSERT(fromPos >= 0);
  73. size_t found = INHERITED::find(substring, (size_t) fromPos);
  74. return found == std::string::npos ? -1 : found;
  75. }
  76. String String::operator+(const char* s) const {
  77. String result(*this);
  78. result.append(s);
  79. return result;
  80. }
  81. String String::operator+(const String& s) const {
  82. String result(*this);
  83. result.append(s);
  84. return result;
  85. }
  86. String String::operator+(StringFragment s) const {
  87. String result(*this);
  88. result.append(s.fChars, s.fLength);
  89. return result;
  90. }
  91. String& String::operator+=(char c) {
  92. INHERITED::operator+=(c);
  93. return *this;
  94. }
  95. String& String::operator+=(const char* s) {
  96. INHERITED::operator+=(s);
  97. return *this;
  98. }
  99. String& String::operator+=(const String& s) {
  100. INHERITED::operator+=(s);
  101. return *this;
  102. }
  103. String& String::operator+=(StringFragment s) {
  104. this->append(s.fChars, s.fLength);
  105. return *this;
  106. }
  107. bool String::operator==(const String& s) const {
  108. return this->size() == s.size() && !memcmp(c_str(), s.c_str(), this->size());
  109. }
  110. bool String::operator!=(const String& s) const {
  111. return !(*this == s);
  112. }
  113. bool String::operator==(const char* s) const {
  114. return this->size() == strlen(s) && !memcmp(c_str(), s, this->size());
  115. }
  116. bool String::operator!=(const char* s) const {
  117. return !(*this == s);
  118. }
  119. String operator+(const char* s1, const String& s2) {
  120. String result(s1);
  121. result.append(s2);
  122. return result;
  123. }
  124. bool operator==(const char* s1, const String& s2) {
  125. return s2 == s1;
  126. }
  127. bool operator!=(const char* s1, const String& s2) {
  128. return s2 != s1;
  129. }
  130. bool StringFragment::operator==(StringFragment s) const {
  131. if (fLength != s.fLength) {
  132. return false;
  133. }
  134. return !memcmp(fChars, s.fChars, fLength);
  135. }
  136. bool StringFragment::operator!=(StringFragment s) const {
  137. if (fLength != s.fLength) {
  138. return true;
  139. }
  140. return memcmp(fChars, s.fChars, fLength);
  141. }
  142. bool StringFragment::operator==(const char* s) const {
  143. for (size_t i = 0; i < fLength; ++i) {
  144. if (fChars[i] != s[i]) {
  145. return false;
  146. }
  147. }
  148. return 0 == s[fLength];
  149. }
  150. bool StringFragment::operator!=(const char* s) const {
  151. for (size_t i = 0; i < fLength; ++i) {
  152. if (fChars[i] != s[i]) {
  153. return true;
  154. }
  155. }
  156. return 0 != s[fLength];
  157. }
  158. bool StringFragment::operator<(StringFragment other) const {
  159. int comparison = strncmp(fChars, other.fChars, std::min(fLength, other.fLength));
  160. if (comparison) {
  161. return comparison < 0;
  162. }
  163. return fLength < other.fLength;
  164. }
  165. bool operator==(const char* s1, StringFragment s2) {
  166. return s2 == s1;
  167. }
  168. bool operator!=(const char* s1, StringFragment s2) {
  169. return s2 != s1;
  170. }
  171. String to_string(int32_t value) {
  172. return SkSL::String::printf("%d", value);
  173. }
  174. String to_string(uint32_t value) {
  175. return SkSL::String::printf("%u", value);
  176. }
  177. String to_string(int64_t value) {
  178. std::stringstream buffer;
  179. buffer << value;
  180. return String(buffer.str().c_str());
  181. }
  182. String to_string(uint64_t value) {
  183. std::stringstream buffer;
  184. buffer << value;
  185. return String(buffer.str().c_str());
  186. }
  187. String to_string(double value) {
  188. std::stringstream buffer;
  189. buffer.imbue(std::locale::classic());
  190. buffer.precision(17);
  191. buffer << value;
  192. bool needsDotZero = true;
  193. const std::string str = buffer.str();
  194. for (int i = str.size() - 1; i >= 0; --i) {
  195. char c = str[i];
  196. if (c == '.' || c == 'e') {
  197. needsDotZero = false;
  198. break;
  199. }
  200. }
  201. if (needsDotZero) {
  202. buffer << ".0";
  203. }
  204. return String(buffer.str().c_str());
  205. }
  206. SKSL_INT stoi(const String& s) {
  207. char* p;
  208. SkDEBUGCODE(errno = 0;)
  209. long result = strtoul(s.c_str(), &p, 0);
  210. SkASSERT(*p == 0);
  211. SkASSERT(!errno);
  212. return result;
  213. }
  214. SKSL_FLOAT stod(const String& s) {
  215. double result;
  216. std::string str(s.c_str(), s.size());
  217. std::stringstream buffer(str);
  218. buffer.imbue(std::locale::classic());
  219. buffer >> result;
  220. SkASSERT(!buffer.fail());
  221. return result;
  222. }
  223. long stol(const String& s) {
  224. char* p;
  225. SkDEBUGCODE(errno = 0;)
  226. long result = strtoul(s.c_str(), &p, 0);
  227. SkASSERT(*p == 0);
  228. SkASSERT(!errno);
  229. return result;
  230. }
  231. } // namespace