aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/SkSLUtil.cpp
blob: 97b5179994ee16845ce9e9aef409898cb7e7f0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkSLUtil.h"

#include <cinttypes>

namespace SkSL {

SkString to_string(double value) {
#ifdef SK_BUILD_FOR_WIN
    #define SNPRINTF    _snprintf
#else
    #define SNPRINTF    snprintf
#endif
#define MAX_DOUBLE_CHARS 25
    char buffer[MAX_DOUBLE_CHARS];
    SkDEBUGCODE(int len = )SNPRINTF(buffer, sizeof(buffer), "%.17g", value);
    ASSERT(len < MAX_DOUBLE_CHARS);
    SkString result(buffer);
    if (!strchr(buffer, '.') && !strchr(buffer, 'e')) {
        result += ".0";
    }
    return result;
#undef SNPRINTF
#undef MAX_DOUBLE_CHARS
}

SkString to_string(int32_t value) {
    return SkStringPrintf("%d", value);
}

SkString to_string(uint32_t value) {
    return SkStringPrintf("%u", value);
}

SkString to_string(int64_t value) {
    return SkStringPrintf("%" PRId64, value);
}

SkString to_string(uint64_t value) {
    return SkStringPrintf("%" PRIu64, value);
}

int stoi(SkString s) {
    if (s.size() > 2 && s[0] == '0' && s[1] == 'x') {
        char* p;
        int result = strtoul(s.c_str() + 2, &p, 16);
        ASSERT(*p == 0);
        return result;
    }
    return atoi(s.c_str());
}

double stod(SkString s) {
    return atof(s.c_str());
}

long stol(SkString s) {
    if (s.size() > 2 && s[0] == '0' && s[1] == 'x') {
        char* p;
        long result = strtoul(s.c_str() + 2, &p, 16);
        ASSERT(*p == 0);
        return result;
    }
    return atol(s.c_str());
}

void sksl_abort() {
#ifdef SKIA
    sk_abort_no_print();
    exit(1);
#else
    abort();
#endif
}

void write_data(const SkData& data, SkWStream& out) {
    out.write(data.data(), data.size());
}

SkString operator+(const SkString& s, const char* c) {
    SkString result(s);
    result += c;
    return result;
}

SkString operator+(const char* c, const SkString& s) {
    SkString result(c);
    result += s;
    return result;
}

SkString operator+(const SkString& s1, const SkString& s2) {
    SkString result(s1);
    result += s2;
    return result;
}

bool operator==(const SkString& s1, const char* s2) {
    return !strcmp(s1.c_str(), s2);
}

bool operator!=(const SkString& s1, const char* s2) {
    return strcmp(s1.c_str(), s2);
}

bool operator!=(const char* s1, const SkString& s2) {
    return strcmp(s1, s2.c_str());
}
} // namespace