1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 #![allow(dead_code)] 15 pub(crate) const COLON: u8 = b':'; 16 pub(crate) const COMMA: u8 = b','; 17 pub(crate) const DECIMAL_POINT: u8 = b'.'; 18 pub(crate) const LEFT_CURLY_BRACKET: u8 = b'{'; 19 pub(crate) const LEFT_SQUARE_BRACKET: u8 = b'['; 20 pub(crate) const MINUS: u8 = b'-'; 21 pub(crate) const PLUS: u8 = b'+'; 22 pub(crate) const RIGHT_CURLY_BRACKET: u8 = b'}'; 23 pub(crate) const RIGHT_SQUARE_BRACKET: u8 = b']'; 24 pub(crate) const SPACE: u8 = b' '; 25 26 pub(crate) const ZERO: u8 = b'0'; 27 pub(crate) const ONE: u8 = b'1'; 28 pub(crate) const NINE: u8 = b'9'; 29 pub(crate) const A_LOWER: u8 = b'a'; 30 pub(crate) const A_UPPER: u8 = b'A'; 31 pub(crate) const E_LOWER: u8 = b'e'; 32 pub(crate) const E_UPPER: u8 = b'E'; 33 pub(crate) const F_LOWER: u8 = b'f'; 34 pub(crate) const F_UPPER: u8 = b'F'; 35 pub(crate) const N_LOWER: u8 = b'n'; 36 pub(crate) const T_LOWER: u8 = b't'; 37 38 pub(crate) const WHITE_SPACE_SET: [u8; 4] = 39 [SPACE, HT_UNICODE as u8, LF_UNICODE as u8, CR_UNICODE as u8]; 40 41 pub(crate) const BS: u8 = b'b'; 42 pub(crate) const BS_UNICODE: char = '\u{0008}'; 43 pub(crate) const BS_UNICODE_U8: u8 = 0x08; 44 pub(crate) const HT: u8 = b't'; 45 pub(crate) const HT_UNICODE: char = '\u{0009}'; 46 pub(crate) const HT_UNICODE_U8: u8 = 0x09; 47 pub(crate) const FF: u8 = b'f'; 48 pub(crate) const FF_UNICODE: char = '\u{000c}'; 49 pub(crate) const FF_UNICODE_U8: u8 = 0x0c; 50 pub(crate) const CR: u8 = b'r'; 51 pub(crate) const CR_UNICODE: char = '\u{000d}'; 52 pub(crate) const CR_UNICODE_U8: u8 = 0x0d; 53 pub(crate) const LF: u8 = b'n'; 54 pub(crate) const LF_UNICODE: char = '\u{000a}'; 55 pub(crate) const LF_UNICODE_U8: u8 = 0x0a; 56 pub(crate) const UNICODE: u8 = b'u'; 57 pub(crate) const QUOTATION_MARK: u8 = b'\"'; 58 pub(crate) const REVERSE_SOLIDUS: u8 = b'\\'; 59 pub(crate) const SOLIDUS: u8 = b'/'; 60 61 pub(crate) const JSON_REVERSE_SOLIDUS: &[u8] = b"\\\\"; 62 pub(crate) const JSON_QUOTATION_MARK: &[u8] = b"\\\""; 63 pub(crate) const JSON_BS: &[u8] = b"\\b"; 64 pub(crate) const JSON_FF: &[u8] = b"\\f"; 65 pub(crate) const JSON_LF: &[u8] = b"\\n"; 66 pub(crate) const JSON_CR: &[u8] = b"\\r"; 67 pub(crate) const JSON_HT: &[u8] = b"\\t"; 68 69 pub(crate) const NULL_STR: &[u8] = b"null"; 70 pub(crate) const NULL_LEFT_STR: &[u8] = b"ull"; 71 pub(crate) const FALSE_STR: &[u8] = b"false"; 72 pub(crate) const FALSE_LEFT_STR: &[u8] = b"alse"; 73 pub(crate) const TRUE_STR: &[u8] = b"true"; 74 pub(crate) const TRUE_LEFT_STR: &[u8] = b"rue"; 75 pub(crate) const UNICODE_START_STR: &[u8] = b"\\u"; 76 pub(crate) const COLON_STR: &[u8] = b":"; 77 pub(crate) const COMMA_STR: &[u8] = b","; 78 pub(crate) const FOUR_SPACES_STR: &[u8] = b" "; 79 pub(crate) const LEFT_CURLY_BRACKET_STR: &[u8] = b"{"; 80 pub(crate) const LEFT_SQUARE_BRACKET_STR: &[u8] = b"["; 81 pub(crate) const LINE_FEED_STR: &[u8] = b"\n"; 82 pub(crate) const QUOTATION_MARK_STR: &[u8] = b"\""; 83 pub(crate) const RIGHT_CURLY_BRACKET_STR: &[u8] = b"}"; 84 pub(crate) const RIGHT_SQUARE_BRACKET_STR: &[u8] = b"]"; 85 pub(crate) const SPACE_STR: &[u8] = b" "; 86 87 pub(crate) const RECURSION_LIMIT: u32 = 128; 88 89 // Improves the string read rate by looking up tables. 90 pub(crate) static ESCAPE: [bool; 256] = { 91 const CT: bool = true; // Control character \x00..=\x1F 92 const QU: bool = true; // Quotation mark \x22 93 const BS: bool = true; // Backslash \x5C 94 const __: bool = false; // Other character 95 [ 96 // 1 2 3 4 5 6 7 8 9 A B C D E F 97 CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0 98 CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1 99 __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2 100 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3 101 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4 102 __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5 103 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6 104 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7 105 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8 106 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9 107 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A 108 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B 109 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C 110 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D 111 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E 112 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F 113 ] 114 }; 115 116 // TODO: Consider modifying the structure of PRINT_MAP. 117 #[cfg(not(feature = "ascii_only"))] 118 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 119 pub(crate) enum PrintMapItem<'a> { 120 Other, 121 Control, 122 Special(&'a [u8]), 123 } 124 125 // Improves the string output rate by looking up the table. 126 #[cfg(not(feature = "ascii_only"))] 127 pub(crate) static PRINT_MAP: [PrintMapItem; 256] = { 128 const BS: PrintMapItem = PrintMapItem::Special(b"\\b"); // BS 退格 \x08 129 const HT: PrintMapItem = PrintMapItem::Special(b"\\t"); // HT 水平定位符 \x09 130 const LF: PrintMapItem = PrintMapItem::Special(b"\\n"); // LF 换行 \x0A 131 const FF: PrintMapItem = PrintMapItem::Special(b"\\f"); // FF 换页 \x0C 132 const CR: PrintMapItem = PrintMapItem::Special(b"\\r"); // CR 归位 \x0D 133 const QU: PrintMapItem = PrintMapItem::Special(b"\\\""); // 双引号 \x22 134 const SO: PrintMapItem = PrintMapItem::Special(b"/"); // 斜杠 \x2F 135 const RS: PrintMapItem = PrintMapItem::Special(b"\\\\"); // 反斜杠 \x5C 136 const CT: PrintMapItem = PrintMapItem::Control; // 控制字符 \x00..=\x1F 137 const __: PrintMapItem = PrintMapItem::Other; // 其他字符 138 [ 139 // 1 2 3 4 5 6 7 8 9 A B C D E F 140 CT, CT, CT, CT, CT, CT, CT, CT, BS, HT, LF, CT, FF, CR, CT, CT, // 0 141 CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1 142 __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, SO, // 2 143 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3 144 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4 145 __, __, __, __, __, __, __, __, __, __, __, __, RS, __, __, __, // 5 146 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6 147 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7 148 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8 149 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9 150 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A 151 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B 152 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C 153 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D 154 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E 155 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F 156 ] 157 }; 158 159 #[cfg(not(feature = "ascii_only"))] 160 #[cfg(test)] 161 mod ut_consts { 162 use crate::consts::PrintMapItem; 163 164 /// UT test case for `PrintMapItem::clone`. 165 /// 166 /// # Title 167 /// ut_print_map_item_clone 168 /// 169 /// # Brief 170 /// 1. Creates a `PrintMapItem`. 171 /// 2. Calls `PrintMapItem::clone`. 172 /// 3. Checks if the results are correct. 173 #[allow(clippy::clone_on_copy)] 174 #[test] 175 fn ut_print_map_item_clone() { 176 let item = PrintMapItem::Other; 177 let item = item.clone(); 178 assert_eq!(item, PrintMapItem::Other); 179 180 let item = PrintMapItem::Control; 181 let item = item.clone(); 182 assert_eq!(item, PrintMapItem::Control); 183 184 let item = PrintMapItem::Special(b"abc"); 185 let item = item.clone(); 186 assert_eq!(item, PrintMapItem::Special(b"abc")); 187 } 188 189 /// UT test case for `PrintMapItem::copy`. 190 /// 191 /// # Title 192 /// ut_print_map_item_copy 193 /// 194 /// # Brief 195 /// 1. Creates a `PrintMapItem`. 196 /// 2. Calls `PrintMapItem::copy`. 197 /// 3. Checks if the results are correct. 198 #[test] 199 fn ut_print_map_item_copy() { 200 let item1 = PrintMapItem::Other; 201 let _item2 = item1; 202 assert_eq!(item1, PrintMapItem::Other); 203 204 let item1 = PrintMapItem::Control; 205 let _item2 = item1; 206 assert_eq!(item1, PrintMapItem::Control); 207 208 let item1 = PrintMapItem::Special(b"abc"); 209 let _item2 = item1; 210 assert_eq!(item1, PrintMapItem::Special(b"abc")); 211 } 212 } 213