Coverage Report

Created: 2026-10-17 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jsonnet/core/unicode.h
Line
Count
Source
1
/*
2
Copyright 2015 Google Inc. All rights reserved.
3
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
8
    http://www.apache.org/licenses/LICENSE-2.0
9
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
17
#ifndef JSONNET_UNICODE_H
18
#define JSONNET_UNICODE_H
19
20
/** Substituted when a unicode translation format encoding error is encountered. */
21
18.3M
#define JSONNET_CODEPOINT_ERROR 0xfffd
22
1.87G
#define JSONNET_CODEPOINT_MAX 0x110000
23
24
namespace jsonnet::internal {
25
26
/** Convert a unicode codepoint to UTF8.
27
 *
28
 * \param x The unicode codepoint.
29
 * \param s The UTF-8 string to append to.
30
 * \returns The number of characters appended.
31
 */
32
static inline int encode_utf8(char32_t x, std::string &s)
33
1.87G
{
34
1.87G
    if (x >= JSONNET_CODEPOINT_MAX)
35
4.39k
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
1.87G
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
1.87G
    if (x < 0x80) {
41
1.87G
        s.push_back((char)x);
42
1.87G
        return 1;
43
1.87G
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
51.4k
        bytes |= 0xC080;
45
51.4k
        s.push_back((bytes >> 8) & 0xFF);
46
51.4k
        s.push_back((bytes >> 0) & 0xFF);
47
51.4k
        return 2;
48
1.97M
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
1.95M
        bytes |= 0xE08080;
50
1.95M
        s.push_back((bytes >> 16) & 0xFF);
51
1.95M
        s.push_back((bytes >> 8) & 0xFF);
52
1.95M
        s.push_back((bytes >> 0) & 0xFF);
53
1.95M
        return 3;
54
1.95M
    } else if (x < 0x110000) {  // note that capital 'Z' bits must be 0
55
19.7k
        bytes |= 0xF0808080;
56
19.7k
        s.push_back((bytes >> 24) & 0xFF);
57
19.7k
        s.push_back((bytes >> 16) & 0xFF);
58
19.7k
        s.push_back((bytes >> 8) & 0xFF);
59
19.7k
        s.push_back((bytes >> 0) & 0xFF);
60
19.7k
        return 4;
61
19.7k
    } else {
62
0
        std::cerr << "Should never get here." << std::endl;
63
0
        abort();
64
0
    }
65
1.87G
}
Unexecuted instantiation: libjsonnet.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: parser.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: pass.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
static_analysis.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
33
24.6M
{
34
24.6M
    if (x >= JSONNET_CODEPOINT_MAX)
35
0
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
24.6M
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
24.6M
    if (x < 0x80) {
41
24.6M
        s.push_back((char)x);
42
24.6M
        return 1;
43
24.6M
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
0
        bytes |= 0xC080;
45
0
        s.push_back((bytes >> 8) & 0xFF);
46
0
        s.push_back((bytes >> 0) & 0xFF);
47
0
        return 2;
48
0
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
0
        bytes |= 0xE08080;
50
0
        s.push_back((bytes >> 16) & 0xFF);
51
0
        s.push_back((bytes >> 8) & 0xFF);
52
0
        s.push_back((bytes >> 0) & 0xFF);
53
0
        return 3;
54
0
    } else if (x < 0x110000) {  // note that capital 'Z' bits must be 0
55
0
        bytes |= 0xF0808080;
56
0
        s.push_back((bytes >> 24) & 0xFF);
57
0
        s.push_back((bytes >> 16) & 0xFF);
58
0
        s.push_back((bytes >> 8) & 0xFF);
59
0
        s.push_back((bytes >> 0) & 0xFF);
60
0
        return 4;
61
0
    } else {
62
0
        std::cerr << "Should never get here." << std::endl;
63
0
        abort();
64
0
    }
65
24.6M
}
string_utils.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
33
414
{
34
414
    if (x >= JSONNET_CODEPOINT_MAX)
35
5
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
414
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
414
    if (x < 0x80) {
41
213
        s.push_back((char)x);
42
213
        return 1;
43
213
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
31
        bytes |= 0xC080;
45
31
        s.push_back((bytes >> 8) & 0xFF);
46
31
        s.push_back((bytes >> 0) & 0xFF);
47
31
        return 2;
48
170
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
125
        bytes |= 0xE08080;
50
125
        s.push_back((bytes >> 16) & 0xFF);
51
125
        s.push_back((bytes >> 8) & 0xFF);
52
125
        s.push_back((bytes >> 0) & 0xFF);
53
125
        return 3;
54
125
    } else if (x < 0x110000) {  // note that capital 'Z' bits must be 0
55
45
        bytes |= 0xF0808080;
56
45
        s.push_back((bytes >> 24) & 0xFF);
57
45
        s.push_back((bytes >> 16) & 0xFF);
58
45
        s.push_back((bytes >> 8) & 0xFF);
59
45
        s.push_back((bytes >> 0) & 0xFF);
60
45
        return 4;
61
45
    } else {
62
0
        std::cerr << "Should never get here." << std::endl;
63
0
        abort();
64
0
    }
65
414
}
vm.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
33
1.84G
{
34
1.84G
    if (x >= JSONNET_CODEPOINT_MAX)
35
4.38k
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
1.84G
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
1.84G
    if (x < 0x80) {
41
1.84G
        s.push_back((char)x);
42
1.84G
        return 1;
43
1.84G
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
51.4k
        bytes |= 0xC080;
45
51.4k
        s.push_back((bytes >> 8) & 0xFF);
46
51.4k
        s.push_back((bytes >> 0) & 0xFF);
47
51.4k
        return 2;
48
1.97M
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
1.95M
        bytes |= 0xE08080;
50
1.95M
        s.push_back((bytes >> 16) & 0xFF);
51
1.95M
        s.push_back((bytes >> 8) & 0xFF);
52
1.95M
        s.push_back((bytes >> 0) & 0xFF);
53
1.95M
        return 3;
54
1.95M
    } else if (x < 0x110000) {  // note that capital 'Z' bits must be 0
55
19.6k
        bytes |= 0xF0808080;
56
19.6k
        s.push_back((bytes >> 24) & 0xFF);
57
19.6k
        s.push_back((bytes >> 16) & 0xFF);
58
19.6k
        s.push_back((bytes >> 8) & 0xFF);
59
19.6k
        s.push_back((bytes >> 0) & 0xFF);
60
19.6k
        return 4;
61
19.6k
    } else {
62
0
        std::cerr << "Should never get here." << std::endl;
63
0
        abort();
64
0
    }
65
1.84G
}
desugarer.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
33
6.01M
{
34
6.01M
    if (x >= JSONNET_CODEPOINT_MAX)
35
0
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
6.01M
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
6.01M
    if (x < 0x80) {
41
6.01M
        s.push_back((char)x);
42
6.01M
        return 1;
43
6.01M
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
0
        bytes |= 0xC080;
45
0
        s.push_back((bytes >> 8) & 0xFF);
46
0
        s.push_back((bytes >> 0) & 0xFF);
47
0
        return 2;
48
0
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
0
        bytes |= 0xE08080;
50
0
        s.push_back((bytes >> 16) & 0xFF);
51
0
        s.push_back((bytes >> 8) & 0xFF);
52
0
        s.push_back((bytes >> 0) & 0xFF);
53
0
        return 3;
54
0
    } else if (x < 0x110000) {  // note that capital 'Z' bits must be 0
55
0
        bytes |= 0xF0808080;
56
0
        s.push_back((bytes >> 24) & 0xFF);
57
0
        s.push_back((bytes >> 16) & 0xFF);
58
0
        s.push_back((bytes >> 8) & 0xFF);
59
0
        s.push_back((bytes >> 0) & 0xFF);
60
0
        return 4;
61
0
    } else {
62
0
        std::cerr << "Should never get here." << std::endl;
63
0
        abort();
64
0
    }
65
6.01M
}
Unexecuted instantiation: formatter.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: lexer.cpp:jsonnet::internal::encode_utf8(char32_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
66
67
/** Convert the UTF8 byte sequence in the given string to a unicode code point.
68
 *
69
 * \param str The string.
70
 * \param i The index of the string from which to start decoding and returns the index of the last
71
 *          byte of the encoded codepoint.
72
 * \returns The decoded unicode codepoint.
73
 */
74
static inline char32_t decode_utf8(const std::string &str, size_t &i)
75
812M
{
76
812M
    char c0 = str[i];
77
812M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
794M
        return c0;
79
794M
    } else if ((c0 & 0xE0) == 0xC0) {  // 110yyyxx 10xxxxxx
80
1.00M
        if (i + 1 >= str.length()) {
81
11.2k
            return JSONNET_CODEPOINT_ERROR;
82
11.2k
        }
83
997k
        char c1 = str[++i];
84
997k
        if ((c1 & 0xC0) != 0x80) {
85
839k
            return JSONNET_CODEPOINT_ERROR;
86
839k
        }
87
157k
        return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
88
17.6M
    } else if ((c0 & 0xF0) == 0xE0) {  // 1110yyyy 10yyyyxx 10xxxxxx
89
1.05M
        if (i + 2 >= str.length()) {
90
7.06k
            return JSONNET_CODEPOINT_ERROR;
91
7.06k
        }
92
1.04M
        char c1 = str[++i];
93
1.04M
        if ((c1 & 0xC0) != 0x80) {
94
931k
            return JSONNET_CODEPOINT_ERROR;
95
931k
        }
96
115k
        char c2 = str[++i];
97
115k
        if ((c2 & 0xC0) != 0x80) {
98
33.2k
            return JSONNET_CODEPOINT_ERROR;
99
33.2k
        }
100
81.7k
        return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
101
16.6M
    } else if ((c0 & 0xF8) == 0xF0) {  // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
102
345k
        if (i + 3 >= str.length()) {
103
6.39k
            return JSONNET_CODEPOINT_ERROR;
104
6.39k
        }
105
338k
        char c1 = str[++i];
106
338k
        if ((c1 & 0xC0) != 0x80) {
107
201k
            return JSONNET_CODEPOINT_ERROR;
108
201k
        }
109
136k
        char c2 = str[++i];
110
136k
        if ((c2 & 0xC0) != 0x80) {
111
11.7k
            return JSONNET_CODEPOINT_ERROR;
112
11.7k
        }
113
125k
        char c3 = str[++i];
114
125k
        if ((c3 & 0xC0) != 0x80) {
115
31.9k
            return JSONNET_CODEPOINT_ERROR;
116
31.9k
        }
117
93.2k
        return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
118
16.2M
    } else {
119
16.2M
        return JSONNET_CODEPOINT_ERROR;
120
16.2M
    }
121
812M
}
Unexecuted instantiation: libjsonnet.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
parser.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
Line
Count
Source
75
625M
{
76
625M
    char c0 = str[i];
77
625M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
607M
        return c0;
79
607M
    } else if ((c0 & 0xE0) == 0xC0) {  // 110yyyxx 10xxxxxx
80
1.00M
        if (i + 1 >= str.length()) {
81
11.2k
            return JSONNET_CODEPOINT_ERROR;
82
11.2k
        }
83
997k
        char c1 = str[++i];
84
997k
        if ((c1 & 0xC0) != 0x80) {
85
839k
            return JSONNET_CODEPOINT_ERROR;
86
839k
        }
87
157k
        return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
88
17.6M
    } else if ((c0 & 0xF0) == 0xE0) {  // 1110yyyy 10yyyyxx 10xxxxxx
89
1.05M
        if (i + 2 >= str.length()) {
90
7.06k
            return JSONNET_CODEPOINT_ERROR;
91
7.06k
        }
92
1.04M
        char c1 = str[++i];
93
1.04M
        if ((c1 & 0xC0) != 0x80) {
94
931k
            return JSONNET_CODEPOINT_ERROR;
95
931k
        }
96
115k
        char c2 = str[++i];
97
115k
        if ((c2 & 0xC0) != 0x80) {
98
33.2k
            return JSONNET_CODEPOINT_ERROR;
99
33.2k
        }
100
81.7k
        return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
101
16.6M
    } else if ((c0 & 0xF8) == 0xF0) {  // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
102
345k
        if (i + 3 >= str.length()) {
103
6.39k
            return JSONNET_CODEPOINT_ERROR;
104
6.39k
        }
105
338k
        char c1 = str[++i];
106
338k
        if ((c1 & 0xC0) != 0x80) {
107
201k
            return JSONNET_CODEPOINT_ERROR;
108
201k
        }
109
136k
        char c2 = str[++i];
110
136k
        if ((c2 & 0xC0) != 0x80) {
111
11.7k
            return JSONNET_CODEPOINT_ERROR;
112
11.7k
        }
113
125k
        char c3 = str[++i];
114
125k
        if ((c3 & 0xC0) != 0x80) {
115
31.9k
            return JSONNET_CODEPOINT_ERROR;
116
31.9k
        }
117
93.2k
        return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
118
16.2M
    } else {
119
16.2M
        return JSONNET_CODEPOINT_ERROR;
120
16.2M
    }
121
625M
}
Unexecuted instantiation: pass.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
Unexecuted instantiation: static_analysis.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
string_utils.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
Line
Count
Source
75
169M
{
76
169M
    char c0 = str[i];
77
169M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
169M
        return c0;
79
169M
    } else if ((c0 & 0xE0) == 0xC0) {  // 110yyyxx 10xxxxxx
80
0
        if (i + 1 >= str.length()) {
81
0
            return JSONNET_CODEPOINT_ERROR;
82
0
        }
83
0
        char c1 = str[++i];
84
0
        if ((c1 & 0xC0) != 0x80) {
85
0
            return JSONNET_CODEPOINT_ERROR;
86
0
        }
87
0
        return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
88
0
    } else if ((c0 & 0xF0) == 0xE0) {  // 1110yyyy 10yyyyxx 10xxxxxx
89
0
        if (i + 2 >= str.length()) {
90
0
            return JSONNET_CODEPOINT_ERROR;
91
0
        }
92
0
        char c1 = str[++i];
93
0
        if ((c1 & 0xC0) != 0x80) {
94
0
            return JSONNET_CODEPOINT_ERROR;
95
0
        }
96
0
        char c2 = str[++i];
97
0
        if ((c2 & 0xC0) != 0x80) {
98
0
            return JSONNET_CODEPOINT_ERROR;
99
0
        }
100
0
        return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
101
0
    } else if ((c0 & 0xF8) == 0xF0) {  // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
102
0
        if (i + 3 >= str.length()) {
103
0
            return JSONNET_CODEPOINT_ERROR;
104
0
        }
105
0
        char c1 = str[++i];
106
0
        if ((c1 & 0xC0) != 0x80) {
107
0
            return JSONNET_CODEPOINT_ERROR;
108
0
        }
109
0
        char c2 = str[++i];
110
0
        if ((c2 & 0xC0) != 0x80) {
111
0
            return JSONNET_CODEPOINT_ERROR;
112
0
        }
113
0
        char c3 = str[++i];
114
0
        if ((c3 & 0xC0) != 0x80) {
115
0
            return JSONNET_CODEPOINT_ERROR;
116
0
        }
117
0
        return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
118
0
    } else {
119
0
        return JSONNET_CODEPOINT_ERROR;
120
0
    }
121
169M
}
vm.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
Line
Count
Source
75
17.5M
{
76
17.5M
    char c0 = str[i];
77
17.5M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
17.5M
        return c0;
79
17.5M
    } else if ((c0 & 0xE0) == 0xC0) {  // 110yyyxx 10xxxxxx
80
0
        if (i + 1 >= str.length()) {
81
0
            return JSONNET_CODEPOINT_ERROR;
82
0
        }
83
0
        char c1 = str[++i];
84
0
        if ((c1 & 0xC0) != 0x80) {
85
0
            return JSONNET_CODEPOINT_ERROR;
86
0
        }
87
0
        return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
88
0
    } else if ((c0 & 0xF0) == 0xE0) {  // 1110yyyy 10yyyyxx 10xxxxxx
89
0
        if (i + 2 >= str.length()) {
90
0
            return JSONNET_CODEPOINT_ERROR;
91
0
        }
92
0
        char c1 = str[++i];
93
0
        if ((c1 & 0xC0) != 0x80) {
94
0
            return JSONNET_CODEPOINT_ERROR;
95
0
        }
96
0
        char c2 = str[++i];
97
0
        if ((c2 & 0xC0) != 0x80) {
98
0
            return JSONNET_CODEPOINT_ERROR;
99
0
        }
100
0
        return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
101
0
    } else if ((c0 & 0xF8) == 0xF0) {  // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
102
0
        if (i + 3 >= str.length()) {
103
0
            return JSONNET_CODEPOINT_ERROR;
104
0
        }
105
0
        char c1 = str[++i];
106
0
        if ((c1 & 0xC0) != 0x80) {
107
0
            return JSONNET_CODEPOINT_ERROR;
108
0
        }
109
0
        char c2 = str[++i];
110
0
        if ((c2 & 0xC0) != 0x80) {
111
0
            return JSONNET_CODEPOINT_ERROR;
112
0
        }
113
0
        char c3 = str[++i];
114
0
        if ((c3 & 0xC0) != 0x80) {
115
0
            return JSONNET_CODEPOINT_ERROR;
116
0
        }
117
0
        return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
118
0
    } else {
119
0
        return JSONNET_CODEPOINT_ERROR;
120
0
    }
121
17.5M
}
desugarer.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
Line
Count
Source
75
116k
{
76
116k
    char c0 = str[i];
77
116k
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
116k
        return c0;
79
116k
    } else if ((c0 & 0xE0) == 0xC0) {  // 110yyyxx 10xxxxxx
80
0
        if (i + 1 >= str.length()) {
81
0
            return JSONNET_CODEPOINT_ERROR;
82
0
        }
83
0
        char c1 = str[++i];
84
0
        if ((c1 & 0xC0) != 0x80) {
85
0
            return JSONNET_CODEPOINT_ERROR;
86
0
        }
87
0
        return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
88
0
    } else if ((c0 & 0xF0) == 0xE0) {  // 1110yyyy 10yyyyxx 10xxxxxx
89
0
        if (i + 2 >= str.length()) {
90
0
            return JSONNET_CODEPOINT_ERROR;
91
0
        }
92
0
        char c1 = str[++i];
93
0
        if ((c1 & 0xC0) != 0x80) {
94
0
            return JSONNET_CODEPOINT_ERROR;
95
0
        }
96
0
        char c2 = str[++i];
97
0
        if ((c2 & 0xC0) != 0x80) {
98
0
            return JSONNET_CODEPOINT_ERROR;
99
0
        }
100
0
        return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
101
0
    } else if ((c0 & 0xF8) == 0xF0) {  // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
102
0
        if (i + 3 >= str.length()) {
103
0
            return JSONNET_CODEPOINT_ERROR;
104
0
        }
105
0
        char c1 = str[++i];
106
0
        if ((c1 & 0xC0) != 0x80) {
107
0
            return JSONNET_CODEPOINT_ERROR;
108
0
        }
109
0
        char c2 = str[++i];
110
0
        if ((c2 & 0xC0) != 0x80) {
111
0
            return JSONNET_CODEPOINT_ERROR;
112
0
        }
113
0
        char c3 = str[++i];
114
0
        if ((c3 & 0xC0) != 0x80) {
115
0
            return JSONNET_CODEPOINT_ERROR;
116
0
        }
117
0
        return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
118
0
    } else {
119
0
        return JSONNET_CODEPOINT_ERROR;
120
0
    }
121
116k
}
Unexecuted instantiation: formatter.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
Unexecuted instantiation: lexer.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long&)
122
123
/** A string class capable of holding unicode codepoints. */
124
typedef std::basic_string<char32_t> UString;
125
126
static inline void encode_utf8(const UString &s, std::string &r)
127
3.53M
{
128
3.53M
    for (char32_t cp : s)
129
1.87G
        encode_utf8(cp, r);
130
3.53M
}
Unexecuted instantiation: libjsonnet.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: parser.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: pass.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
static_analysis.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
127
1.75k
{
128
1.75k
    for (char32_t cp : s)
129
24.6M
        encode_utf8(cp, r);
130
1.75k
}
Unexecuted instantiation: string_utils.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
vm.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
127
2.62M
{
128
2.62M
    for (char32_t cp : s)
129
1.84G
        encode_utf8(cp, r);
130
2.62M
}
desugarer.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Line
Count
Source
127
898k
{
128
898k
    for (char32_t cp : s)
129
6.01M
        encode_utf8(cp, r);
130
898k
}
Unexecuted instantiation: formatter.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
Unexecuted instantiation: lexer.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
131
132
static inline std::string encode_utf8(const UString &s)
133
3.53M
{
134
3.53M
    std::string r;
135
3.53M
    encode_utf8(s, r);
136
3.53M
    return r;
137
3.53M
}
Unexecuted instantiation: libjsonnet.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
Unexecuted instantiation: parser.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
Unexecuted instantiation: pass.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
static_analysis.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
Line
Count
Source
133
1.75k
{
134
1.75k
    std::string r;
135
1.75k
    encode_utf8(s, r);
136
1.75k
    return r;
137
1.75k
}
Unexecuted instantiation: string_utils.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
vm.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
Line
Count
Source
133
2.62M
{
134
2.62M
    std::string r;
135
2.62M
    encode_utf8(s, r);
136
2.62M
    return r;
137
2.62M
}
desugarer.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
Line
Count
Source
133
898k
{
134
898k
    std::string r;
135
898k
    encode_utf8(s, r);
136
898k
    return r;
137
898k
}
Unexecuted instantiation: formatter.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
Unexecuted instantiation: lexer.cpp:jsonnet::internal::encode_utf8(std::__1::basic_string<char32_t, std::__1::char_traits<char32_t>, std::__1::allocator<char32_t> > const&)
138
139
static inline UString decode_utf8(const std::string &s)
140
142M
{
141
142M
    UString r;
142
955M
    for (size_t i = 0; i < s.length(); ++i)
143
812M
        r.push_back(decode_utf8(s, i));
144
142M
    return r;
145
142M
}
Unexecuted instantiation: libjsonnet.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
parser.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
140
104M
{
141
104M
    UString r;
142
730M
    for (size_t i = 0; i < s.length(); ++i)
143
625M
        r.push_back(decode_utf8(s, i));
144
104M
    return r;
145
104M
}
Unexecuted instantiation: pass.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: static_analysis.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
string_utils.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
140
28.2M
{
141
28.2M
    UString r;
142
197M
    for (size_t i = 0; i < s.length(); ++i)
143
169M
        r.push_back(decode_utf8(s, i));
144
28.2M
    return r;
145
28.2M
}
vm.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
140
10.2M
{
141
10.2M
    UString r;
142
27.8M
    for (size_t i = 0; i < s.length(); ++i)
143
17.5M
        r.push_back(decode_utf8(s, i));
144
10.2M
    return r;
145
10.2M
}
desugarer.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
140
21.3k
{
141
21.3k
    UString r;
142
138k
    for (size_t i = 0; i < s.length(); ++i)
143
116k
        r.push_back(decode_utf8(s, i));
144
21.3k
    return r;
145
21.3k
}
Unexecuted instantiation: formatter.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: lexer.cpp:jsonnet::internal::decode_utf8(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
146
147
/** A stringstream-like class capable of holding unicode codepoints.
148
 * The C++ standard does not support std::basic_stringstream<char32_t.
149
 */
150
class UStringStream {
151
    UString buf;
152
153
   public:
154
    UStringStream &operator<<(const UString &s)
155
63.8M
    {
156
63.8M
        buf.append(s);
157
63.8M
        return *this;
158
63.8M
    }
159
    UStringStream &operator<<(const char32_t *s)
160
2.60G
    {
161
2.60G
        buf.append(s);
162
2.60G
        return *this;
163
2.60G
    }
164
    UStringStream &operator<<(char32_t c)
165
114M
    {
166
114M
        buf.push_back(c);
167
114M
        return *this;
168
114M
    }
169
    template <class T>
170
    UStringStream &operator<<(T c)
171
3.09M
    {
172
3.09M
        std::stringstream ss;
173
3.09M
        ss << c;
174
3.09M
        for (char c : ss.str())
175
6.14M
            buf.push_back(char32_t(c));
176
3.09M
        return *this;
177
3.09M
    }
jsonnet::internal::UStringStream& jsonnet::internal::UStringStream::operator<< <int>(int)
Line
Count
Source
171
2.54M
    {
172
2.54M
        std::stringstream ss;
173
2.54M
        ss << c;
174
2.54M
        for (char c : ss.str())
175
4.28M
            buf.push_back(char32_t(c));
176
2.54M
        return *this;
177
2.54M
    }
jsonnet::internal::UStringStream& jsonnet::internal::UStringStream::operator<< <unsigned int>(unsigned int)
Line
Count
Source
171
552k
    {
172
552k
        std::stringstream ss;
173
552k
        ss << c;
174
552k
        for (char c : ss.str())
175
1.85M
            buf.push_back(char32_t(c));
176
552k
        return *this;
177
552k
    }
178
    UString str()
179
18.1M
    {
180
18.1M
        return buf;
181
18.1M
    }
182
};
183
184
}  // namespace jsonnet::internal
185
186
#endif  // JSONNET_UNICODE_H