From 20a49f38cb6e0aee01340a6c1652a18a191a13b4 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Fri, 21 Nov 2008 18:57:51 +0100 Subject: [PATCH] do not create various redundant temp strings in Song class --- src/song.cpp | 28 +++++++++++++++++++--------- src/song.h | 4 ++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/song.cpp b/src/song.cpp index 790fe39b..7989013e 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -48,24 +48,34 @@ void DefineEmptyTags() } Song::Song(mpd_Song *s, bool copy_ptr) : itsSong(s), + itsSlash(string::npos), itsHash(0), copyPtr(copy_ptr), isStream(0), itsGetEmptyFields(0) { - string itsFile = itsSong->file ? itsSong->file : ""; + size_t file_len = itsSong->file ? strlen(itsSong->file) : 0; - itsSlash = itsFile.find_last_of("/"); - - if (itsFile.substr(0, 7) == "http://") - isStream = 1; + if (itsSong->file) + { + for (size_t i = file_len-1; i < file_len; i--) + { + if (itsSong->file[i] == '/') + { + itsSlash = i; + break; + } + } + if (strncmp(itsSong->file, "http://", 7) == 0) + isStream = 1; + } // generate pseudo-hash - for (size_t i = 0; i < itsFile.length(); i++) + for (size_t i = 0; i < file_len; i++) { - itsHash += itsFile[i]; + itsHash += itsSong->file[i]; if (i%3) - itsHash *= itsFile[i]; + itsHash *= itsSong->file[i]; } } @@ -123,7 +133,7 @@ string Song::GetFile() const string Song::GetName() const { - return !itsSong->file ? (itsGetEmptyFields ? "" : EMPTY_TAG) : (itsSlash != string::npos && !isStream ? string(itsSong->file).substr(itsSlash+1) : (isStream && itsSong->name ? itsSong->name : itsSong->file)); + return !itsSong->file ? (itsGetEmptyFields ? "" : EMPTY_TAG) : (itsSlash != string::npos && !isStream ? itsSong->file+itsSlash+1 : (isStream && itsSong->name ? itsSong->name : itsSong->file)); } string Song::GetDirectory() const diff --git a/src/song.h b/src/song.h index 4f8ad78d..6700f968 100644 --- a/src/song.h +++ b/src/song.h @@ -36,7 +36,7 @@ void DefineEmptyTags(); class Song { public: - Song() : itsHash(0), copyPtr(0), isStream(0), itsGetEmptyFields(0) { itsSong = mpd_newSong(); } + Song() : itsSlash(string::npos), itsHash(0), copyPtr(0), isStream(0), itsGetEmptyFields(0) { itsSong = mpd_newSong(); } Song(mpd_Song *, bool = 0); Song(const Song &); ~Song(); @@ -97,7 +97,7 @@ class Song private: mpd_Song *itsSong; string itsNewName; - unsigned itsSlash; + size_t itsSlash; long long itsHash; bool copyPtr; bool isStream;