X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/aef82f12c833f22b22d4a1ffb09261569d1fe1e8..c437b3f4e47b74715a2f2385d4862972babd7802:/src/common/tokenzr.cpp diff --git a/src/common/tokenzr.cpp b/src/common/tokenzr.cpp index b22a79ba26..8c29cd7cfa 100644 --- a/src/common/tokenzr.cpp +++ b/src/common/tokenzr.cpp @@ -1,17 +1,21 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: tokenzr.cpp +// Name: src/common/tokenzr.cpp // Purpose: String tokenizer // Author: Guilhem Lavaux -// Modified by: +// Modified by: Vadim Zeitlin (almost full rewrite) // Created: 04/22/98 // RCS-ID: $Id$ // Copyright: (c) Guilhem Lavaux // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ - #pragma implementation "tokenzr.h" -#endif +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -22,117 +26,271 @@ #include "wx/tokenzr.h" -wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize, - const wxString& delims, - bool ret_delims) -{ - m_string = to_tokenize; - m_delims = delims; - m_retdelims = ret_delims; -} +#ifndef WX_PRECOMP + #include "wx/arrstr.h" + #include "wx/crt.h" +#endif + +// Required for wxIs... functions +#include + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// helpers +// ---------------------------------------------------------------------------- -wxStringTokenizer::~wxStringTokenizer() +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, _T("invalid index") ); + + for ( wxString::const_iterator i = from; i != end; ++i ) + { + if ( wxTmemchr(delims, *i, len) ) + return i; + } + + return end; } -off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims) const +static wxString::const_iterator +find_first_not_of(const wxChar *delims, size_t len, + const wxString::const_iterator& from, + const wxString::const_iterator& end) { - for ( size_t i = 0; i < str.Length(); i++ ) - { - char c = str[i]; + wxASSERT_MSG( from <= end, _T("invalid index") ); - for ( size_t j = 0; j < delims.Length() ; j++ ) - { - if ( delims[j] == c ) - return i; - } + for ( wxString::const_iterator i = from; i != end; ++i ) + { + if ( !wxTmemchr(delims, *i, len) ) + return i; } - return -1; + return end; } -int wxStringTokenizer::CountTokens() const -{ - wxString p_string = m_string; - bool found = TRUE; - int pos, count = 1; +// ---------------------------------------------------------------------------- +// wxStringTokenizer construction +// ---------------------------------------------------------------------------- - if (p_string.Length() == 0) - return 0; +wxStringTokenizer::wxStringTokenizer(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) +{ + SetString(str, delims, mode); +} - while (found) +void wxStringTokenizer::SetString(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) +{ + if ( mode == wxTOKEN_DEFAULT ) { - pos = FindDelims(p_string, m_delims); - if (pos != -1) + // 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 ) { - count++; - p_string = p_string(0, pos); + if ( !wxIsspace(*p) ) + break; + } + + if ( p != delims.end() ) + { + // not whitespace char in delims + mode = wxTOKEN_RET_EMPTY; } else { - found = FALSE; + // only whitespaces + mode = wxTOKEN_STRTOK; } } - return count; +#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); } -bool wxStringTokenizer::HasMoreTokens() +void wxStringTokenizer::Reinit(const wxString& str) { - return !m_string.IsEmpty(); + wxASSERT_MSG( IsOk(), _T("you should call SetString() first") ); + + m_string = str; + m_stringEnd = m_string.end(); + m_pos = m_string.begin(); + m_lastDelim = _T('\0'); + m_hasMoreTokens = MoreTokens_Unknown; } -// needed to fix leading whitespace / mult. delims bugs -void wxStringTokenizer::EatLeadingDelims() -{ - int pos; +// ---------------------------------------------------------------------------- +// access to the tokens +// ---------------------------------------------------------------------------- - // while leading delims trim 'em from the left - while ( ( pos = FindDelims(m_string, m_delims)) == 0 ) +// do we have more of them? +bool wxStringTokenizer::HasMoreTokens() const +{ + // 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 ) { - m_string = m_string.Mid((size_t)1); + bool r = DoHasMoreTokens(); + wxConstCast(this, wxStringTokenizer)->m_hasMoreTokens = + r ? MoreTokens_Yes : MoreTokens_No; + return r; } + else + return m_hasMoreTokens == MoreTokens_Yes; } -wxString wxStringTokenizer::NextToken() +bool wxStringTokenizer::DoHasMoreTokens() const { - off_t pos, pos2; - wxString r_string; + wxCHECK_MSG( IsOk(), false, _T("you should call SetString() first") ); + + 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; + } - if ( m_string.IsEmpty() ) - return m_string; + 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(); - if ( !m_retdelims ) - EatLeadingDelims(); + 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 != _T('\0'); - pos = FindDelims(m_string, m_delims); - if (pos == -1) + case wxTOKEN_INVALID: + case wxTOKEN_DEFAULT: + wxFAIL_MSG( _T("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, _T("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() ) { - r_string = m_string; - m_string = wxEmptyString; + count++; - return r_string; + (void)tkz.GetNextToken(); } - if (m_retdelims) + return count; +} + +// ---------------------------------------------------------------------------- +// token extraction +// ---------------------------------------------------------------------------- + +wxString wxStringTokenizer::GetNextToken() +{ + wxString token; + do { - if (!pos) + if ( !HasMoreTokens() ) { - pos++; - pos2 = 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 = pos; + // 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; + + // it wasn't terminated + m_lastDelim = _T('\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.assign(m_pos, tokenEnd); + + // skip the token and the trailing delimiter + m_pos = pos + 1; + + m_lastDelim = (pos == m_stringEnd) ? _T('\0') : (wxChar)*pos; } } - else + 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() ) { - pos2 = pos + 1; + tokens.Add(tk.GetNextToken()); } - r_string = m_string.Left((size_t)pos); - m_string = m_string.Mid((size_t)pos2); - - return r_string; + return tokens; }