Pango/Escape: used a stringstream for constructing the escaped text
(cherry-picked from commit 6920729812
)
This commit is contained in:
parent
a5859dc957
commit
e679abcf84
1 changed files with 19 additions and 14 deletions
|
@ -14,8 +14,10 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
namespace font {
|
||||
#include <sstream>
|
||||
|
||||
namespace font
|
||||
{
|
||||
/**
|
||||
* Escapes the pango markup characters in a text.
|
||||
*
|
||||
|
@ -29,32 +31,35 @@ namespace font {
|
|||
*/
|
||||
inline std::string escape_text(const std::string& text)
|
||||
{
|
||||
std::string result;
|
||||
std::ostringstream ss;
|
||||
for(const char c : text) {
|
||||
switch(c) {
|
||||
case '&': result += "&"; break;
|
||||
case '<': result += "<"; break;
|
||||
case '>': result += ">"; break;
|
||||
case '\'': result += "'"; break;
|
||||
case '"': result += """; break;
|
||||
default: result += c;
|
||||
case '&': ss << "&"; break;
|
||||
case '<': ss << "<"; break;
|
||||
case '>': ss << ">"; break;
|
||||
case '\'': ss << "'"; break;
|
||||
case '"': ss << """; break;
|
||||
default: ss << c;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// Escape only the ampersands. This is used by pango_text to try to recover from
|
||||
// markup parsing failure.
|
||||
inline std::string semi_escape_text(const std::string & text) {
|
||||
std::string semi_escaped;
|
||||
inline std::string semi_escape_text(const std::string & text)
|
||||
{
|
||||
std::ostringstream ss;
|
||||
for(const char c : text) {
|
||||
if(c == '&') {
|
||||
semi_escaped += "&";
|
||||
ss << "&";
|
||||
} else {
|
||||
semi_escaped += c;
|
||||
ss << c;
|
||||
}
|
||||
}
|
||||
return semi_escaped;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
} // end namespace font
|
||||
|
|
Loading…
Add table
Reference in a new issue