Coverage Report

Created: 2026-08-13 06:16

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
14.2M
#define JSONNET_CODEPOINT_ERROR 0xfffd
22
1.56G
#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.56G
{
34
1.56G
    if (x >= JSONNET_CODEPOINT_MAX)
35
2.31k
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
1.56G
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
1.56G
    if (x < 0x80) {
41
1.56G
        s.push_back((char)x);
42
1.56G
        return 1;
43
1.56G
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
61.3k
        bytes |= 0xC080;
45
61.3k
        s.push_back((bytes >> 8) & 0xFF);
46
61.3k
        s.push_back((bytes >> 0) & 0xFF);
47
61.3k
        return 2;
48
2.33M
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
2.31M
        bytes |= 0xE08080;
50
2.31M
        s.push_back((bytes >> 16) & 0xFF);
51
2.31M
        s.push_back((bytes >> 8) & 0xFF);
52
2.31M
        s.push_back((bytes >> 0) & 0xFF);
53
2.31M
        return 3;
54
2.31M
    } 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.56G
}
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
21.5M
{
34
21.5M
    if (x >= JSONNET_CODEPOINT_MAX)
35
0
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
21.5M
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
21.5M
    if (x < 0x80) {
41
21.5M
        s.push_back((char)x);
42
21.5M
        return 1;
43
21.5M
    } 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
21.5M
}
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
369
{
34
369
    if (x >= JSONNET_CODEPOINT_MAX)
35
5
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
369
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
369
    if (x < 0x80) {
41
189
        s.push_back((char)x);
42
189
        return 1;
43
189
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
33
        bytes |= 0xC080;
45
33
        s.push_back((bytes >> 8) & 0xFF);
46
33
        s.push_back((bytes >> 0) & 0xFF);
47
33
        return 2;
48
147
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
113
        bytes |= 0xE08080;
50
113
        s.push_back((bytes >> 16) & 0xFF);
51
113
        s.push_back((bytes >> 8) & 0xFF);
52
113
        s.push_back((bytes >> 0) & 0xFF);
53
113
        return 3;
54
113
    } else if (x < 0x110000) {  // note that capital 'Z' bits must be 0
55
34
        bytes |= 0xF0808080;
56
34
        s.push_back((bytes >> 24) & 0xFF);
57
34
        s.push_back((bytes >> 16) & 0xFF);
58
34
        s.push_back((bytes >> 8) & 0xFF);
59
34
        s.push_back((bytes >> 0) & 0xFF);
60
34
        return 4;
61
34
    } else {
62
0
        std::cerr << "Should never get here." << std::endl;
63
0
        abort();
64
0
    }
65
369
}
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.53G
{
34
1.53G
    if (x >= JSONNET_CODEPOINT_MAX)
35
2.31k
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
1.53G
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
1.53G
    if (x < 0x80) {
41
1.53G
        s.push_back((char)x);
42
1.53G
        return 1;
43
1.53G
    } else if (x < 0x800) {  // note that capital 'Y' bits must be 0
44
61.3k
        bytes |= 0xC080;
45
61.3k
        s.push_back((bytes >> 8) & 0xFF);
46
61.3k
        s.push_back((bytes >> 0) & 0xFF);
47
61.3k
        return 2;
48
2.33M
    } else if (x < 0x10000) {  // note that 'z' bits must be 0
49
2.31M
        bytes |= 0xE08080;
50
2.31M
        s.push_back((bytes >> 16) & 0xFF);
51
2.31M
        s.push_back((bytes >> 8) & 0xFF);
52
2.31M
        s.push_back((bytes >> 0) & 0xFF);
53
2.31M
        return 3;
54
2.31M
    } else if (x < 0x110000) {  // note that capital 'Z' bits must be 0
55
19.5k
        bytes |= 0xF0808080;
56
19.5k
        s.push_back((bytes >> 24) & 0xFF);
57
19.5k
        s.push_back((bytes >> 16) & 0xFF);
58
19.5k
        s.push_back((bytes >> 8) & 0xFF);
59
19.5k
        s.push_back((bytes >> 0) & 0xFF);
60
19.5k
        return 4;
61
19.5k
    } else {
62
0
        std::cerr << "Should never get here." << std::endl;
63
0
        abort();
64
0
    }
65
1.53G
}
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
5.65M
{
34
5.65M
    if (x >= JSONNET_CODEPOINT_MAX)
35
0
        x = JSONNET_CODEPOINT_ERROR;
36
37
    // 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
38
5.65M
    long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
39
40
5.65M
    if (x < 0x80) {
41
5.65M
        s.push_back((char)x);
42
5.65M
        return 1;
43
5.65M
    } 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
5.65M
}
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
760M
{
76
760M
    char c0 = str[i];
77
760M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
746M
        return c0;
79
746M
    } else if ((c0 & 0xE0) == 0xC0) {  // 110yyyxx 10xxxxxx
80
963k
        if (i + 1 >= str.length()) {
81
10.8k
            return JSONNET_CODEPOINT_ERROR;
82
10.8k
        }
83
952k
        char c1 = str[++i];
84
952k
        if ((c1 & 0xC0) != 0x80) {
85
769k
            return JSONNET_CODEPOINT_ERROR;
86
769k
        }
87
183k
        return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
88
13.6M
    } else if ((c0 & 0xF0) == 0xE0) {  // 1110yyyy 10yyyyxx 10xxxxxx
89
1.16M
        if (i + 2 >= str.length()) {
90
6.96k
            return JSONNET_CODEPOINT_ERROR;
91
6.96k
        }
92
1.15M
        char c1 = str[++i];
93
1.15M
        if ((c1 & 0xC0) != 0x80) {
94
1.03M
            return JSONNET_CODEPOINT_ERROR;
95
1.03M
        }
96
123k
        char c2 = str[++i];
97
123k
        if ((c2 & 0xC0) != 0x80) {
98
30.7k
            return JSONNET_CODEPOINT_ERROR;
99
30.7k
        }
100
92.5k
        return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
101
12.4M
    } else if ((c0 & 0xF8) == 0xF0) {  // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
102
367k
        if (i + 3 >= str.length()) {
103
7.12k
            return JSONNET_CODEPOINT_ERROR;
104
7.12k
        }
105
360k
        char c1 = str[++i];
106
360k
        if ((c1 & 0xC0) != 0x80) {
107
217k
            return JSONNET_CODEPOINT_ERROR;
108
217k
        }
109
142k
        char c2 = str[++i];
110
142k
        if ((c2 & 0xC0) != 0x80) {
111
11.4k
            return JSONNET_CODEPOINT_ERROR;
112
11.4k
        }
113
130k
        char c3 = str[++i];
114
130k
        if ((c3 & 0xC0) != 0x80) {
115
36.1k
            return JSONNET_CODEPOINT_ERROR;
116
36.1k
        }
117
94.5k
        return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
118
12.0M
    } else {
119
12.0M
        return JSONNET_CODEPOINT_ERROR;
120
12.0M
    }
121
760M
}
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
588M
{
76
588M
    char c0 = str[i];
77
588M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
573M
        return c0;
79
573M
    } else if ((c0 & 0xE0) == 0xC0) {  // 110yyyxx 10xxxxxx
80
963k
        if (i + 1 >= str.length()) {
81
10.8k
            return JSONNET_CODEPOINT_ERROR;
82
10.8k
        }
83
952k
        char c1 = str[++i];
84
952k
        if ((c1 & 0xC0) != 0x80) {
85
769k
            return JSONNET_CODEPOINT_ERROR;
86
769k
        }
87
183k
        return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
88
13.6M
    } else if ((c0 & 0xF0) == 0xE0) {  // 1110yyyy 10yyyyxx 10xxxxxx
89
1.16M
        if (i + 2 >= str.length()) {
90
6.96k
            return JSONNET_CODEPOINT_ERROR;
91
6.96k
        }
92
1.15M
        char c1 = str[++i];
93
1.15M
        if ((c1 & 0xC0) != 0x80) {
94
1.03M
            return JSONNET_CODEPOINT_ERROR;
95
1.03M
        }
96
123k
        char c2 = str[++i];
97
123k
        if ((c2 & 0xC0) != 0x80) {
98
30.7k
            return JSONNET_CODEPOINT_ERROR;
99
30.7k
        }
100
92.5k
        return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
101
12.4M
    } else if ((c0 & 0xF8) == 0xF0) {  // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
102
367k
        if (i + 3 >= str.length()) {
103
7.12k
            return JSONNET_CODEPOINT_ERROR;
104
7.12k
        }
105
360k
        char c1 = str[++i];
106
360k
        if ((c1 & 0xC0) != 0x80) {
107
217k
            return JSONNET_CODEPOINT_ERROR;
108
217k
        }
109
142k
        char c2 = str[++i];
110
142k
        if ((c2 & 0xC0) != 0x80) {
111
11.4k
            return JSONNET_CODEPOINT_ERROR;
112
11.4k
        }
113
130k
        char c3 = str[++i];
114
130k
        if ((c3 & 0xC0) != 0x80) {
115
36.1k
            return JSONNET_CODEPOINT_ERROR;
116
36.1k
        }
117
94.5k
        return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
118
12.0M
    } else {
119
12.0M
        return JSONNET_CODEPOINT_ERROR;
120
12.0M
    }
121
588M
}
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
156M
{
76
156M
    char c0 = str[i];
77
156M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
156M
        return c0;
79
156M
    } 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
156M
}
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
16.2M
{
76
16.2M
    char c0 = str[i];
77
16.2M
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
16.2M
        return c0;
79
16.2M
    } 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
16.2M
}
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
110k
{
76
110k
    char c0 = str[i];
77
110k
    if ((c0 & 0x80) == 0) {  // 0xxxxxxx
78
110k
        return c0;
79
110k
    } 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
110k
}
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.16M
{
128
3.16M
    for (char32_t cp : s)
129
1.56G
        encode_utf8(cp, r);
130
3.16M
}
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.63k
{
128
1.63k
    for (char32_t cp : s)
129
21.5M
        encode_utf8(cp, r);
130
1.63k
}
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.31M
{
128
2.31M
    for (char32_t cp : s)
129
1.53G
        encode_utf8(cp, r);
130
2.31M
}
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
844k
{
128
844k
    for (char32_t cp : s)
129
5.65M
        encode_utf8(cp, r);
130
844k
}
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.16M
{
134
3.16M
    std::string r;
135
3.16M
    encode_utf8(s, r);
136
3.16M
    return r;
137
3.16M
}
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.63k
{
134
1.63k
    std::string r;
135
1.63k
    encode_utf8(s, r);
136
1.63k
    return r;
137
1.63k
}
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.31M
{
134
2.31M
    std::string r;
135
2.31M
    encode_utf8(s, r);
136
2.31M
    return r;
137
2.31M
}
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
844k
{
134
844k
    std::string r;
135
844k
    encode_utf8(s, r);
136
844k
    return r;
137
844k
}
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
133M
{
141
133M
    UString r;
142
894M
    for (size_t i = 0; i < s.length(); ++i)
143
760M
        r.push_back(decode_utf8(s, i));
144
133M
    return r;
145
133M
}
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
98.2M
{
141
98.2M
    UString r;
142
686M
    for (size_t i = 0; i < s.length(); ++i)
143
588M
        r.push_back(decode_utf8(s, i));
144
98.2M
    return r;
145
98.2M
}
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
26.0M
{
141
26.0M
    UString r;
142
182M
    for (size_t i = 0; i < s.length(); ++i)
143
156M
        r.push_back(decode_utf8(s, i));
144
26.0M
    return r;
145
26.0M
}
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
9.37M
{
141
9.37M
    UString r;
142
25.5M
    for (size_t i = 0; i < s.length(); ++i)
143
16.2M
        r.push_back(decode_utf8(s, i));
144
9.37M
    return r;
145
9.37M
}
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
20.1k
{
141
20.1k
    UString r;
142
130k
    for (size_t i = 0; i < s.length(); ++i)
143
110k
        r.push_back(decode_utf8(s, i));
144
20.1k
    return r;
145
20.1k
}
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
59.7M
    {
156
59.7M
        buf.append(s);
157
59.7M
        return *this;
158
59.7M
    }
159
    UStringStream &operator<<(const char32_t *s)
160
2.16G
    {
161
2.16G
        buf.append(s);
162
2.16G
        return *this;
163
2.16G
    }
164
    UStringStream &operator<<(char32_t c)
165
125M
    {
166
125M
        buf.push_back(c);
167
125M
        return *this;
168
125M
    }
169
    template <class T>
170
    UStringStream &operator<<(T c)
171
3.22M
    {
172
3.22M
        std::stringstream ss;
173
3.22M
        ss << c;
174
3.22M
        for (char c : ss.str())
175
7.02M
            buf.push_back(char32_t(c));
176
3.22M
        return *this;
177
3.22M
    }
jsonnet::internal::UStringStream& jsonnet::internal::UStringStream::operator<< <int>(int)
Line
Count
Source
171
2.48M
    {
172
2.48M
        std::stringstream ss;
173
2.48M
        ss << c;
174
2.48M
        for (char c : ss.str())
175
4.40M
            buf.push_back(char32_t(c));
176
2.48M
        return *this;
177
2.48M
    }
jsonnet::internal::UStringStream& jsonnet::internal::UStringStream::operator<< <unsigned int>(unsigned int)
Line
Count
Source
171
734k
    {
172
734k
        std::stringstream ss;
173
734k
        ss << c;
174
734k
        for (char c : ss.str())
175
2.61M
            buf.push_back(char32_t(c));
176
734k
        return *this;
177
734k
    }
178
    UString str()
179
17.6M
    {
180
17.6M
        return buf;
181
17.6M
    }
182
};
183
184
}  // namespace jsonnet::internal
185
186
#endif  // JSONNET_UNICODE_H