| 2086 | } |
| 2087 | |
| 2088 | std::string convertIntoString(StringRef string, bool escapeInvisibles) { |
| 2089 | std::string ret; |
| 2090 | // This is enough for the "don't escape invisibles" case, and a good |
| 2091 | // lower bound on the "escape invisibles" case. |
| 2092 | ret.reserve( string.size() + 2 ); |
| 2093 | |
| 2094 | if ( !escapeInvisibles ) { |
| 2095 | ret += '"'; |
| 2096 | ret += string; |
| 2097 | ret += '"'; |
| 2098 | return ret; |
| 2099 | } |
| 2100 | |
| 2101 | size_t last_start = 0; |
| 2102 | auto write_to = [&]( size_t idx ) { |
| 2103 | if ( last_start < idx ) { |
| 2104 | ret += string.substr( last_start, idx - last_start ); |
| 2105 | } |
| 2106 | last_start = idx + 1; |
| 2107 | }; |
| 2108 | |
| 2109 | ret += '"'; |
| 2110 | for ( size_t i = 0; i < string.size(); ++i ) { |
| 2111 | const char c = string[i]; |
| 2112 | if ( c == '\r' || c == '\n' || c == '\t' || c == '\f' ) { |
| 2113 | write_to( i ); |
| 2114 | if ( c == '\r' ) { ret.append( "\\r" ); } |
| 2115 | if ( c == '\n' ) { ret.append( "\\n" ); } |
| 2116 | if ( c == '\t' ) { ret.append( "\\t" ); } |
| 2117 | if ( c == '\f' ) { ret.append( "\\f" ); } |
| 2118 | } |
| 2119 | } |
| 2120 | write_to( string.size() ); |
| 2121 | ret += '"'; |
| 2122 | |
| 2123 | return ret; |
| 2124 | } |
| 2125 | |
| 2126 | std::string convertIntoString(StringRef string) { |
| 2127 | return convertIntoString(string, getCurrentContext().getConfig()->showInvisibles()); |
no test coverage detected