move code responsible for replacing content in strings to Replace()

This commit is contained in:
Andrzej Rybczak
2009-10-10 15:36:51 +02:00
parent 63fbdaae1f
commit f6f7a8a27e
5 changed files with 15 additions and 15 deletions

View File

@@ -199,12 +199,9 @@ void EscapeHtml(std::string &s)
size_t j = s.find(">")+1; size_t j = s.find(">")+1;
s.replace(i, j-i, ""); s.replace(i, j-i, "");
} }
for (size_t i = s.find("'"); i != std::string::npos; i = s.find("'")) Replace(s, "'", "'");
s.replace(i, static_strlen("'"), "'"); Replace(s, """, "\"");
for (size_t i = s.find("""); i != std::string::npos; i = s.find(""")) Replace(s, "&", "&");
s.replace(i, static_strlen("""), "\"");
for (size_t i = s.find("&"); i != std::string::npos; i = s.find("&"))
s.replace(i, static_strlen("&"), "&");
for (size_t i = 0; i < s.length(); ++i) for (size_t i = 0; i < s.length(); ++i)
{ {
if (erase) if (erase)

View File

@@ -31,6 +31,12 @@ template <size_t N> inline size_t static_strlen(const char (&)[N])
return N-1; return N-1;
} }
template <size_t N> void Replace(std::string &s, const char (&from)[N], const char *to)
{
for (size_t i = 0; (i = s.find(from, i)) != std::string::npos; i += N)
s.replace(i, N-1, to);
}
void ToLower(std::string &); void ToLower(std::string &);
int StrToInt(const std::string &); int StrToInt(const std::string &);

View File

@@ -315,8 +315,7 @@ std::string GetLineValue(std::string &line, char a, char b, bool once)
} }
pos[0]++; pos[0]++;
std::string result = pos[0] >= 0 && pos[1] >= 0 ? line.substr(pos[0], pos[1]-pos[0]) : ""; std::string result = pos[0] >= 0 && pos[1] >= 0 ? line.substr(pos[0], pos[1]-pos[0]) : "";
for (i = result.find("\\\""); i != std::string::npos; i = result.find("\\\"")) Replace(result, "\\\"", "\"");
result.replace(i, 2, "\"");
return result; return result;
} }

View File

@@ -209,8 +209,8 @@ void *Lyrics::Get(void *screen_void_ptr)
char *c_title = curl_easy_escape(0, title.c_str(), title.length()); char *c_title = curl_easy_escape(0, title.c_str(), title.length());
std::string url = my_lyrics->url; std::string url = my_lyrics->url;
url.replace(url.find("%artist%"), static_strlen("%artist%"), c_artist); Replace(url, "%artist%", c_artist);
url.replace(url.find("%title%"), static_strlen("%title%"), c_title); Replace(url, "%title%", c_title);
CURLcode code; CURLcode code;
pthread_mutex_lock(&CurlLock); pthread_mutex_lock(&CurlLock);
@@ -255,10 +255,8 @@ void *Lyrics::Get(void *screen_void_ptr)
pthread_exit(0); pthread_exit(0);
} }
for (size_t i = result.find("&lt;"); i != std::string::npos; i = result.find("&lt;")) Replace(result, "&lt;", "<");
result.replace(i, static_strlen("&lt;"), "<"); Replace(result, "&gt;", ">");
for (size_t i = result.find("&gt;"); i != std::string::npos; i = result.find("&gt;"))
result.replace(i, static_strlen("&gt;"), ">");
EscapeHtml(result); EscapeHtml(result);
Trim(result); Trim(result);

View File

@@ -415,7 +415,7 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *
std::string tag = GetTags(get); std::string tag = GetTags(get);
if (escape_chars) // prepend format escape character to all given chars to escape if (escape_chars) // prepend format escape character to all given chars to escape
for (const char *ch = escape_chars; *ch; ++ch) for (const char *ch = escape_chars; *ch; ++ch)
for (size_t i = tag.find(*ch); i != std::string::npos; i = tag.find(*ch, i += 2)) for (size_t i = 0; (i = tag.find(*ch), i) != std::string::npos; i += 2)
tag.replace(i, 1, std::string(1, FormatEscapeCharacter) + ch); tag.replace(i, 1, std::string(1, FormatEscapeCharacter) + ch);
if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength())) if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength()))
{ {