blob: 98e5fa45997e87b7e4a362c2876007dfb96fffe0 [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 Nicholas9e1138d2016-11-21 10:39:35 -050010#ifndef __STDC_FORMAT_MACROS
11#define __STDC_FORMAT_MACROS
12#endif
13#include <cinttypes>
14
ethannicholasb3058bd2016-07-01 08:22:01 -070015namespace SkSL {
16
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050017SkString to_string(double value) {
18#ifdef SK_BUILD_FOR_WIN
19 #define SNPRINTF _snprintf
20#else
21 #define SNPRINTF snprintf
22#endif
23#define MAX_DOUBLE_CHARS 25
24 char buffer[MAX_DOUBLE_CHARS];
25 SkDEBUGCODE(int len = )SNPRINTF(buffer, sizeof(buffer), "%.17g", value);
26 ASSERT(len < MAX_DOUBLE_CHARS);
27 SkString result(buffer);
28 if (!strchr(buffer, '.') && !strchr(buffer, 'e')) {
ethannicholas5961bc92016-10-12 06:39:56 -070029 result += ".0";
30 }
31 return result;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050032#undef SNPRINTF
33#undef MAX_DOUBLE_CHARS
ethannicholas5961bc92016-10-12 06:39:56 -070034}
35
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050036SkString to_string(int32_t value) {
37 return SkStringPrintf("%d", value);
ethannicholas5961bc92016-10-12 06:39:56 -070038}
39
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050040SkString to_string(uint32_t value) {
41 return SkStringPrintf("%u", value);
ethannicholas5961bc92016-10-12 06:39:56 -070042}
43
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050044SkString to_string(int64_t value) {
45 return SkStringPrintf("%" PRId64, value);
ethannicholas5961bc92016-10-12 06:39:56 -070046}
47
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050048SkString to_string(uint64_t value) {
49 return SkStringPrintf("%" PRIu64, value);
ethannicholas5961bc92016-10-12 06:39:56 -070050}
51
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050052int stoi(SkString s) {
ethannicholas5961bc92016-10-12 06:39:56 -070053 if (s.size() > 2 && s[0] == '0' && s[1] == 'x') {
54 char* p;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050055 int result = strtoul(s.c_str() + 2, &p, 16);
ethannicholas5961bc92016-10-12 06:39:56 -070056 ASSERT(*p == 0);
57 return result;
58 }
ethannicholasb3058bd2016-07-01 08:22:01 -070059 return atoi(s.c_str());
60}
61
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050062double stod(SkString s) {
ethannicholasb3058bd2016-07-01 08:22:01 -070063 return atof(s.c_str());
64}
65
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050066long stol(SkString s) {
ethannicholas5961bc92016-10-12 06:39:56 -070067 if (s.size() > 2 && s[0] == '0' && s[1] == 'x') {
68 char* p;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050069 long result = strtoul(s.c_str() + 2, &p, 16);
ethannicholas5961bc92016-10-12 06:39:56 -070070 ASSERT(*p == 0);
71 return result;
72 }
ethannicholasb3058bd2016-07-01 08:22:01 -070073 return atol(s.c_str());
74}
75
76void sksl_abort() {
77#ifdef SKIA
78 sk_abort_no_print();
79 exit(1);
80#else
81 abort();
82#endif
83}
84
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050085void write_data(const SkData& data, SkWStream& out) {
86 out.write(data.data(), data.size());
87}
88
89SkString operator+(const SkString& s, const char* c) {
90 SkString result(s);
91 result += c;
92 return result;
93}
94
95SkString operator+(const char* c, const SkString& s) {
96 SkString result(c);
97 result += s;
98 return result;
99}
100
101SkString operator+(const SkString& s1, const SkString& s2) {
102 SkString result(s1);
103 result += s2;
104 return result;
105}
106
107bool operator==(const SkString& s1, const char* s2) {
108 return !strcmp(s1.c_str(), s2);
109}
110
111bool operator!=(const SkString& s1, const char* s2) {
112 return strcmp(s1.c_str(), s2);
113}
114
115bool operator!=(const char* s1, const SkString& s2) {
116 return strcmp(s1, s2.c_str());
117}
ethannicholasb3058bd2016-07-01 08:22:01 -0700118} // namespace