X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/bbf8fc5391b2458d630d29a794df99c8e681e6eb..23dd0dcea96a6ac281cda9e861c6675b4ae53d15:/src/common/tokenzr.cpp?ds=sidebyside diff --git a/src/common/tokenzr.cpp b/src/common/tokenzr.cpp index 0601d5d0e5..2822025bb7 100644 --- a/src/common/tokenzr.cpp +++ b/src/common/tokenzr.cpp @@ -1,8 +1,8 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: tokenzr.cpp +// Name: src/common/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" @@ -30,63 +26,198 @@ #include "wx/tokenzr.h" +#ifndef WX_PRECOMP + #include "wx/arrstr.h" + #include "wx/crt.h" +#endif + +// Required for wxIs... functions +#include + // ============================================================================ // implementation // ============================================================================ +// ---------------------------------------------------------------------------- +// helpers +// ---------------------------------------------------------------------------- + +static wxString::const_iterator +find_first_of(const wxChar *delims, size_t len, + const wxString::const_iterator& from, + const wxString::const_iterator& end) +{ + wxASSERT_MSG( from <= end, wxT("invalid index") ); + + for ( wxString::const_iterator i = from; i != end; ++i ) + { + if ( wxTmemchr(delims, *i, len) ) + return i; + } + + return end; +} + +static wxString::const_iterator +find_first_not_of(const wxChar *delims, size_t len, + const wxString::const_iterator& from, + const wxString::const_iterator& end) +{ + wxASSERT_MSG( from <= end, wxT("invalid index") ); + + for ( wxString::const_iterator i = from; i != end; ++i ) + { + if ( !wxTmemchr(delims, *i, len) ) + return i; + } + + return end; +} + // ---------------------------------------------------------------------------- // wxStringTokenizer construction // ---------------------------------------------------------------------------- -wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize, +wxStringTokenizer::wxStringTokenizer(const wxString& str, const wxString& delims, - bool ret_delims) + wxStringTokenizerMode mode) { - SetString(to_tokenize, delims, ret_delims); + SetString(str, delims, mode); } -void wxStringTokenizer::SetString(const wxString& to_tokenize, +void wxStringTokenizer::SetString(const wxString& str, const wxString& delims, - bool ret_delim) + wxStringTokenizerMode mode) { - m_string = to_tokenize; - m_delims = delims; - m_retdelims = ret_delim; - m_pos = 0; + if ( mode == wxTOKEN_DEFAULT ) + { + // by default, we behave like strtok() if the delimiters are only + // whitespace characters and as wxTOKEN_RET_EMPTY otherwise (for + // whitespace delimiters, strtok() behaviour is better because we want + // to count consecutive spaces as one delimiter) + wxString::const_iterator p; + for ( p = delims.begin(); p != delims.end(); ++p ) + { + if ( !wxIsspace(*p) ) + break; + } - // empty string doesn't have any tokens - m_hasMore = !m_string.empty(); + if ( p != delims.end() ) + { + // not whitespace char in delims + mode = wxTOKEN_RET_EMPTY; + } + else + { + // only whitespaces + mode = wxTOKEN_STRTOK; + } + } + +#if wxUSE_UNICODE // FIXME-UTF8: only wc_str() + m_delims = delims.wc_str(); +#else + m_delims = delims.mb_str(); +#endif + m_delimsLen = delims.length(); + + m_mode = mode; + + Reinit(str); } -wxStringTokenizer::~wxStringTokenizer() +void wxStringTokenizer::Reinit(const wxString& str) { + wxASSERT_MSG( IsOk(), wxT("you should call SetString() first") ); + + m_string = str; + m_stringEnd = m_string.end(); + m_pos = m_string.begin(); + m_lastDelim = wxT('\0'); + m_hasMoreTokens = MoreTokens_Unknown; } // ---------------------------------------------------------------------------- -// count the number of tokens in the string +// access to the tokens // ---------------------------------------------------------------------------- -size_t wxStringTokenizer::CountTokens() const +// do we have more of them? +bool wxStringTokenizer::HasMoreTokens() const { - size_t pos = 0; - size_t count = 0; - for ( ;; ) + // GetNextToken() calls HasMoreTokens() and so HasMoreTokens() is called + // twice in every interation in the following common usage patten: + // while ( HasMoreTokens() ) + // GetNextToken(); + // We optimize this case by caching HasMoreTokens() return value here: + if ( m_hasMoreTokens == MoreTokens_Unknown ) { - pos = m_string.find_first_of(m_delims, pos); - if ( pos == wxString::npos ) - break; + bool r = DoHasMoreTokens(); + wxConstCast(this, wxStringTokenizer)->m_hasMoreTokens = + r ? MoreTokens_Yes : MoreTokens_No; + return r; + } + else + return m_hasMoreTokens == MoreTokens_Yes; +} - count++; // one more token found +bool wxStringTokenizer::DoHasMoreTokens() const +{ + wxCHECK_MSG( IsOk(), false, wxT("you should call SetString() first") ); - pos++; // skip delimiter + if ( find_first_not_of(m_delims, m_delimsLen, m_pos, m_stringEnd) + != m_stringEnd ) + { + // there are non delimiter characters left, so we do have more tokens + return true; } - // normally, we didn't count the last token in the loop above - so add it - // unless the string was empty from the very beginning, in which case it - // still has 0 (and not 1) tokens - if ( !m_string.empty() ) + switch ( m_mode ) + { + case wxTOKEN_RET_EMPTY: + case wxTOKEN_RET_DELIMS: + // special hack for wxTOKEN_RET_EMPTY: we should return the initial + // empty token even if there are only delimiters after it + return !m_string.empty() && m_pos == m_string.begin(); + + case wxTOKEN_RET_EMPTY_ALL: + // special hack for wxTOKEN_RET_EMPTY_ALL: we can know if we had + // already returned the trailing empty token after the last + // delimiter by examining m_lastDelim: it is set to NUL if we run + // up to the end of the string in GetNextToken(), but if it is not + // NUL yet we still have this last token to return even if m_pos is + // already at m_string.length() + return m_pos < m_stringEnd || m_lastDelim != wxT('\0'); + + case wxTOKEN_INVALID: + case wxTOKEN_DEFAULT: + wxFAIL_MSG( wxT("unexpected tokenizer mode") ); + // fall through + + case wxTOKEN_STRTOK: + // never return empty delimiters + break; + } + + return false; +} + +// count the number of (remaining) tokens in the string +size_t wxStringTokenizer::CountTokens() const +{ + wxCHECK_MSG( IsOk(), 0, wxT("you should call SetString() first") ); + + // VZ: this function is IMHO not very useful, so it's probably not very + // important if its implementation here is not as efficient as it + // could be -- but OTOH like this we're sure to get the correct answer + // in all modes + wxStringTokenizer tkz(wxString(m_pos, m_stringEnd), m_delims, m_mode); + + size_t count = 0; + while ( tkz.HasMoreTokens() ) { count++; + + (void)tkz.GetNextToken(); } return count; @@ -99,32 +230,67 @@ size_t wxStringTokenizer::CountTokens() const wxString wxStringTokenizer::GetNextToken() { wxString token; - if ( HasMoreTokens() ) + do { - size_t pos = m_string.find_first_of(m_delims); // end of token - size_t pos2; // start of the next one - if ( pos != wxString::npos ) + if ( !HasMoreTokens() ) { - // return the delimiter too - pos2 = pos + 1; + break; } - else + + m_hasMoreTokens = MoreTokens_Unknown; + + // find the end of this token + wxString::const_iterator pos = + find_first_of(m_delims, m_delimsLen, m_pos, m_stringEnd); + + // and the start of the next one + if ( pos == m_stringEnd ) { - pos2 = m_string.length(); + // no more delimiters, the token is everything till the end of + // string + token.assign(m_pos, m_stringEnd); + + // skip the token + m_pos = m_stringEnd; - // no more tokens in this string - m_hasMore = FALSE; + // it wasn't terminated + m_lastDelim = wxT('\0'); } + else // we found a delimiter at pos + { + // in wxTOKEN_RET_DELIMS mode we return the delimiter character + // with token, otherwise leave it out + wxString::const_iterator tokenEnd(pos); + if ( m_mode == wxTOKEN_RET_DELIMS ) + ++tokenEnd; - token = wxString(m_string, m_retdelims ? pos2 : pos); + token.assign(m_pos, tokenEnd); - // remove token with the following it delimiter from string - m_string.erase(0, pos2); + // skip the token and the trailing delimiter + m_pos = pos + 1; - // keep track of the position in the original string too - m_pos += pos2; + m_lastDelim = (pos == m_stringEnd) ? wxT('\0') : (wxChar)*pos; + } } - //else: no more tokens, return empty token + while ( !AllowEmpty() && token.empty() ); 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; +}