• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/StringTokenizer.cpp

00001 /*
00002  * Copyright 1999-2004 The Apache Software Foundation.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 // Class header file.
00017 #include <spl/text/StringTokenizer.h>
00018 
00019 
00020 const char StringTokenizer::s_defaultTokens[] =
00021 {
00022         ' ',
00023         '\t',
00024         '\n',
00025         '\r',
00026         0,
00027 };
00028 
00029 StringTokenizer::StringTokenizer(
00030                         const String&   theString,
00031                         const String&   theTokens,
00032                         bool fReturnTokens) :
00033         m_string(theString),
00034         m_tokens(theTokens),
00035         m_returnTokens(fReturnTokens),
00036         m_currentIndex(0)
00037 {
00038         ASSERT(m_string.Length() != 0 && m_tokens.Length() != 0);
00039 }
00040 
00041 StringTokenizer::StringTokenizer(const StringTokenizer& st)
00042 :               m_string(st.m_string),
00043                 m_tokens(st.m_tokens),
00044                 m_returnTokens(st.m_returnTokens),
00045                 m_currentIndex(st.m_currentIndex)
00046 {
00047 }
00048 
00049 StringTokenizer& StringTokenizer::operator =(const StringTokenizer& st)
00050 {
00051         m_string = st.m_string;
00052         m_tokens = st.m_tokens;
00053         m_returnTokens = st.m_returnTokens;
00054         m_currentIndex = st.m_currentIndex;
00055 
00056         return *this;
00057 }
00058 
00059 StringTokenizer::~StringTokenizer()
00060 {
00061 }
00062 
00063 bool
00064 StringTokenizer::HasMoreTokens() const
00065 {
00066         int theCount = 0;
00067         int     theCurrentIndex = m_currentIndex;
00068 
00069         if (theCurrentIndex < m_string.Length())
00070         {
00071                 while(theCurrentIndex < m_string.Length())
00072                 {
00073                         const int theNextIndex = FindNextDelimiterIndex(theCurrentIndex);
00074 
00075                         if (theNextIndex == theCurrentIndex)
00076                         {
00077                                 theCurrentIndex = theNextIndex + 1;
00078 
00079                                 if (m_returnTokens == true)
00080                                 {
00081                                         return true;
00082                                 }
00083                         }
00084                         else
00085                         {
00086                                 return true;
00087                         }
00088                 }
00089         }
00090 
00091         return false;
00092 }
00093 
00094 void
00095 StringTokenizer::NextToken(String& theToken)
00096 {
00097         ASSERT(m_currentIndex < m_string.Length());
00098 
00099         // Find the index of the next delimiter.
00100         int     theIndex = FindNextDelimiterIndex(m_currentIndex);
00101 
00102         if (theIndex == m_currentIndex)
00103         {
00104                 m_currentIndex = theIndex + 1;
00105 
00106                 if (m_returnTokens == true)
00107                 {
00108                         // The next delimiter is at the current index.  If we're
00109                         // returning delimiters as tokens, then make that the
00110                         // return value.  Otherwise, return an empty string.
00111                         theToken = *m_string.Mid(theIndex, theIndex + 1);
00112                 }
00113                 else if (m_currentIndex < m_string.Length())
00114                 {
00115                          NextToken( theToken );
00116                 }
00117         }
00118         else
00119         {
00120                 if (theIndex == m_currentIndex)
00121                 {
00122                         theIndex = FindNextDelimiterIndex(m_currentIndex + 1);
00123                 }
00124                 ASSERT(theIndex > m_currentIndex);
00125 
00126                 theToken = *m_string.Mid(m_currentIndex, theIndex);
00127 
00128                 m_currentIndex = theIndex;
00129         }
00130 }
00131 
00132 int
00133 StringTokenizer::CountTokens() const
00134 {
00135         int theCount = 0;
00136         int     theCurrentIndex = m_currentIndex;
00137 
00138         if (theCurrentIndex < m_string.Length())
00139         {
00140                 while(theCurrentIndex < m_string.Length())
00141                 {
00142                         const int theNextIndex = FindNextDelimiterIndex(theCurrentIndex);
00143 
00144                         if (theNextIndex == theCurrentIndex)
00145                         {
00146                                 theCurrentIndex = theNextIndex + 1;
00147 
00148                                 if (m_returnTokens == true)
00149                                 {
00150                                         theCount++;
00151                                 }
00152                         }
00153                         else
00154                         {
00155                                 theCount++;
00156 
00157                                 theCurrentIndex = theNextIndex;
00158                         }
00159                 }
00160         }
00161 
00162         return theCount;
00163 }
00164 
00165 int
00166 StringTokenizer::FindNextDelimiterIndex(int     theStartIndex) const
00167 {
00168         bool fTokenFound = false;
00169         int     theIndex = theStartIndex;
00170 
00171         while(theIndex < m_string.Length() && fTokenFound == false)
00172         {
00173                 const char theCurrentChar = m_string[theIndex];
00174 
00175                 if (m_tokens.IndexOf(theCurrentChar) > -1)
00176                 {
00177                         fTokenFound = true;
00178                 }
00179                 else
00180                 {
00181                         theIndex++;
00182                 }
00183         }
00184 
00185         return theIndex;
00186 }
00187 
00188 #if defined(DEBUG) || defined(_DEBUG)
00189 void StringTokenizer::CheckMem() const
00190 {
00191         m_string.CheckMem();
00192         m_tokens.CheckMem();
00193 }
00194 
00195 void StringTokenizer::ValidateMem() const
00196 {
00197         m_string.ValidateMem();
00198         m_tokens.ValidateMem();
00199 }
00200 #endif
00201