/src/spotify-json/src/detail/escape.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2015-2019 Spotify AB |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | | * use this file except in compliance with the License. You may obtain a copy of |
6 | | * 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, WITHOUT |
12 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | * License for the specific language governing permissions and limitations under |
14 | | * the License. |
15 | | */ |
16 | | |
17 | | #include <spotify/json/detail/escape.hpp> |
18 | | |
19 | | #include <cstring> |
20 | | #include <spotify/json/detail/macros.hpp> |
21 | | |
22 | | #include "escape_common.hpp" |
23 | | |
24 | | namespace spotify { |
25 | | namespace json { |
26 | | namespace detail { |
27 | | |
28 | | #if defined(json_arch_x86_sse42) |
29 | | void write_escaped_sse42(encode_context &context, const char *begin, const char *end); |
30 | | #endif // defined(json_arch_x86_sse42) |
31 | | |
32 | 0 | void write_escaped_scalar(encode_context &context, const char *begin, const char *end) { |
33 | 0 | const auto buf = context.reserve(6 * (end - begin)); // 6 is the length of \u00xx |
34 | 0 | auto ptr = buf; |
35 | |
|
36 | 0 | if (json_unaligned_2(begin) && (end - begin) >= 1) { write_escaped_1(ptr, begin); } |
37 | 0 | if (json_unaligned_4(begin) && (end - begin) >= 2) { write_escaped_2(ptr, begin); } |
38 | 0 | if (json_unaligned_8(begin) && (end - begin) >= 4) { write_escaped_4(ptr, begin); } |
39 | 0 | while ((end - begin) >= 8) { write_escaped_8(ptr, begin); } |
40 | 0 | if ((end - begin) >= 4) { write_escaped_4(ptr, begin); } |
41 | 0 | if ((end - begin) >= 2) { write_escaped_2(ptr, begin); } |
42 | 0 | if ((end - begin) >= 1) { write_escaped_1(ptr, begin); } |
43 | |
|
44 | 0 | context.advance(ptr - buf); |
45 | 0 | } |
46 | | |
47 | 2.34k | void write_escaped(encode_context &context, const char *begin, const char *end) { |
48 | 2.34k | #if defined(json_arch_x86_sse42) |
49 | 2.34k | if (json_likely(context.has_sse42)) { |
50 | 2.34k | return write_escaped_sse42(context, begin, end); |
51 | 2.34k | } |
52 | 0 | #endif // defined(json_arch_x86_sse42) |
53 | 0 | write_escaped_scalar(context, begin, end); |
54 | 0 | } |
55 | | |
56 | | } // namespace detail |
57 | | } // namespace json |
58 | | } // namespace spotify |