Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 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 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 8 | #include "include/private/SkSLString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/sksl/SkSLUtil.h" |
Brian Osman | de7220f | 2018-02-09 09:32:36 -0500 | [diff] [blame] | 10 | #include <algorithm> |
John Stiles | 70e51e6 | 2021-02-17 20:43:54 -0500 | [diff] [blame] | 11 | #include <cinttypes> |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 12 | #include <errno.h> |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | #include <locale> |
| 15 | #include <sstream> |
| 16 | #include <string> |
| 17 | |
| 18 | namespace SkSL { |
| 19 | |
| 20 | String String::printf(const char* fmt, ...) { |
| 21 | va_list args; |
| 22 | va_start(args, fmt); |
| 23 | String result; |
| 24 | result.vappendf(fmt, args); |
z102.zhang | d74f2c8 | 2018-08-10 09:08:47 +0800 | [diff] [blame] | 25 | va_end(args); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 26 | return result; |
| 27 | } |
| 28 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 29 | void String::appendf(const char* fmt, ...) { |
| 30 | va_list args; |
| 31 | va_start(args, fmt); |
| 32 | this->vappendf(fmt, args); |
z102.zhang | d74f2c8 | 2018-08-10 09:08:47 +0800 | [diff] [blame] | 33 | va_end(args); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 34 | } |
Michael Ludwig | e84fb19 | 2018-08-21 16:32:20 -0400 | [diff] [blame] | 35 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 36 | void String::vappendf(const char* fmt, va_list args) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 37 | #define BUFFER_SIZE 256 |
| 38 | char buffer[BUFFER_SIZE]; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 39 | va_list reuse; |
| 40 | va_copy(reuse, args); |
Brian Osman | 061a5cf | 2020-06-24 14:50:25 -0400 | [diff] [blame] | 41 | size_t size = vsnprintf(buffer, BUFFER_SIZE, fmt, args); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 42 | if (BUFFER_SIZE >= size + 1) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 43 | this->append(buffer, size); |
| 44 | } else { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 45 | auto newBuffer = std::unique_ptr<char[]>(new char[size + 1]); |
Brian Osman | 061a5cf | 2020-06-24 14:50:25 -0400 | [diff] [blame] | 46 | vsnprintf(newBuffer.get(), size + 1, fmt, reuse); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 47 | this->append(newBuffer.get(), size); |
| 48 | } |
z102.zhang | d74f2c8 | 2018-08-10 09:08:47 +0800 | [diff] [blame] | 49 | va_end(reuse); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 50 | } |
| 51 | |
John Stiles | 72664be | 2020-09-16 17:43:11 -0400 | [diff] [blame] | 52 | bool String::consumeSuffix(const char suffix[]) { |
| 53 | size_t suffixLength = strlen(suffix); |
| 54 | if (this->length() < suffixLength) { |
| 55 | return false; |
| 56 | } |
| 57 | if (0 != strncmp(this->data() + this->size() - suffixLength, suffix, suffixLength)) { |
| 58 | return false; |
| 59 | } |
| 60 | this->resize(this->length() - suffixLength); |
| 61 | return true; |
| 62 | } |
| 63 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 64 | String String::operator+(const char* s) const { |
| 65 | String result(*this); |
| 66 | result.append(s); |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | String String::operator+(const String& s) const { |
| 71 | String result(*this); |
| 72 | result.append(s); |
| 73 | return result; |
| 74 | } |
| 75 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 76 | String String::operator+(skstd::string_view s) const { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 77 | String result(*this); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 78 | result.append(s.data(), s.length()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 79 | return result; |
| 80 | } |
| 81 | |
| 82 | String& String::operator+=(char c) { |
| 83 | INHERITED::operator+=(c); |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | String& String::operator+=(const char* s) { |
| 88 | INHERITED::operator+=(s); |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | String& String::operator+=(const String& s) { |
| 93 | INHERITED::operator+=(s); |
| 94 | return *this; |
| 95 | } |
| 96 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 97 | String& String::operator+=(skstd::string_view s) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 98 | this->append(s.data(), s.length()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 99 | return *this; |
| 100 | } |
| 101 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 102 | String operator+(const char* s1, const String& s2) { |
| 103 | String result(s1); |
| 104 | result.append(s2); |
| 105 | return result; |
| 106 | } |
| 107 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 108 | String operator+(skstd::string_view left, skstd::string_view right) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 109 | return String(left) + right; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 112 | String to_string(int32_t value) { |
John Stiles | c8bcef1 | 2021-03-18 12:45:15 -0400 | [diff] [blame] | 113 | return SkSL::String(std::to_string(value)); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | String to_string(uint32_t value) { |
John Stiles | c8bcef1 | 2021-03-18 12:45:15 -0400 | [diff] [blame] | 117 | return SkSL::String(std::to_string(value)); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | String to_string(int64_t value) { |
John Stiles | c8bcef1 | 2021-03-18 12:45:15 -0400 | [diff] [blame] | 121 | return SkSL::String(std::to_string(value)); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | String to_string(uint64_t value) { |
John Stiles | c8bcef1 | 2021-03-18 12:45:15 -0400 | [diff] [blame] | 125 | return SkSL::String(std::to_string(value)); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | String to_string(double value) { |
Ethan Nicholas | 2f103f0 | 2019-02-25 10:58:48 -0500 | [diff] [blame] | 129 | std::stringstream buffer; |
| 130 | buffer.imbue(std::locale::classic()); |
| 131 | buffer.precision(17); |
| 132 | buffer << value; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 133 | bool needsDotZero = true; |
Ethan Nicholas | 2f103f0 | 2019-02-25 10:58:48 -0500 | [diff] [blame] | 134 | const std::string str = buffer.str(); |
| 135 | for (int i = str.size() - 1; i >= 0; --i) { |
| 136 | char c = str[i]; |
| 137 | if (c == '.' || c == 'e') { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 138 | needsDotZero = false; |
| 139 | break; |
| 140 | } |
| 141 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 142 | if (needsDotZero) { |
Ethan Nicholas | 2f103f0 | 2019-02-25 10:58:48 -0500 | [diff] [blame] | 143 | buffer << ".0"; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 144 | } |
Ethan Nicholas | 2f103f0 | 2019-02-25 10:58:48 -0500 | [diff] [blame] | 145 | return String(buffer.str().c_str()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 148 | bool stod(const skstd::string_view& s, SKSL_FLOAT* value) { |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 149 | std::string str(s.data(), s.size()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 150 | std::stringstream buffer(str); |
| 151 | buffer.imbue(std::locale::classic()); |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 152 | buffer >> *value; |
| 153 | return !buffer.fail(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 156 | bool stoi(const skstd::string_view& s, SKSL_INT* value) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 157 | char* p; |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 158 | errno = 0; |
| 159 | unsigned long long result = strtoull(s.begin(), &p, /*base=*/0); |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 160 | *value = static_cast<SKSL_INT>(result); |
| 161 | return p == s.end() && errno == 0 && result <= 0xFFFFFFFF; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 162 | } |
| 163 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 164 | } // namespace SkSL |