File: StringBuilderJSON.h

package info (click to toggle)
webkit2gtk 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 429,620 kB
  • sloc: cpp: 3,696,936; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (75 lines) | stat: -rw-r--r-- 2,610 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2010-2018 Apple Inc. All rights reserved.
 * Copyright (C) 2012 Google Inc. All rights reserved.
 * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>. All rights reserved.
 * Copyright (C) 2017 Mozilla Foundation. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <wtf/text/EscapedFormsForJSON.h>
#include <wtf/text/ParsingUtilities.h>
#include <wtf/text/StringBuilderInternals.h>
#include <wtf/text/WTFString.h>

namespace WTF {

template<typename OutputCharacterType, typename InputCharacterType>
ALWAYS_INLINE static void appendEscapedJSONStringContent(std::span<OutputCharacterType>& output, std::span<const InputCharacterType> input)
{
    for (; !input.empty(); skip(input, 1)) {
        auto character = input.front();
        if (LIKELY(character <= 0xFF)) {
            auto escaped = escapedFormsForJSON[character];
            if (LIKELY(!escaped)) {
                consume(output) = character;
                continue;
            }

            output[0] = '\\';
            output[1] = escaped;
            skip(output, 2);
            if (UNLIKELY(escaped == 'u')) {
                output[0] = '0';
                output[1] = '0';
                output[2] = upperNibbleToLowercaseASCIIHexDigit(character);
                output[3] = lowerNibbleToLowercaseASCIIHexDigit(character);
                skip(output, 4);
            }
            continue;
        }

        if (LIKELY(!U16_IS_SURROGATE(character))) {
            consume(output) = character;
            continue;
        }

        if (input.size() > 1) {
            auto next = input[1];
            bool isValidSurrogatePair = U16_IS_SURROGATE_LEAD(character) && U16_IS_TRAIL(next);
            if (isValidSurrogatePair) {
                output[0] = character;
                output[1] = next;
                skip(output, 2);
                skip(input, 1);
                continue;
            }
        }

        uint8_t upper = static_cast<uint32_t>(character) >> 8;
        uint8_t lower = static_cast<uint8_t>(character);
        output[0] = '\\';
        output[1] = 'u';
        output[2] = upperNibbleToLowercaseASCIIHexDigit(upper);
        output[3] = lowerNibbleToLowercaseASCIIHexDigit(upper);
        output[4] = upperNibbleToLowercaseASCIIHexDigit(lower);
        output[5] = lowerNibbleToLowercaseASCIIHexDigit(lower);
        skip(output, 6);
    }
}

} // namespace WTF