SkSLString.h 3.6 KB

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