-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
escape.hpp
66 lines (57 loc) · 1.55 KB
/
escape.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Copyright (C) 2008 - 2024
by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include <sstream>
namespace font
{
/**
* Escapes the pango markup characters in a text.
*
* The markups escaped are the ones used in the pango markup. The special
* characters are: @verbatim <>'"& @endverbatim
* The escaping is the same as for HTML.
*
* @param text The text to escape.
*
* @returns The escaped text.
*/
inline std::string escape_text(const std::string& text)
{
std::ostringstream ss;
for(const char c : text) {
switch(c) {
case '&': ss << "&"; break;
case '<': ss << "<"; break;
case '>': ss << ">"; break;
case '\'': ss << "'"; break;
case '"': ss << """; break;
default: ss << c;
}
}
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::ostringstream ss;
for(const char c : text) {
if(c == '&') {
ss << "&";
} else {
ss << c;
}
}
return ss.str();
}
} // end namespace font