|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +#include <aws/core/utils/memory/stl/AWSArray.h> |
1 | 6 | #include <smithy/client/schema/JsonWriteUtils.h> |
2 | 7 |
|
3 | | -using namespace smithy::schema; |
| 8 | +namespace { |
4 | 9 |
|
5 | | -const char JsonWriteUtils::HEX[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 10 | +static const Aws::Array<char, 16> HEX = {{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}}; |
6 | 11 |
|
7 | | -const bool JsonWriteUtils::NEEDS_ESCAPE[128] = { |
8 | | - // 0x00-0x1F: control chars |
9 | | - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
10 | | - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
11 | | - // 0x20-0x2F: space ! " # $ % & ' ( ) * + , - . / |
12 | | - 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0, |
13 | | - // 0x30-0x3F |
14 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
15 | | - // 0x40-0x4F |
16 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
17 | | - // 0x50-0x5F: P-Z [ \ ] ^ _ |
18 | | - 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0, |
19 | | - // 0x60-0x6F |
20 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
21 | | - // 0x70-0x7F |
22 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
23 | | -}; |
| 12 | +void EscapeChar(Aws::String& buf, unsigned char c) { |
| 13 | + switch (c) { |
| 14 | + case '\"': |
| 15 | + buf += "\\\""; |
| 16 | + break; |
| 17 | + case '\\': |
| 18 | + buf += "\\\\"; |
| 19 | + break; |
| 20 | + case '\b': |
| 21 | + buf += "\\b"; |
| 22 | + break; |
| 23 | + case '\f': |
| 24 | + buf += "\\f"; |
| 25 | + break; |
| 26 | + case '\n': |
| 27 | + buf += "\\n"; |
| 28 | + break; |
| 29 | + case '\r': |
| 30 | + buf += "\\r"; |
| 31 | + break; |
| 32 | + case '\t': |
| 33 | + buf += "\\t"; |
| 34 | + break; |
| 35 | + default: |
| 36 | + buf += "\\u00"; |
| 37 | + buf += HEX[(c >> 4) & 0xF]; |
| 38 | + buf += HEX[c & 0xF]; |
| 39 | + break; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +bool NeedsEscape(unsigned char c) { return c < 0x20 || c == '"' || c == '\\'; } |
| 44 | + |
| 45 | +} // anonymous namespace |
24 | 46 |
|
25 | | -const char JsonWriteUtils::ESCAPE_TABLE[128] = { |
26 | | - // 0x00-0x0F |
27 | | - 0,0,0,0,0,0,0,0,'b','t','n',0,'f','r',0,0, |
28 | | - // 0x10-0x1F |
29 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
30 | | - // 0x20-0x2F |
31 | | - 0,0,'"',0,0,0,0,0,0,0,0,0,0,0,0,0, |
32 | | - // 0x30-0x3F |
33 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
34 | | - // 0x40-0x4F |
35 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
36 | | - // 0x50-0x5F |
37 | | - 0,0,0,0,0,0,0,0,0,0,0,0,'\\',0,0,0, |
38 | | - // 0x60-0x6F |
39 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
40 | | - // 0x70-0x7F |
41 | | - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
42 | | -}; |
| 47 | +namespace Aws { |
| 48 | +namespace Schema { |
43 | 49 |
|
44 | | -void JsonWriteUtils::WriteQuotedString(Aws::String& buf, const Aws::String& value) { |
45 | | - buf += '"'; |
46 | | - const char* data = value.data(); |
47 | | - const size_t len = value.size(); |
| 50 | +void WriteQuotedJsonString(Aws::String& buf, const Aws::String& value) { |
| 51 | + buf += '"'; |
| 52 | + const char* data = value.data(); |
| 53 | + const size_t len = value.size(); |
48 | 54 |
|
49 | | - size_t i = 0; |
| 55 | + size_t i = 0; |
| 56 | + while (i < len) { |
| 57 | + size_t start = i; |
50 | 58 | while (i < len) { |
51 | | - // Fast scan: find next char that needs escaping |
52 | | - size_t start = i; |
53 | | - while (i < len) { |
54 | | - unsigned char c = static_cast<unsigned char>(data[i]); |
55 | | - if (c < 0x80 && NEEDS_ESCAPE[c]) break; |
56 | | - i++; |
57 | | - } |
58 | | - // Bulk append safe run |
59 | | - if (i > start) { |
60 | | - buf.append(data + start, i - start); |
61 | | - } |
62 | | - // Escape one char |
63 | | - if (i < len) { |
64 | | - unsigned char c = static_cast<unsigned char>(data[i]); |
65 | | - if (ESCAPE_TABLE[c] != 0) { |
66 | | - buf += '\\'; |
67 | | - buf += ESCAPE_TABLE[c]; |
68 | | - } else { |
69 | | - buf += '\\'; |
70 | | - buf += 'u'; |
71 | | - buf += '0'; |
72 | | - buf += '0'; |
73 | | - buf += HEX[(c >> 4) & 0xF]; |
74 | | - buf += HEX[c & 0xF]; |
75 | | - } |
76 | | - i++; |
77 | | - } |
| 59 | + unsigned char c = static_cast<unsigned char>(data[i]); |
| 60 | + if (c < 0x80 && NeedsEscape(c)) break; |
| 61 | + i++; |
78 | 62 | } |
79 | | - buf += '"'; |
| 63 | + if (i > start) { |
| 64 | + buf.append(data + start, i - start); |
| 65 | + } |
| 66 | + if (i < len) { |
| 67 | + EscapeChar(buf, static_cast<unsigned char>(data[i])); |
| 68 | + i++; |
| 69 | + } |
| 70 | + } |
| 71 | + buf += '"'; |
80 | 72 | } |
| 73 | + |
| 74 | +} // namespace Schema |
| 75 | +} // namespace Aws |
0 commit comments