File: string_utils.cpp

package info (click to toggle)
jsonnet 0.20.0%2Bds-3.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,776 kB
  • sloc: cpp: 23,318; python: 1,788; javascript: 1,003; ansic: 885; sh: 745; makefile: 194; java: 140
file content (183 lines) | stat: -rw-r--r-- 6,242 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Copyright 2015 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <iomanip>

#include "static_error.h"
#include "string_utils.h"

namespace jsonnet::internal {

UString jsonnet_string_unparse(const UString &str, bool single)
{
    UStringStream ss;
    ss << (single ? U'\'' : U'\"');
    ss << jsonnet_string_escape(str, single);
    ss << (single ? U'\'' : U'\"');
    return ss.str();
}

UString jsonnet_string_escape(const UString &str, bool single)
{
    UStringStream ss;
    for (std::size_t i = 0; i < str.length(); ++i) {
        char32_t c = str[i];
        switch (c) {
            case U'\"': ss << (single ? U"\"" : U"\\\""); break;
            case U'\'': ss << (single ? U"\\\'" : U"\'"); break;
            case U'\\': ss << U"\\\\"; break;
            case U'\b': ss << U"\\b"; break;
            case U'\f': ss << U"\\f"; break;
            case U'\n': ss << U"\\n"; break;
            case U'\r': ss << U"\\r"; break;
            case U'\t': ss << U"\\t"; break;
            case U'\0': ss << U"\\u0000"; break;
            default: {
                if (c < 0x20 || (c >= 0x7f && c <= 0x9f)) {
                    // Unprintable, use \u
                    std::stringstream ss8;
                    ss8 << "\\u" << std::hex << std::setfill('0') << std::setw(4)
                        << (unsigned long)(c);
                    ss << decode_utf8(ss8.str());
                } else {
                    // Printable, write verbatim
                    ss << c;
                }
            }
        }
    }
    return ss.str();
}

unsigned long jsonnet_string_parse_unicode(const LocationRange &loc, const char32_t *c)
{
    unsigned long codepoint = 0;
    // Expect 4 hex digits.
    for (unsigned i = 0; i < 4; ++i) {
        auto x = (unsigned char)(c[i]);
        unsigned digit;
        if (x == '\0') {
            auto msg = "Truncated unicode escape sequence in string literal.";
            throw StaticError(loc, msg);
        } else if (x >= '0' && x <= '9') {
            digit = x - '0';
        } else if (x >= 'a' && x <= 'f') {
            digit = x - 'a' + 10;
        } else if (x >= 'A' && x <= 'F') {
            digit = x - 'A' + 10;
        } else {
            std::stringstream ss;
            ss << "Malformed unicode escape character, "
               << "should be hex: '" << x << "'";
            throw StaticError(loc, ss.str());
        }
        codepoint *= 16;
        codepoint += digit;
    }
    return codepoint;
}

bool is_bmp_codepoint(const unsigned long codepoint)
{
    return codepoint < 0xd800 || (codepoint >= 0xe000 && codepoint < 0x10000);
}

char32_t decode_utf16_surrogates(const LocationRange &loc, const unsigned long high, const unsigned long low)
{
    if (high >= 0xd800 && high < 0xdc00 && low >= 0xdc00 && low < 0xe000) {
        return 0x10000 + ((high & 0x03ff) << 10) + (low & 0x03ff);
    } else {
        std::stringstream ss;
        ss << "Invalid UTF-16 bytes";
        throw StaticError(loc, ss.str());
    }
}

UString jsonnet_string_unescape(const LocationRange &loc, const UString &s)
{
    UString r;
    const char32_t *s_ptr = s.c_str();
    for (const char32_t *c = s_ptr; *c != U'\0'; ++c) {
        switch (*c) {
            case '\\':
                switch (*(++c)) {
                    case '"':
                    case '\'': r += *c; break;

                    case '\\': r += *c; break;

                    case '/': r += *c; break;

                    case 'b': r += '\b'; break;

                    case 'f': r += '\f'; break;

                    case 'n': r += '\n'; break;

                    case 'r': r += '\r'; break;

                    case 't': r += '\t'; break;

                    case 'u': {
                        ++c;  // Consume the 'u'.
                        unsigned long codepoint = jsonnet_string_parse_unicode(loc, c);

                        // Leave us on the last char, ready for the ++c at
                        // the outer for loop.
                        c += 3;
                        if (!is_bmp_codepoint(codepoint)) {
                           if (*(++c) != '\\') {
                                std::stringstream ss;
                                ss << "Invalid non-BMP Unicode escape in string literal";
                                throw StaticError(loc, ss.str());
                           }
                           if (*(++c) != 'u') {
                                std::stringstream ss;
                                ss << "Invalid non-BMP Unicode escape in string literal";
                                throw StaticError(loc, ss.str());
                           }
                           ++c;
                           unsigned long codepoint2 = jsonnet_string_parse_unicode(loc, c);
                           c += 3;
                           codepoint = decode_utf16_surrogates(loc, codepoint, codepoint2);
                       }
                       r += codepoint;
                    } break;

                    case '\0': {
                        auto msg = "Truncated escape sequence in string literal.";
                        throw StaticError(loc, msg);
                    }

                    default: {
                        std::stringstream ss;
                        std::string utf8;
                        encode_utf8(*c, utf8);
                        ss << "Unknown escape sequence in string literal: '" << utf8 << "'";
                        throw StaticError(loc, ss.str());
                    }
                }
                break;

            default:
                // Just a regular letter.
                r += *c;
        }
    }
    return r;
}

}  // namespace jsonnet::internal