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