X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7c968cee840eda3b7340cdb4ba822065721cbdf6..98cb8dcb0cf3b0e660f1f67e1c02451ab10257de:/src/common/tokenzr.cpp diff --git a/src/common/tokenzr.cpp b/src/common/tokenzr.cpp index 64f49e5822..26612661ee 100644 --- a/src/common/tokenzr.cpp +++ b/src/common/tokenzr.cpp @@ -2,7 +2,7 @@ // Name: tokenzr.cpp // Purpose: String tokenizer // Author: Guilhem Lavaux -// Modified by: Vadim Zeitlin +// Modified by: Vadim Zeitlin (almost full rewrite) // Created: 04/22/98 // RCS-ID: $Id$ // Copyright: (c) Guilhem Lavaux @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ - #pragma implementation "tokenzr.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -29,6 +25,10 @@ #endif #include "wx/tokenzr.h" +#include "wx/arrstr.h" + +// Required for wxIs... functions +#include // ============================================================================ // implementation @@ -98,20 +98,26 @@ void wxStringTokenizer::Reinit(const wxString& str) // do we have more of them? bool wxStringTokenizer::HasMoreTokens() const { - wxCHECK_MSG( IsOk(), FALSE, _T("you should call SetString() first") ); + wxCHECK_MSG( IsOk(), false, _T("you should call SetString() first") ); if ( m_string.find_first_not_of(m_delims) == wxString::npos ) { - // no non empty tokens left, but in wxTOKEN_RET_EMPTY_ALL mode we - // still may return TRUE if GetNextToken() wasn't called yet for the - // last trailing empty token - return m_mode == wxTOKEN_RET_EMPTY_ALL ? m_hasMore : FALSE; + // no non empty tokens left, but in 2 cases we still may return true if + // GetNextToken() wasn't called yet for this empty token: + // + // a) in wxTOKEN_RET_EMPTY_ALL mode we always do it + // b) in wxTOKEN_RET_EMPTY mode we do it in the special case of a + // string containing only the delimiter: then there is an empty + // token just before it + return (m_mode == wxTOKEN_RET_EMPTY_ALL) || + (m_mode == wxTOKEN_RET_EMPTY && m_pos == 0) + ? m_hasMore : false; } else { // there are non delimiter characters left, hence we do have more // tokens - return TRUE; + return true; } } @@ -171,7 +177,7 @@ wxString wxStringTokenizer::GetNextToken() // no more tokens in this string, even in wxTOKEN_RET_EMPTY_ALL // mode (we will return the trailing one right now in this case) - m_hasMore = FALSE; + m_hasMore = false; } else { @@ -193,3 +199,21 @@ wxString wxStringTokenizer::GetNextToken() return token; } + +// ---------------------------------------------------------------------------- +// public functions +// ---------------------------------------------------------------------------- + +wxArrayString wxStringTokenize(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) +{ + wxArrayString tokens; + wxStringTokenizer tk(str, delims, mode); + while ( tk.HasMoreTokens() ) + { + tokens.Add(tk.GetNextToken()); + } + + return tokens; +}