[llvm] [Support][YamlTraits] Add quoting for keys in textual YAML representation (PR #88763)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 16 14:06:03 PDT 2024
================
@@ -801,6 +769,45 @@ void Output::output(StringRef s) {
Out << s;
}
+void Output::output(StringRef S, QuotingType MustQuote) {
+ if (MustQuote == QuotingType::None) {
+ // Only quote if we must.
+ output(S);
+ return;
+ }
+
+ const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
+ output(Quote); // Starting quote.
+
+ // When using double-quoted strings (and only in that case), non-printable
+ // characters may be present, and will be escaped using a variety of
+ // unicode-scalar and special short-form escapes. This is handled in
+ // yaml::escape.
+ if (MustQuote == QuotingType::Double) {
+ output(yaml::escape(S, /* EscapePrintable= */ false));
+ output(Quote);
+ return;
+ }
+
+ unsigned i = 0;
+ unsigned j = 0;
+ unsigned End = S.size();
+ const char *Base = S.data();
+
+ // When using single-quoted strings, any single quote ' must be doubled to be
+ // escaped.
+ while (j < End) {
+ if (S[j] == '\'') { // Escape quotes.
+ output(StringRef(&Base[i], j - i)); // "flush".
+ output(StringLiteral("''")); // Print it as ''
+ i = j + 1;
----------------
arsenm wrote:
Can you use a StringRef with take_back incrementing? (I guess this is just moving code and shouldn't change here)
https://github.com/llvm/llvm-project/pull/88763
More information about the llvm-commits
mailing list