Tue, 21 May 2013 17:39:27 +0300
Fixed basename (for real...) and ported ::first and ::last from my previous string class implementation, std::string::find_last_of doesn't do what I thought it did
src/file.cpp | file | annotate | diff | comparison | revisions | |
src/file.h | file | annotate | diff | comparison | revisions | |
src/gui.cpp | file | annotate | diff | comparison | revisions | |
src/string.cpp | file | annotate | diff | comparison | revisions | |
src/string.h | file | annotate | diff | comparison | revisions |
--- a/src/file.cpp Tue May 21 14:24:41 2013 +0300 +++ b/src/file.cpp Tue May 21 17:39:27 2013 +0300 @@ -112,25 +112,24 @@ // ============================================================================= str dirname (str path) { - long lastpos; // FIXME: why doesn't str::last () work here? - for (lastpos = path.len () - 1; lastpos >= 0; --lastpos) - if (path[(size_t) lastpos] == DIRSLASH_CHAR) - break; + long lastpos = path.last (DIRSLASH); if (lastpos > 0) return path.substr (0, lastpos); +#ifndef _WIN32 + if (path[0] == DIRSLASH_CHAR) + return DIRSLASH; +#endif // _WIN32 + return ""; } // ============================================================================= str basename (str path) { - long lastpos; // FIXME: why doesn't str::last () work here? - for (lastpos = path.len () - 1; lastpos >= 0; --lastpos) - if (path[(size_t) lastpos] == DIRSLASH_CHAR) - break; + long lastpos = path.last (DIRSLASH); - if (lastpos < (log) path.len () - 1) + if (lastpos < (long) path.len () - 1) return path.substr (lastpos + 1, -1); return path;
--- a/src/file.h Tue May 21 14:24:41 2013 +0300 +++ b/src/file.h Tue May 21 17:39:27 2013 +0300 @@ -127,5 +127,6 @@ extern vector<partListEntry> g_PartList; str basename (str path); +str dirname (str path); #endif // FILE_H \ No newline at end of file
--- a/src/gui.cpp Tue May 21 14:24:41 2013 +0300 +++ b/src/gui.cpp Tue May 21 17:39:27 2013 +0300 @@ -509,7 +509,7 @@ // Append our current file if we have one if (g_curfile) { if (g_curfile->m_filename.len () > 0) - title += fmt (": %s", basename (g_curfile->m_filename)); + title += fmt (": %s", basename (g_curfile->m_filename).chars ()); else title += fmt (": <anonymous>");
--- a/src/string.cpp Tue May 21 14:24:41 2013 +0300 +++ b/src/string.cpp Tue May 21 17:39:27 2013 +0300 @@ -171,4 +171,55 @@ } return sub; +} + +// ============================================================================ +int String::first (const char* c, int a) const { + unsigned int r = 0; + unsigned int index = 0; + for (; a < len (); a++) { + if (m_string[a] == c[r]) { + if (r == 0) + index = a; + + r++; + if (r == strlen (c)) + return index; + } else { + if (r != 0) { + // If the string sequence broke at this point, we need to + // check this character again, for a new sequence just + // might start right here. + a--; + } + + r = 0; + } + } + + return -1; +} + +// ============================================================================ +int String::last (const char* c, int a) const { + if (a == -1) + a = len(); + + int max = strlen (c) - 1; + + int r = max; + for (; a >= 0; a--) { + if (m_string[a] == c[r]) { + r--; + if (r == -1) + return a; + } else { + if (r != max) + a++; + + r = max; + } + } + + return -1; } \ No newline at end of file
--- a/src/string.h Tue May 21 14:24:41 2013 +0300 +++ b/src/string.h Tue May 21 17:39:27 2013 +0300 @@ -58,14 +58,10 @@ ushort count (const char needle) const; bool empty () const { return m_string.empty (); } void erase (size_t pos) { m_string.erase (m_string.begin () + pos); } - size_t first (const char* other, size_t pos = 0) const { return m_string.find_first_of (other, pos); } - size_t first (const char other, size_t pos = 0) const { return m_string.find_first_of (other, pos); } - size_t first (const String other, size_t pos = 0) const { return m_string.find_first_of (other, pos); } + int first (const char* c, int a = 0) const; void format (const char* fmtstr, ...); void insert (size_t pos, char c) { m_string.insert (m_string.begin () + pos, c); } - size_t last (const char* other, size_t pos = 0) const { return m_string.find_last_of (other, pos); } - size_t last (const char other, size_t pos = 0) const { return m_string.find_last_of (other, pos); } - size_t last (const String other, size_t pos = 0) const { return m_string.find_last_of (other, pos); } + int last (const char* c, int a = -1) const; size_t len () const { return m_string.length (); } String lower () const; size_t maxSize () const { return m_string.max_size (); }