Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUITextUtils.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUITextUtils.cpp
00003         created:        30/5/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implementation of text support class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUITextUtils.h"
00027 
00028 // Start of CEGUI namespace section
00029 namespace CEGUI
00030 {
00031 /*************************************************************************
00032         Constants
00033 *************************************************************************/
00034 const String    TextUtils::DefaultWhitespace            = (utf8*)" \n\t\r";
00035 const String    TextUtils::DefaultAlphanumerical        = (utf8*)"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
00036 const String    TextUtils::DefaultWrapDelimiters        = (utf8*)" \n\t\r";
00037 
00038 
00039 /*************************************************************************
00040         return a String containing the the next word in a String.
00041 *************************************************************************/
00042 String TextUtils::getNextWord(const String& str, String::size_type start_idx, const String& delimiters)
00043 {
00044         String::size_type       word_start = str.find_first_not_of(delimiters, start_idx);
00045 
00046         if (word_start == String::npos)
00047         {
00048                 word_start = start_idx;
00049         }
00050 
00051         String::size_type       word_end = str.find_first_of(delimiters, word_start);
00052 
00053         if (word_end == String::npos)
00054         {
00055                 word_end = str.length();
00056         }
00057 
00058         return str.substr(start_idx, (word_end - start_idx));
00059 }
00060 
00061 
00062 /*************************************************************************
00063         Return the index of the first character of the word at 'idx'.   
00064 *************************************************************************/
00065 String::size_type TextUtils::getWordStartIdx(const String& str, String::size_type idx)
00066 {
00067         String  temp = str.substr(0, idx);
00068 
00069         trimTrailingChars(temp, DefaultWhitespace);
00070 
00071         if (temp.length() <= 1) {
00072                 return 0;
00073         }
00074 
00075         // identify the type of character at 'pos'
00076         if (String::npos != DefaultAlphanumerical.find(temp[temp.length() - 1]))
00077         {
00078                 idx = temp.find_last_not_of(DefaultAlphanumerical);
00079         }
00080         // since whitespace was stripped, character must be a symbol
00081         else
00082         {
00083                 idx = temp.find_last_of(DefaultAlphanumerical + DefaultWhitespace);
00084         }
00085 
00086         // make sure we do not go past end of string (+1)
00087         if (idx == String::npos)
00088         {
00089                 return 0;
00090         }
00091         else
00092         {
00093                 return idx + 1;
00094         }
00095 
00096 }
00097 
00098 
00099 /*************************************************************************
00100         Return the index of the first character of the word after the word
00101         at 'idx'.       
00102 *************************************************************************/
00103 String::size_type TextUtils::getNextWordStartIdx(const String& str, String::size_type idx)
00104 {
00105         String::size_type str_len = str.length();
00106 
00107         // do some checks for simple cases
00108         if ((idx >= str_len) || (str_len == 0))
00109         {
00110                 return str_len;
00111         }
00112 
00113         // is character at 'idx' alphanumeric
00114         if (String::npos != DefaultAlphanumerical.find(str[idx]))
00115         {
00116                 // find position of next character that is not alphanumeric
00117                 idx = str.find_first_not_of(DefaultAlphanumerical, idx);
00118         }
00119         // is character also not whitespace (therefore a symbol)
00120         else if (String::npos == DefaultWhitespace.find(str[idx]))
00121         {
00122                 // find index of next character that is either alphanumeric or whitespace
00123                 idx = str.find_first_of(DefaultAlphanumerical + DefaultWhitespace, idx);
00124         }
00125 
00126         // check result at this stage.
00127         if (String::npos == idx)
00128         {
00129                 idx = str_len;
00130         }
00131         else
00132         {
00133                 // if character at 'idx' is whitespace
00134                 if (String::npos != DefaultWhitespace.find(str[idx]))
00135                 {
00136                         // find next character that is not whitespace
00137                         idx = str.find_first_not_of(DefaultWhitespace, idx);
00138                 }
00139 
00140                 if (String::npos == idx)
00141                 {
00142                         idx = str_len;
00143                 }
00144 
00145         }
00146 
00147         return idx;
00148 }
00149 
00150 
00151 /*************************************************************************
00152         Trim all characters from the set specified in \a chars from the
00153         begining of 'str'.      
00154 *************************************************************************/
00155 void TextUtils::trimLeadingChars(String& str, const String& chars)
00156 {
00157         String::size_type idx = str.find_first_not_of(chars);
00158 
00159         if (idx != String::npos)
00160         {
00161                 str.erase(0, idx);
00162         }
00163         else
00164         {
00165                 str.erase();
00166         }
00167 
00168 }
00169 
00170 
00171 /*************************************************************************
00172         Trim all characters from the set specified in \a chars from the end
00173         of 'str'.       
00174 *************************************************************************/
00175 void TextUtils::trimTrailingChars(String& str, const String& chars)
00176 {
00177         String::size_type idx = str.find_last_not_of(chars);
00178 
00179         if (idx != String::npos)
00180         {
00181                 str.resize(idx + 1);
00182         }
00183         else
00184         {
00185                 str.erase();
00186         }
00187 
00188 }
00189 
00190 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:07 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1