new wxStringTokenizer
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5763 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
// Name: tokenzr.cpp
|
// Name: tokenzr.cpp
|
||||||
// Purpose: String tokenizer
|
// Purpose: String tokenizer
|
||||||
// Author: Guilhem Lavaux
|
// Author: Guilhem Lavaux
|
||||||
// Modified by:
|
// Modified by: Gregory Pietsch
|
||||||
// Created: 04/22/98
|
// Created: 04/22/98
|
||||||
// RCS-ID: $Id$
|
// RCS-ID: $Id$
|
||||||
// Copyright: (c) Guilhem Lavaux
|
// Copyright: (c) Guilhem Lavaux
|
||||||
@@ -29,110 +29,79 @@ wxStringTokenizer::wxStringTokenizer(const wxString& to_tokenize,
|
|||||||
m_string = to_tokenize;
|
m_string = to_tokenize;
|
||||||
m_delims = delims;
|
m_delims = delims;
|
||||||
m_retdelims = ret_delims;
|
m_retdelims = ret_delims;
|
||||||
|
m_pos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
wxStringTokenizer::~wxStringTokenizer()
|
wxStringTokenizer::~wxStringTokenizer()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
off_t wxStringTokenizer::FindDelims(const wxString& str, const wxString& delims) const
|
|
||||||
{
|
|
||||||
for ( size_t i = 0; i < str.Length(); i++ )
|
|
||||||
{
|
|
||||||
wxChar c = str[i];
|
|
||||||
|
|
||||||
for ( size_t j = 0; j < delims.Length() ; j++ )
|
|
||||||
{
|
|
||||||
if ( delims[j] == c )
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int wxStringTokenizer::CountTokens() const
|
int wxStringTokenizer::CountTokens() const
|
||||||
{
|
{
|
||||||
wxString p_string = m_string;
|
size_t pos = 0;
|
||||||
bool found = TRUE;
|
int count = 0;
|
||||||
int pos, count = 1;
|
bool at_delim;
|
||||||
|
|
||||||
if (p_string.Length() == 0)
|
while (pos < m_string.length()) {
|
||||||
return 0;
|
// while we're still counting ...
|
||||||
|
at_delim = (m_delims.find(m_string.at(pos)) < m_delims.length());
|
||||||
while (found)
|
// are we at a delimiter? if so, move to the next nondelimiter;
|
||||||
{
|
// if not, move to the next delimiter. If the find_first_of
|
||||||
pos = FindDelims(p_string, m_delims);
|
// and find_first_not_of methods fail, pos will be assigned
|
||||||
if (pos != -1)
|
// npos (0xFFFFFFFF) which will terminate the loop on the next
|
||||||
|
// go-round unless we have a really long string, which is unlikely
|
||||||
|
pos = at_delim ? m_string.find_first_not_of(m_delims, pos)
|
||||||
|
: m_string.find_first_of(m_delims, pos);
|
||||||
|
if (m_retdelims)
|
||||||
{
|
{
|
||||||
|
// if we're retaining delimiters, increment count
|
||||||
count++;
|
count++;
|
||||||
p_string = p_string(pos+1, p_string.Length());
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
found = FALSE;
|
// if we're not retaining delimiters and at a token, inc count
|
||||||
|
count += (!at_delim);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxStringTokenizer::HasMoreTokens()
|
bool wxStringTokenizer::HasMoreTokens()
|
||||||
{
|
{
|
||||||
return !m_string.IsEmpty();
|
return (m_retdelims
|
||||||
}
|
? !m_string.IsEmpty()
|
||||||
|
: m_string.find_first_not_of(m_delims) < m_string.length());
|
||||||
// needed to fix leading whitespace / mult. delims bugs
|
|
||||||
void wxStringTokenizer::EatLeadingDelims()
|
|
||||||
{
|
|
||||||
int pos;
|
|
||||||
|
|
||||||
// while leading delims trim 'em from the left
|
|
||||||
while ( ( pos = FindDelims(m_string, m_delims)) == 0 )
|
|
||||||
{
|
|
||||||
m_string = m_string.Mid((size_t)1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxString wxStringTokenizer::NextToken()
|
wxString wxStringTokenizer::NextToken()
|
||||||
{
|
{
|
||||||
off_t pos, pos2;
|
size_t pos;
|
||||||
wxString r_string;
|
wxString r_string;
|
||||||
|
|
||||||
if ( m_string.IsEmpty() )
|
if ( m_string.IsEmpty() )
|
||||||
return m_string;
|
return m_string;
|
||||||
|
pos = m_string.find_first_not_of(m_delims);
|
||||||
if ( !m_retdelims )
|
if ( m_retdelims ) {
|
||||||
EatLeadingDelims();
|
// we're retaining delimiters (unusual behavior, IMHO)
|
||||||
|
if (pos == 0)
|
||||||
pos = FindDelims(m_string, m_delims);
|
// first char is a non-delimiter
|
||||||
if (pos == -1)
|
pos = m_string.find_first_of(m_delims);
|
||||||
{
|
} else {
|
||||||
|
// we're not retaining delimiters
|
||||||
|
m_string.erase(0, pos);
|
||||||
|
m_pos += pos;
|
||||||
|
if (m_string.IsEmpty())
|
||||||
|
return m_string;
|
||||||
|
pos = m_string.find_first_of(m_delims);
|
||||||
|
}
|
||||||
|
if (pos <= m_string.length()) {
|
||||||
|
r_string = m_string.substr(0, pos);
|
||||||
|
m_string.erase(0, pos);
|
||||||
|
m_pos += pos;
|
||||||
|
} else {
|
||||||
r_string = m_string;
|
r_string = m_string;
|
||||||
m_string = wxEmptyString;
|
m_pos += m_string.length();
|
||||||
|
m_string.Empty();
|
||||||
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;
|
return r_string;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user