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/SkTypes.h"
12 #include "include/private/SkSLDefines.h"
13
14 #include <stdarg.h>
15 #include <string>
16 #include <string_view>
17
18 namespace SkSL {
19
20 bool stod(std::string_view s, SKSL_FLOAT* value);
21 bool stoi(std::string_view s, SKSL_INT* value);
22
23 namespace String {
24
25 std::string printf(const char* fmt, ...) SK_PRINTF_LIKE(1, 2);
26 void appendf(std::string* str, const char* fmt, ...) SK_PRINTF_LIKE(2, 3);
27 void vappendf(std::string* str, const char* fmt, va_list va) SK_PRINTF_LIKE(2, 0);
28
Separator()29 inline auto Separator() {
30 // This returns a lambda which emits "" the first time it is called, and ", " every subsequent
31 // time it is called.
32 struct Output {
33 const std::string fSpace, fComma;
34 };
35 static const Output* kOutput = new Output{{}, {", "}};
36
37 return [firstSeparator = true]() mutable -> const std::string& {
38 if (firstSeparator) {
39 firstSeparator = false;
40 return kOutput->fSpace;
41 } else {
42 return kOutput->fComma;
43 }
44 };
45 }
46
47 } // namespace String
48 } // namespace SkSL
49
50 namespace skstd {
51
52 // We use a custom to_string(float|double) which ignores locale settings and writes `1.0` instead
53 // of `1.00000`.
54 std::string to_string(float value);
55 std::string to_string(double value);
56
57 } // namespace skstd
58
59 #endif
60