get rid of all mysterious numbers that meant length of string literals

This commit is contained in:
Andrzej Rybczak
2009-09-15 23:12:45 +00:00
parent 6e903f9fc8
commit 61a9d6a785
4 changed files with 18 additions and 13 deletions

View File

@@ -204,11 +204,11 @@ void EscapeHtml(std::string &s)
s.replace(i, j-i, ""); s.replace(i, j-i, "");
} }
for (size_t i = s.find("'"); i != std::string::npos; i = s.find("'")) for (size_t i = s.find("'"); i != std::string::npos; i = s.find("'"))
s.replace(i, 6, "'"); s.replace(i, static_strlen("'"), "'");
for (size_t i = s.find("""); i != std::string::npos; i = s.find(""")) for (size_t i = s.find("""); i != std::string::npos; i = s.find("""))
s.replace(i, 6, "\""); s.replace(i, static_strlen("""), "\"");
for (size_t i = s.find("&"); i != std::string::npos; i = s.find("&")) for (size_t i = s.find("&"); i != std::string::npos; i = s.find("&"))
s.replace(i, 5, "&"); 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

@@ -26,6 +26,11 @@
#include "window.h" #include "window.h"
#include "song.h" #include "song.h"
template <size_t N> inline size_t static_strlen(const char (&)[N])
{
return N-1;
}
void ToLower(std::string &); void ToLower(std::string &);
int StrToInt(const std::string &); int StrToInt(const std::string &);

View File

@@ -134,7 +134,7 @@ void Info::GetArtist()
# ifdef HAVE_PTHREAD_H # ifdef HAVE_PTHREAD_H
if (Downloader && !ArtistReady) if (Downloader && !ArtistReady)
{ {
ShowMessage("Artist's info is being downloaded..."); ShowMessage("Artist info is being downloaded...");
return; return;
} }
else if (ArtistReady) else if (ArtistReady)
@@ -263,7 +263,7 @@ void *Info::PrepareArtist(void *screen_void_ptr)
result[i] = '.'; result[i] = '.';
size_t j = result.find("</name>"); size_t j = result.find("</name>");
result[j] = '.'; result[j] = '.';
i += 6; i += static_strlen("<name>");
similar.push_back(result.substr(i, j-i)); similar.push_back(result.substr(i, j-i));
EscapeHtml(similar.back()); EscapeHtml(similar.back());
} }
@@ -273,11 +273,11 @@ void *Info::PrepareArtist(void *screen_void_ptr)
result[i] = '.'; result[i] = '.';
size_t j = result.find("</url>"); size_t j = result.find("</url>");
result[j] = '.'; result[j] = '.';
i += 5; i += static_strlen("<url>");
urls.push_back(result.substr(i, j-i)); urls.push_back(result.substr(i, j-i));
} }
a = result.find("<content>")+9; a = result.find("<content>")+static_strlen("<content>");
b = result.find("</content>"); b = result.find("</content>");
if (a == b) if (a == b)
@@ -287,8 +287,8 @@ void *Info::PrepareArtist(void *screen_void_ptr)
} }
else else
{ {
a += 9; // for <![CDATA[ a += static_strlen("<![CDATA[");
b -= 3; // for ]]> b -= static_strlen("]]>");
result = result.substr(a, b-a); result = result.substr(a, b-a);
} }

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%"), 8, c_artist); url.replace(url.find("%artist%"), static_strlen("%artist%"), c_artist);
url.replace(url.find("%title%"), 7, c_title); url.replace(url.find("%title%"), static_strlen("%title%"), c_title);
CURLcode code; CURLcode code;
pthread_mutex_lock(&CurlLock); pthread_mutex_lock(&CurlLock);
@@ -247,9 +247,9 @@ void *Lyrics::Get(void *screen_void_ptr)
} }
for (size_t i = result.find("&lt;"); i != std::string::npos; i = result.find("&lt;")) for (size_t i = result.find("&lt;"); i != std::string::npos; i = result.find("&lt;"))
result.replace(i, 4, "<"); result.replace(i, static_strlen("&lt;"), "<");
for (size_t i = result.find("&gt;"); i != std::string::npos; i = result.find("&gt;")) for (size_t i = result.find("&gt;"); i != std::string::npos; i = result.find("&gt;"))
result.replace(i, 4, ">"); result.replace(i, static_strlen("&gt;"), ">");
EscapeHtml(result); EscapeHtml(result);
Trim(result); Trim(result);