From f6f7a8a27e3100957699f5f9d93ba40f05a59bb9 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 10 Oct 2009 15:36:51 +0200 Subject: [PATCH] move code responsible for replacing content in strings to Replace() --- src/conv.cpp | 9 +++------ src/conv.h | 6 ++++++ src/helpers.cpp | 3 +-- src/lyrics.cpp | 10 ++++------ src/song.cpp | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/conv.cpp b/src/conv.cpp index 565e6e6a..a4ae1d05 100644 --- a/src/conv.cpp +++ b/src/conv.cpp @@ -199,12 +199,9 @@ void EscapeHtml(std::string &s) size_t j = s.find(">")+1; s.replace(i, j-i, ""); } - for (size_t i = s.find("'"); i != std::string::npos; i = s.find("'")) - 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 = s.find("&"); i != std::string::npos; i = s.find("&")) - s.replace(i, static_strlen("&"), "&"); + Replace(s, "'", "'"); + Replace(s, """, "\""); + Replace(s, "&", "&"); for (size_t i = 0; i < s.length(); ++i) { if (erase) diff --git a/src/conv.h b/src/conv.h index 717f1250..ed3e44d2 100644 --- a/src/conv.h +++ b/src/conv.h @@ -31,6 +31,12 @@ template inline size_t static_strlen(const char (&)[N]) return N-1; } +template 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 &); int StrToInt(const std::string &); diff --git a/src/helpers.cpp b/src/helpers.cpp index c8685a5f..dcfe2f65 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -315,8 +315,7 @@ std::string GetLineValue(std::string &line, char a, char b, bool once) } 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("\\\"")) - result.replace(i, 2, "\""); + Replace(result, "\\\"", "\""); return result; } diff --git a/src/lyrics.cpp b/src/lyrics.cpp index de97b8ce..ba5ba82a 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -209,8 +209,8 @@ void *Lyrics::Get(void *screen_void_ptr) char *c_title = curl_easy_escape(0, title.c_str(), title.length()); std::string url = my_lyrics->url; - url.replace(url.find("%artist%"), static_strlen("%artist%"), c_artist); - url.replace(url.find("%title%"), static_strlen("%title%"), c_title); + Replace(url, "%artist%", c_artist); + Replace(url, "%title%", c_title); CURLcode code; pthread_mutex_lock(&CurlLock); @@ -255,10 +255,8 @@ void *Lyrics::Get(void *screen_void_ptr) pthread_exit(0); } - for (size_t i = result.find("<"); i != std::string::npos; i = result.find("<")) - result.replace(i, static_strlen("<"), "<"); - for (size_t i = result.find(">"); i != std::string::npos; i = result.find(">")) - result.replace(i, static_strlen(">"), ">"); + Replace(result, "<", "<"); + Replace(result, ">", ">"); EscapeHtml(result); Trim(result); diff --git a/src/song.cpp b/src/song.cpp index 1b402be0..04690446 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -415,7 +415,7 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char * std::string tag = GetTags(get); if (escape_chars) // prepend format escape character to all given chars to escape 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); if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength())) {