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 | |
| 10 | namespace SkSL { |
| 11 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 12 | std::string to_string(double value) { |
| 13 | std::stringstream buffer; |
| 14 | buffer << std::setprecision(std::numeric_limits<double>::digits10) << value; |
| 15 | std::string result = buffer.str(); |
| 16 | if (result.find_last_of(".") == std::string::npos && |
| 17 | result.find_last_of("e") == std::string::npos) { |
| 18 | result += ".0"; |
| 19 | } |
| 20 | return result; |
| 21 | } |
| 22 | |
| 23 | std::string to_string(int32_t value) { |
| 24 | std::stringstream buffer; |
| 25 | buffer << value; |
| 26 | return buffer.str(); |
| 27 | } |
| 28 | |
| 29 | std::string to_string(uint32_t value) { |
| 30 | std::stringstream buffer; |
| 31 | buffer << value; |
| 32 | return buffer.str(); |
| 33 | } |
| 34 | |
| 35 | std::string to_string(int64_t value) { |
| 36 | std::stringstream buffer; |
| 37 | buffer << value; |
| 38 | return buffer.str(); |
| 39 | } |
| 40 | |
| 41 | std::string to_string(uint64_t value) { |
| 42 | std::stringstream buffer; |
| 43 | buffer << value; |
| 44 | return buffer.str(); |
| 45 | } |
| 46 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | int stoi(std::string s) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 48 | if (s.size() > 2 && s[0] == '0' && s[1] == 'x') { |
| 49 | char* p; |
| 50 | int result = strtoul(s.substr(2).c_str(), &p, 16); |
| 51 | ASSERT(*p == 0); |
| 52 | return result; |
| 53 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 54 | return atoi(s.c_str()); |
| 55 | } |
| 56 | |
| 57 | double stod(std::string s) { |
| 58 | return atof(s.c_str()); |
| 59 | } |
| 60 | |
| 61 | long stol(std::string s) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 62 | if (s.size() > 2 && s[0] == '0' && s[1] == 'x') { |
| 63 | char* p; |
| 64 | long result = strtoul(s.substr(2).c_str(), &p, 16); |
| 65 | ASSERT(*p == 0); |
| 66 | return result; |
| 67 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 68 | return atol(s.c_str()); |
| 69 | } |
| 70 | |
| 71 | void sksl_abort() { |
| 72 | #ifdef SKIA |
| 73 | sk_abort_no_print(); |
| 74 | exit(1); |
| 75 | #else |
| 76 | abort(); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | } // namespace |