tvm
Loading...
Searching...
No Matches
str_escape.h
Go to the documentation of this file.
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
25#ifndef TVM_SUPPORT_STR_ESCAPE_H_
26#define TVM_SUPPORT_STR_ESCAPE_H_
27
28#include <sstream>
29#include <string>
30
31namespace tvm {
32namespace support {
33
50inline std::string StrEscape(const char* data, size_t size, bool use_octal_escape = false,
51 bool escape_whitespace_special_chars = true) {
52 std::ostringstream stream;
53 for (size_t i = 0; i < size; ++i) {
54 unsigned char c = data[i];
55 if (c >= ' ' && c <= '~' && c != '\\' && c != '"') {
56 stream << c;
57 } else {
58 switch (c) {
59 case '"':
60 stream << '\\' << '"';
61 break;
62 case '\\':
63 stream << '\\' << '\\';
64 break;
65 case '\t':
66 if (escape_whitespace_special_chars) {
67 stream << '\\' << 't';
68 } else {
69 stream << c;
70 }
71 break;
72 case '\r':
73 if (escape_whitespace_special_chars) {
74 stream << '\\' << 'r';
75 } else {
76 stream << c;
77 }
78 break;
79 case '\n':
80 if (escape_whitespace_special_chars) {
81 stream << '\\' << 'n';
82 } else {
83 stream << c;
84 }
85 break;
86 default:
87 if (use_octal_escape) {
88 stream << '\\' << static_cast<unsigned char>('0' + ((c >> 6) & 0x03))
89 << static_cast<unsigned char>('0' + ((c >> 3) & 0x07))
90 << static_cast<unsigned char>('0' + (c & 0x07));
91 } else {
92 const char* hex_digits = "0123456789ABCDEF";
93 stream << '\\' << 'x' << hex_digits[c >> 4] << hex_digits[c & 0xf];
94 }
95 }
96 }
97 }
98 return stream.str();
99}
100
116inline std::string StrEscape(const std::string& val, bool use_octal_escape = false,
117 bool escape_whitespace_special_chars = true) {
118 return StrEscape(val.data(), val.length(), use_octal_escape, escape_whitespace_special_chars);
119}
120
121} // namespace support
122} // namespace tvm
123#endif // TVM_SUPPORT_STR_ESCAPE_H_
std::string StrEscape(const char *data, size_t size, bool use_octal_escape=false, bool escape_whitespace_special_chars=true)
Create a stream with escape.
Definition str_escape.h:50
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23