X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ce3ed50dbe32d118321082db84c3a9abb047d5b7..6143d3b61e311ae647577c284eaf41dded4e2b3c:/src/common/tokenzr.cpp diff --git a/src/common/tokenzr.cpp b/src/common/tokenzr.cpp index 34079c0f66..43641b726e 100644 --- a/src/common/tokenzr.cpp +++ b/src/common/tokenzr.cpp @@ -2,113 +2,222 @@ // Name: 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" +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) + #pragma implementation "tokenzr.h" #endif // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#ifndef WX_PRECOMP -#endif - -#include "wx/object.h" -#include "wx/string.h" #include "wx/tokenzr.h" +#include "wx/arrstr.h" + +// Required for wxIs... functions +#include -wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize, +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxStringTokenizer construction +// ---------------------------------------------------------------------------- + +wxStringTokenizer::wxStringTokenizer(const wxString& str, const wxString& delims, - bool ret_delims) - : wxObject() + wxStringTokenizerMode mode) +{ + SetString(str, delims, mode); +} + +void wxStringTokenizer::SetString(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) { - m_string = to_tokenize; - m_delims = delims; - m_retdelims = ret_delims; + 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) + const wxChar *p; + for ( p = delims.c_str(); *p; p++ ) + { + if ( !wxIsspace(*p) ) + break; + } + + if ( *p ) + { + // not whitespace char in delims + mode = wxTOKEN_RET_EMPTY; + } + else + { + // only whitespaces + mode = wxTOKEN_STRTOK; + } + } + + m_delims = delims; + m_mode = mode; + + Reinit(str); } -wxStringTokenizer::~wxStringTokenizer() +void wxStringTokenizer::Reinit(const wxString& str) { + wxASSERT_MSG( IsOk(), _T("you should call SetString() first") ); + + m_string = str; + m_pos = 0; + + // empty string doesn't have any tokens + m_hasMore = !m_string.empty(); } -off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims) +// ---------------------------------------------------------------------------- +// access to the tokens +// ---------------------------------------------------------------------------- + +// do we have more of them? +bool wxStringTokenizer::HasMoreTokens() const { - int i, j; - register int s_len = str.Length(), - len = delims.Length(); - - for (i=0;iHasMoreTokens() ) + { + count++; + + (void)self->GetNextToken(); + } + + self->Reinit(stringInitial); + + return count; } -bool wxStringTokenizer::HasMoreToken() +// ---------------------------------------------------------------------------- +// token extraction +// ---------------------------------------------------------------------------- + +wxString wxStringTokenizer::GetNextToken() { - return (m_string.Length() != 0); + // strtok() doesn't return empty tokens, all other modes do + bool allowEmpty = m_mode != wxTOKEN_STRTOK; + + wxString token; + do + { + if ( !HasMoreTokens() ) + { + break; + } + // find the end of this token + size_t pos = m_string.find_first_of(m_delims); + + // and the start of the next one + if ( pos == wxString::npos ) + { + // no more delimiters, the token is everything till the end of + // string + token = m_string; + + m_pos += m_string.length(); + m_string.clear(); + + // 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; + } + else + { + size_t pos2 = pos + 1; + + // in wxTOKEN_RET_DELIMS mode we return the delimiter character + // with token + token = wxString(m_string, m_mode == wxTOKEN_RET_DELIMS ? pos2 + : pos); + + // remove token with the following it delimiter from string + m_string.erase(0, pos2); + + // keep track of the position in the original string too + m_pos += pos2; + } + } + while ( !allowEmpty && token.empty() ); + + return token; } -wxString wxStringTokenizer::NextToken() +// ---------------------------------------------------------------------------- +// public functions +// ---------------------------------------------------------------------------- + +wxArrayString wxStringTokenize(const wxString& str, + const wxString& delims, + wxStringTokenizerMode mode) { - register off_t pos, pos2; - wxString r_string; - - if (m_string.IsNull()) - return m_string; - - pos = FindDelims(m_string, m_delims); - if (pos == -1) { - r_string = m_string; - m_string = wxEmptyString; - - return r_string; - } - - if (m_retdelims) { - if (!pos) { - pos++; - pos2 = 1; - } else - pos2 = pos; - } else - pos2 = pos + 1; - - r_string = m_string.Left((size_t)pos); - m_string = m_string.Mid((size_t)pos2); - - return r_string; + wxArrayString tokens; + wxStringTokenizer tk(str, delims, mode); + while ( tk.HasMoreTokens() ) + { + tokens.Add(tk.GetNextToken()); + } + + return tokens; }