blob: 97b5179994ee16845ce9e9aef409898cb7e7f0d3 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 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#include "SkSLUtil.h"
9
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050010#include <cinttypes>
11
ethannicholasb3058bd2016-07-01 08:22:01 -070012namespace SkSL {
13
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050014SkString to_string(double value) {
15#ifdef SK_BUILD_FOR_WIN
16 #define SNPRINTF _snprintf
17#else
18 #define SNPRINTF snprintf
19#endif
20#define MAX_DOUBLE_CHARS 25
21 char buffer[MAX_DOUBLE_CHARS];
22 SkDEBUGCODE(int len = )SNPRINTF(buffer, sizeof(buffer), "%.17g", value);
23 ASSERT(len < MAX_DOUBLE_CHARS);
24 SkString result(buffer);
25 if (!strchr(buffer, '.') && !strchr(buffer, 'e')) {
ethannicholas5961bc92016-10-12 06:39:56 -070026 result += ".0";
27 }
28 return result;
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050029#undef SNPRINTF
30#undef MAX_DOUBLE_CHARS
ethannicholas5961bc92016-10-12 06:39:56 -070031}
32
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050033SkString to_string(int32_t value) {
34 return SkStringPrintf("%d", value);
ethannicholas5961bc92016-10-12 06:39:56 -070035}
36
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050037SkString to_string(uint32_t value) {
38 return SkStringPrintf("%u", value);
ethannicholas5961bc92016-10-12 06:39:56 -070039}
40
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050041SkString to_string(int64_t value) {
42 return SkStringPrintf("%" PRId64, value);
ethannicholas5961bc92016-10-12 06:39:56 -070043}
44
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050045SkString to_string(uint64_t value) {
46 return SkStringPrintf("%" PRIu64, value);
ethannicholas5961bc92016-10-12 06:39:56 -070047}
48
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050049int stoi(SkString s) {
ethannicholas5961bc92016-10-12 06:39:56 -070050 if (s.size() > 2 && s[0] == '0' && s[1] == 'x') {
51 char* p;
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050052 int result = strtoul(s.c_str() + 2, &p, 16);
ethannicholas5961bc92016-10-12 06:39:56 -070053 ASSERT(*p == 0);
54 return result;
55 }
ethannicholasb3058bd2016-07-01 08:22:01 -070056 return atoi(s.c_str());
57}
58
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050059double stod(SkString s) {
ethannicholasb3058bd2016-07-01 08:22:01 -070060 return atof(s.c_str());
61}
62
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050063long stol(SkString s) {
ethannicholas5961bc92016-10-12 06:39:56 -070064 if (s.size() > 2 && s[0] == '0' && s[1] == 'x') {
65 char* p;
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050066 long result = strtoul(s.c_str() + 2, &p, 16);
ethannicholas5961bc92016-10-12 06:39:56 -070067 ASSERT(*p == 0);
68 return result;
69 }
ethannicholasb3058bd2016-07-01 08:22:01 -070070 return atol(s.c_str());
71}
72
73void sksl_abort() {
74#ifdef SKIA
75 sk_abort_no_print();
76 exit(1);
77#else
78 abort();
79#endif
80}
81
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050082void write_data(const SkData& data, SkWStream& out) {
83 out.write(data.data(), data.size());
84}
85
86SkString operator+(const SkString& s, const char* c) {
87 SkString result(s);
88 result += c;
89 return result;
90}
91
92SkString operator+(const char* c, const SkString& s) {
93 SkString result(c);
94 result += s;
95 return result;
96}
97
98SkString operator+(const SkString& s1, const SkString& s2) {
99 SkString result(s1);
100 result += s2;
101 return result;
102}
103
104bool operator==(const SkString& s1, const char* s2) {
105 return !strcmp(s1.c_str(), s2);
106}
107
108bool operator!=(const SkString& s1, const char* s2) {
109 return strcmp(s1.c_str(), s2);
110}
111
112bool operator!=(const char* s1, const SkString& s2) {
113 return strcmp(s1, s2.c_str());
114}
ethannicholasb3058bd2016-07-01 08:22:01 -0700115} // namespace