@@ -33,44 +33,64 @@ namespace support {
3333
3434/* !
3535 * \brief Create a stream with escape.
36+ *
3637 * \param data The data
38+ *
3739 * \param size The size of the string.
40+ *
3841 * \param use_octal_escape True to use octal escapes instead of hex. If producing C
3942 * strings, use octal escapes to avoid ambiguously-long hex escapes.
43+ *
44+ * \param escape_whitespace_special_chars If True (default), escape
45+ * any tab, newline, and carriage returns that occur in the string.
46+ * If False, do not escape these characters.
47+ *
4048 * \return the Result string.
4149 */
42- inline std::string StrEscape (const char * data, size_t size, bool use_octal_escape = false ) {
50+ inline std::string StrEscape (const char * data, size_t size, bool use_octal_escape = false ,
51+ bool escape_whitespace_special_chars = true ) {
4352 std::ostringstream stream;
4453 for (size_t i = 0 ; i < size; ++i) {
4554 unsigned char c = data[i];
4655 if (c >= ' ' && c <= ' ~' && c != ' \\ ' && c != ' "' ) {
4756 stream << c;
4857 } else {
49- stream << ' \\ ' ;
5058 switch (c) {
5159 case ' "' :
52- stream << ' "' ;
60+ stream << ' \\ ' << ' "' ;
5361 break ;
5462 case ' \\ ' :
55- stream << ' \\ ' ;
63+ stream << ' \\ ' << ' \\ ' ;
5664 break ;
5765 case ' \t ' :
58- stream << ' t' ;
66+ if (escape_whitespace_special_chars) {
67+ stream << ' \\ ' << ' t' ;
68+ } else {
69+ stream << c;
70+ }
5971 break ;
6072 case ' \r ' :
61- stream << ' r' ;
73+ if (escape_whitespace_special_chars) {
74+ stream << ' \\ ' << ' r' ;
75+ } else {
76+ stream << c;
77+ }
6278 break ;
6379 case ' \n ' :
64- stream << ' n' ;
80+ if (escape_whitespace_special_chars) {
81+ stream << ' \\ ' << ' n' ;
82+ } else {
83+ stream << c;
84+ }
6585 break ;
6686 default :
6787 if (use_octal_escape) {
68- stream << static_cast <unsigned char >(' 0' + ((c >> 6 ) & 0x03 ))
88+ stream << ' \\ ' << static_cast <unsigned char >(' 0' + ((c >> 6 ) & 0x03 ))
6989 << static_cast <unsigned char >(' 0' + ((c >> 3 ) & 0x07 ))
7090 << static_cast <unsigned char >(' 0' + (c & 0x07 ));
7191 } else {
7292 const char * hex_digits = " 0123456789ABCDEF" ;
73- stream << ' x' << hex_digits[c >> 4 ] << hex_digits[c & 0xf ];
93+ stream << ' \\ ' << ' x' << hex_digits[c >> 4 ] << hex_digits[c & 0xf ];
7494 }
7595 }
7696 }
@@ -80,11 +100,23 @@ inline std::string StrEscape(const char* data, size_t size, bool use_octal_escap
80100
81101/* !
82102 * \brief Create a stream with escape.
83- * \param data The data
84- * \param size The size of the string.
103+ *
104+ * \param val The C++ string
105+ *
106+ * \param use_octal_escape True to use octal escapes instead of hex. If producing C
107+ * strings, use octal escapes to avoid ambiguously-long hex escapes.
108+ *
109+ * \param escape_whitespace_special_chars If True (default), escape
110+ * any tab, newline, and carriage returns that occur in the string.
111+ * If False, do not escape these characters. If producing python
112+ * strings with """triple quotes""", do not escape these characters.
113+ *
85114 * \return the Result string.
86115 */
87- inline std::string StrEscape (const std::string& val) { return StrEscape (val.data (), val.length ()); }
116+ inline 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+ }
88120
89121} // namespace support
90122} // namespace tvm
0 commit comments