1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one |
3 | * or more contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. The ASF licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | |
20 | /*! |
21 | * |
22 | * \file support/str_escape.h |
23 | * \brief Print escape sequence of a string. |
24 | */ |
25 | #ifndef TVM_SUPPORT_STR_ESCAPE_H_ |
26 | #define TVM_SUPPORT_STR_ESCAPE_H_ |
27 | |
28 | #include <sstream> |
29 | #include <string> |
30 | |
31 | namespace tvm { |
32 | namespace support { |
33 | |
34 | /*! |
35 | * \brief Create a stream with escape. |
36 | * \param data The data |
37 | * \param size The size of the string. |
38 | * \param use_octal_escape True to use octal escapes instead of hex. If producing C |
39 | * strings, use octal escapes to avoid ambiguously-long hex escapes. |
40 | * \return the Result string. |
41 | */ |
42 | inline std::string StrEscape(const char* data, size_t size, bool use_octal_escape = false) { |
43 | std::ostringstream stream; |
44 | for (size_t i = 0; i < size; ++i) { |
45 | unsigned char c = data[i]; |
46 | if (c >= ' ' && c <= '~' && c != '\\' && c != '"') { |
47 | stream << c; |
48 | } else { |
49 | stream << '\\'; |
50 | switch (c) { |
51 | case '"': |
52 | stream << '"'; |
53 | break; |
54 | case '\\': |
55 | stream << '\\'; |
56 | break; |
57 | case '\t': |
58 | stream << 't'; |
59 | break; |
60 | case '\r': |
61 | stream << 'r'; |
62 | break; |
63 | case '\n': |
64 | stream << 'n'; |
65 | break; |
66 | default: |
67 | if (use_octal_escape) { |
68 | stream << static_cast<unsigned char>('0' + ((c >> 6) & 0x03)) |
69 | << static_cast<unsigned char>('0' + ((c >> 3) & 0x07)) |
70 | << static_cast<unsigned char>('0' + (c & 0x07)); |
71 | } else { |
72 | const char* hex_digits = "0123456789ABCDEF" ; |
73 | stream << 'x' << hex_digits[c >> 4] << hex_digits[c & 0xf]; |
74 | } |
75 | } |
76 | } |
77 | } |
78 | return stream.str(); |
79 | } |
80 | |
81 | /*! |
82 | * \brief Create a stream with escape. |
83 | * \param data The data |
84 | * \param size The size of the string. |
85 | * \return the Result string. |
86 | */ |
87 | inline std::string StrEscape(const std::string& val) { return StrEscape(val.data(), val.length()); } |
88 | |
89 | } // namespace support |
90 | } // namespace tvm |
91 | #endif // TVM_SUPPORT_STR_ESCAPE_H_ |
92 | |