Made utils::escape/unescape not modify their argument.

This commit is contained in:
Guillaume Melquiond 2009-12-25 09:48:46 +00:00
parent 16529a0473
commit 33ce127fe2
5 changed files with 28 additions and 33 deletions

View File

@ -437,8 +437,7 @@ void save_preview_pane::draw_contents()
std::stringstream str;
// Escape all special characters in filenames
std::string name = (*info_)[index_].name;
str << font::BOLD_TEXT << utils::escape(name) << "\n" << time_buf;
str << font::BOLD_TEXT << utils::escape((*info_)[index_].name) << '\n' << time_buf;
const std::string& campaign_type = summary["campaign_type"];
if(utils::string_bool(summary["corrupt"], false)) {

View File

@ -211,9 +211,7 @@ SDL_Rect draw_text(surface dst, const SDL_Rect& area, int size,
i1 = parse_markup(i1,i2,&sz,&col,&text_style);
if(i1 != i2) {
std::string new_string(i1,i2);
utils::unescape(new_string);
std::string new_string = utils::unescape(std::string(i1, i2));
const SDL_Rect rect = draw_text_line(dst, area, sz, col, new_string, x, y, use_tooltips, text_style);
if(rect.w > res.w) {

View File

@ -463,10 +463,9 @@ preprocessor_data::preprocessor_data(preprocessor_streambuf &t,
s << history;
if (!name.empty()) {
std::string ename(name);
if (!history.empty())
s << ' ';
s << utils::escape(ename, " \\");
s << utils::escape(name, " \\");
}
if (!t.location_.empty())
s << ' ' << t.linenum_ << ' ' << t.location_;

View File

@ -207,35 +207,33 @@ int apply_modifier( const int number, const std::string &amount, const int minim
return value;
}
std::string &escape(std::string &str, const std::string& special_chars)
std::string escape(const std::string &str, const char *special_chars)
{
std::string::size_type pos = 0;
std::string::size_type pos = str.find_first_of(special_chars);
if (pos == std::string::npos) {
// Fast path, possibly involving only reference counting.
return str;
}
std::string res = str;
do {
pos = str.find_first_of(special_chars, pos);
if (pos == std::string::npos)
break;
str.insert(pos, 1, '\\');
pos += 2;
} while (pos < str.size());
return str;
res.insert(pos, 1, '\\');
pos = res.find_first_of(special_chars, pos + 2);
} while (pos != std::string::npos);
return res;
}
std::string& escape(std::string& str)
std::string unescape(const std::string &str)
{
static const std::string special_chars("#@{}+-,\\*=");
return escape(str, special_chars);
}
std::string &unescape(std::string &str)
{
std::string::size_type pos = 0;
std::string::size_type pos = str.find('\\');
if (pos == std::string::npos) {
// Fast path, possibly involving only reference counting.
return str;
}
std::string res = str;
do {
pos = str.find('\\', pos);
if (pos == std::string::npos)
break;
str.erase(pos, 1);
++pos;
} while (pos < str.size());
res.erase(pos, 1);
pos = res.find('\\', pos + 1);
} while (pos != std::string::npos);
return str;
}

View File

@ -109,7 +109,7 @@ std::vector< std::pair< int, int > > parse_ranges(std::string const &str);
int apply_modifier( const int number, const std::string &amount, const int minimum = 0);
/** Prepends a configurable set of characters with a backslash */
std::string &escape(std::string &str, const std::string& special_chars);
std::string escape(const std::string &str, const char *special_chars);
/**
* Prepend all special characters with a backslash.
@ -117,10 +117,11 @@ std::string &escape(std::string &str, const std::string& special_chars);
* Special characters are:
* #@{}+-,\*=
*/
std::string &escape(std::string &str);
inline std::string escape(const std::string &str)
{ return escape(str, "#@{}+-,\\*="); }
/** Remove all escape characters (backslash) */
std::string &unescape(std::string &str);
std::string unescape(const std::string &str);
/** Remove whitespace from the front and back of the string 'str'. */
std::string &strip(std::string &str);