ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 10 | #ifndef __STDC_FORMAT_MACROS |
| 11 | #define __STDC_FORMAT_MACROS |
| 12 | #endif |
| 13 | #include <cinttypes> |
| 14 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 15 | namespace SkSL { |
| 16 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 17 | SkString 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')) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 29 | result += ".0"; |
| 30 | } |
| 31 | return result; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 32 | #undef SNPRINTF |
| 33 | #undef MAX_DOUBLE_CHARS |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 36 | SkString to_string(int32_t value) { |
| 37 | return SkStringPrintf("%d", value); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 40 | SkString to_string(uint32_t value) { |
| 41 | return SkStringPrintf("%u", value); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 44 | SkString to_string(int64_t value) { |
| 45 | return SkStringPrintf("%" PRId64, value); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 48 | SkString to_string(uint64_t value) { |
| 49 | return SkStringPrintf("%" PRIu64, value); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 52 | int stoi(SkString s) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 53 | if (s.size() > 2 && s[0] == '0' && s[1] == 'x') { |
| 54 | char* p; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 55 | int result = strtoul(s.c_str() + 2, &p, 16); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 56 | ASSERT(*p == 0); |
| 57 | return result; |
| 58 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 59 | return atoi(s.c_str()); |
| 60 | } |
| 61 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 62 | double stod(SkString s) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 63 | return atof(s.c_str()); |
| 64 | } |
| 65 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 66 | long stol(SkString s) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 67 | if (s.size() > 2 && s[0] == '0' && s[1] == 'x') { |
| 68 | char* p; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 69 | long result = strtoul(s.c_str() + 2, &p, 16); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 70 | ASSERT(*p == 0); |
| 71 | return result; |
| 72 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | return atol(s.c_str()); |
| 74 | } |
| 75 | |
| 76 | void sksl_abort() { |
| 77 | #ifdef SKIA |
| 78 | sk_abort_no_print(); |
| 79 | exit(1); |
| 80 | #else |
| 81 | abort(); |
| 82 | #endif |
| 83 | } |
| 84 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 85 | void write_data(const SkData& data, SkWStream& out) { |
| 86 | out.write(data.data(), data.size()); |
| 87 | } |
| 88 | |
| 89 | SkString operator+(const SkString& s, const char* c) { |
| 90 | SkString result(s); |
| 91 | result += c; |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | SkString operator+(const char* c, const SkString& s) { |
| 96 | SkString result(c); |
| 97 | result += s; |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | SkString operator+(const SkString& s1, const SkString& s2) { |
| 102 | SkString result(s1); |
| 103 | result += s2; |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | bool operator==(const SkString& s1, const char* s2) { |
| 108 | return !strcmp(s1.c_str(), s2); |
| 109 | } |
| 110 | |
| 111 | bool operator!=(const SkString& s1, const char* s2) { |
| 112 | return strcmp(s1.c_str(), s2); |
| 113 | } |
| 114 | |
| 115 | bool operator!=(const char* s1, const SkString& s2) { |
| 116 | return strcmp(s1, s2.c_str()); |
| 117 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | } // namespace |