00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "CEGUITextUtils.h"
00027
00028
00029 namespace CEGUI
00030 {
00031
00032
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
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
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
00076 if (String::npos != DefaultAlphanumerical.find(temp[temp.length() - 1]))
00077 {
00078 idx = temp.find_last_not_of(DefaultAlphanumerical);
00079 }
00080
00081 else
00082 {
00083 idx = temp.find_last_of(DefaultAlphanumerical + DefaultWhitespace);
00084 }
00085
00086
00087 if (idx == String::npos)
00088 {
00089 return 0;
00090 }
00091 else
00092 {
00093 return idx + 1;
00094 }
00095
00096 }
00097
00098
00099
00100
00101
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
00108 if ((idx >= str_len) || (str_len == 0))
00109 {
00110 return str_len;
00111 }
00112
00113
00114 if (String::npos != DefaultAlphanumerical.find(str[idx]))
00115 {
00116
00117 idx = str.find_first_not_of(DefaultAlphanumerical, idx);
00118 }
00119
00120 else if (String::npos == DefaultWhitespace.find(str[idx]))
00121 {
00122
00123 idx = str.find_first_of(DefaultAlphanumerical + DefaultWhitespace, idx);
00124 }
00125
00126
00127 if (String::npos == idx)
00128 {
00129 idx = str_len;
00130 }
00131 else
00132 {
00133
00134 if (String::npos != DefaultWhitespace.find(str[idx]))
00135 {
00136
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
00153
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
00173
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 }