• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkStringView.h"
12 #include "include/private/SkSLDefines.h"
13 #include <cstring>
14 #include <stdarg.h>
15 #include <string>
16 
17 #ifndef SKSL_STANDALONE
18 #include "include/core/SkString.h"
19 #endif
20 
21 namespace SkSL {
22 
23 class String;
24 
25 class SK_API String : public std::string {
26 public:
27     using std::string::string;
28 
String(std::string s)29     explicit String(std::string s) : INHERITED(std::move(s)) {}
String(skstd::string_view s)30     explicit String(skstd::string_view s) : INHERITED(s.data(), s.length()) {}
31     // TODO(johnstiles): add operator skstd::string_view
32 
33     static String printf(const char* fmt, ...) SK_PRINTF_LIKE(1, 2);
34     void appendf(const char* fmt, ...) SK_PRINTF_LIKE(2, 3);
35     void vappendf(const char* fmt, va_list va);
36 
starts_with(const char prefix[])37     bool starts_with(const char prefix[]) const {
38         return skstd::string_view(data(), size()).starts_with(prefix);
39     }
ends_with(const char suffix[])40     bool ends_with(const char suffix[]) const {
41         return skstd::string_view(data(), size()).ends_with(suffix);
42     }
43 
44     bool consumeSuffix(const char suffix[]);
45 
46     String operator+(const char* s) const;
47     String operator+(const String& s) const;
48     String operator+(skstd::string_view s) const;
49     String& operator+=(char c);
50     String& operator+=(const char* s);
51     String& operator+=(const String& s);
52     String& operator+=(skstd::string_view s);
53     friend String operator+(const char* s1, const String& s2);
54 
55 private:
56     using INHERITED = std::string;
57 };
58 
59 String operator+(skstd::string_view left, skstd::string_view right);
60 
61 String to_string(double value);
62 String to_string(int32_t value);
63 String to_string(uint32_t value);
64 String to_string(int64_t value);
65 String to_string(uint64_t value);
66 
67 bool stod(const skstd::string_view& s, SKSL_FLOAT* value);
68 bool stoi(const skstd::string_view& s, SKSL_INT* value);
69 
70 } // namespace SkSL
71 
72 namespace std {
73     template<> struct hash<SkSL::String> {
74         size_t operator()(const SkSL::String& s) const {
75             return hash<std::string>{}(s);
76         }
77     };
78 } // namespace std
79 
80 #endif
81