| 196 | } |
| 197 | |
| 198 | string PBTxtFromMultiline(StringPiece multiline_pbtxt) { |
| 199 | string pbtxt; |
| 200 | // Probably big enough, since the input and output are about the |
| 201 | // same size, but just a guess. |
| 202 | pbtxt.reserve(multiline_pbtxt.size() * (33. / 32)); |
| 203 | StringPiece line; |
| 204 | while (!multiline_pbtxt.empty()) { |
| 205 | // Split multiline_pbtxt into its first line and everything after. |
| 206 | if (!SplitAt('\n', &multiline_pbtxt, &line)) { |
| 207 | strings::StrAppend(&pbtxt, line); |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | string end; |
| 212 | auto colon = line.find(':'); |
| 213 | if (!FindMultiline(line, colon, &end)) { |
| 214 | // Normal case: not a multi-line string, just output the line as-is. |
| 215 | strings::StrAppend(&pbtxt, line, "\n"); |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | // Multi-line case: |
| 220 | // something: <<END |
| 221 | // xx |
| 222 | // yy |
| 223 | // END |
| 224 | // Should be converted to: |
| 225 | // something: "xx\nyy" |
| 226 | |
| 227 | // Output everything up to the colon (" something:"). |
| 228 | strings::StrAppend(&pbtxt, line.substr(0, colon + 1)); |
| 229 | |
| 230 | // Add every line to unescaped until we see the "END" string. |
| 231 | string unescaped; |
| 232 | bool first = true; |
| 233 | while (!multiline_pbtxt.empty()) { |
| 234 | SplitAt('\n', &multiline_pbtxt, &line); |
| 235 | if (absl::ConsumePrefix(&line, end)) break; |
| 236 | if (first) { |
| 237 | first = false; |
| 238 | } else { |
| 239 | unescaped.push_back('\n'); |
| 240 | } |
| 241 | strings::StrAppend(&unescaped, line); |
| 242 | line = StringPiece(); |
| 243 | } |
| 244 | |
| 245 | // Escape what we extracted and then output it in quotes. |
| 246 | strings::StrAppend(&pbtxt, " \"", absl::CEscape(unescaped), "\"", line, |
| 247 | "\n"); |
| 248 | } |
| 249 | return pbtxt; |
| 250 | } |
| 251 | |
| 252 | static void StringReplace(const string& from, const string& to, string* s) { |
| 253 | // Split *s into pieces delimited by `from`. |