From e3c1fb8b124246343f20b98f4035b61dd3ccd7df Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Wed, 30 May 2018 23:35:14 +0000 Subject: [llvm-cov] Use the new PrintHTMLEscaped utility This removes some duplicate logic to escape characters in HTML output. llvm-svn: 333608 --- llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp | 38 ++++++++++++-------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp') diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp index e8105ab..330da8bd 100644 --- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -25,31 +25,29 @@ namespace { // Return a string with the special characters in \p Str escaped. std::string escape(StringRef Str, const CoverageViewOptions &Opts) { - std::string Result; + std::string TabExpandedResult; unsigned ColNum = 0; // Record the column number. for (char C : Str) { - ++ColNum; - if (C == '&') - Result += "&"; - else if (C == '<') - Result += "<"; - else if (C == '>') - Result += ">"; - else if (C == '\"') - Result += """; - else if (C == '\n' || C == '\r') { - Result += C; - ColNum = 0; - } else if (C == '\t') { - // Replace '\t' with TabSize spaces. - unsigned NumSpaces = Opts.TabSize - (--ColNum % Opts.TabSize); + if (C == '\t') { + // Replace '\t' with up to TabSize spaces. + unsigned NumSpaces = Opts.TabSize - (ColNum % Opts.TabSize); for (unsigned I = 0; I < NumSpaces; ++I) - Result += " "; + TabExpandedResult += ' '; ColNum += NumSpaces; - } else - Result += C; + } else { + TabExpandedResult += C; + if (C == '\n' || C == '\r') + ColNum = 0; + else + ++ColNum; + } + } + std::string EscapedHTML; + { + raw_string_ostream OS{EscapedHTML}; + PrintHTMLEscaped(TabExpandedResult, OS); } - return Result; + return EscapedHTML; } // Create a \p Name tag around \p Str, and optionally set its \p ClassName. -- cgit v1.1