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

CEGUIString.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIString.cpp
00003         created:        26/2/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements string 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 "CEGUIString.h"
00027 
00028 #include <iostream>
00029 
00030 // Start of CEGUI namespace section
00031 namespace CEGUI
00032 {
00033 
00034 // definition of 'no position' value
00035 const String::size_type String::npos = (String::size_type)(-1);
00036 
00037 
00039 // Destructor
00041 String::~String(void)
00042 {
00043         if (d_reserve > STR_QUICKBUFF_SIZE)
00044         {
00045                 delete[] d_buffer;
00046         }
00047                 if (d_encodedbufflen > 0)
00048         {
00049                 delete[] d_encodedbuff;
00050         }
00051 }
00052 
00053 bool String::grow(size_type new_size)
00054 {
00055     // check for too big
00056     if (max_size() <= new_size)
00057         std::length_error("Resulting CEGUI::String would be too big");
00058 
00059     // increase, as we always null-terminate the buffer.
00060     ++new_size;
00061 
00062     if (new_size > d_reserve)
00063     {
00064         utf32* temp = new utf32[new_size];
00065 
00066         if (d_reserve > STR_QUICKBUFF_SIZE)
00067         {
00068             memcpy(temp, d_buffer, (d_cplength + 1) * sizeof(utf32));
00069             delete[] d_buffer;
00070         }
00071         else
00072         {
00073             memcpy(temp, d_quickbuff, (d_cplength + 1) * sizeof(utf32));
00074         }
00075 
00076         d_buffer = temp;
00077         d_reserve = new_size;
00078 
00079         return true;
00080     }
00081 
00082     return false;
00083 }
00084 
00085 // perform re-allocation to remove wasted space.
00086 void String::trim(void)
00087 {
00088     size_type min_size = d_cplength + 1;
00089 
00090     // only re-allocate when not using quick-buffer, and when size can be trimmed
00091     if ((d_reserve > STR_QUICKBUFF_SIZE) && (d_reserve > min_size))
00092     {
00093             // see if we can trim to quick-buffer
00094         if (min_size <= STR_QUICKBUFF_SIZE)
00095         {
00096             memcpy(d_quickbuff, d_buffer, min_size * sizeof(utf32));
00097             delete[] d_buffer;
00098             d_reserve = STR_QUICKBUFF_SIZE;
00099         }
00100         // re-allocate buffer
00101         else
00102         {
00103             utf32* temp = new utf32[min_size];
00104             memcpy(temp, d_buffer, min_size * sizeof(utf32));
00105             delete[] d_buffer;
00106             d_buffer = temp;
00107             d_reserve = min_size;
00108         }
00109 
00110     }
00111 
00112 }
00113 
00114 // build an internal buffer with the string encoded as utf8 (remains valid until string is modified).
00115 utf8* String::build_utf8_buff(void) const
00116 {
00117     size_type buffsize = encoded_size(ptr(), d_cplength) + 1;
00118 
00119     if (buffsize > d_encodedbufflen) {
00120 
00121         if (d_encodedbufflen > 0)
00122         {
00123             delete[] d_encodedbuff;
00124         }
00125 
00126         d_encodedbuff = new utf8[buffsize];
00127         d_encodedbufflen = buffsize;
00128     }
00129 
00130     encode(ptr(), d_encodedbuff, buffsize, d_cplength);
00131 
00132     // always add a null at end
00133     d_encodedbuff[buffsize-1] = ((utf8)0);
00134     d_encodeddatlen = buffsize;
00135 
00136     return d_encodedbuff;
00137 }
00138 
00139 
00140 
00142 // Comparison operators
00144 bool    operator==(const String& str1, const String& str2)
00145 {
00146         return (str1.compare(str2) == 0);
00147 }
00148 
00149 bool    operator==(const String& str, const std::string& std_str)
00150 {
00151         return (str.compare(std_str) == 0);
00152 }
00153 
00154 bool    operator==(const std::string& std_str, const String& str)
00155 {
00156         return (str.compare(std_str) == 0);
00157 }
00158 
00159 bool    operator==(const String& str, const utf8* utf8_str)
00160 {
00161         return (str.compare(utf8_str) == 0);
00162 }
00163 
00164 bool    operator==(const utf8* utf8_str, const String& str)
00165 {
00166         return (str.compare(utf8_str) == 0);
00167 }
00168 
00169 
00170 bool    operator!=(const String& str1, const String& str2)
00171 {
00172         return (str1.compare(str2) != 0);
00173 }
00174 
00175 bool    operator!=(const String& str, const std::string& std_str)
00176 {
00177         return (str.compare(std_str) != 0);
00178 }
00179 
00180 bool    operator!=(const std::string& std_str, const String& str)
00181 {
00182         return (str.compare(std_str) != 0);
00183 }
00184 
00185 bool    operator!=(const String& str, const utf8* utf8_str)
00186 {
00187         return (str.compare(utf8_str) != 0);
00188 }
00189 
00190 bool    operator!=(const utf8* utf8_str, const String& str)
00191 {
00192         return (str.compare(utf8_str) != 0);
00193 }
00194 
00195 
00196 bool    operator<(const String& str1, const String& str2)
00197 {
00198         return (str1.compare(str2) < 0);
00199 }
00200 
00201 bool    operator<(const String& str, const std::string& std_str)
00202 {
00203         return (str.compare(std_str) < 0);
00204 }
00205 
00206 bool    operator<(const std::string& std_str, const String& str)
00207 {
00208         return (str.compare(std_str) >= 0);
00209 }
00210 
00211 bool    operator<(const String& str, const utf8* utf8_str)
00212 {
00213         return (str.compare(utf8_str) < 0);
00214 }
00215 
00216 bool    operator<(const utf8* utf8_str, const String& str)
00217 {
00218         return (str.compare(utf8_str) >= 0);
00219 }
00220 
00221 
00222 bool    operator>(const String& str1, const String& str2)
00223 {
00224         return (str1.compare(str2) > 0);
00225 }
00226 
00227 bool    operator>(const String& str, const std::string& std_str)
00228 {
00229         return (str.compare(std_str) > 0);
00230 }
00231 
00232 bool    operator>(const std::string& std_str, const String& str)
00233 {
00234         return (str.compare(std_str) <= 0);
00235 }
00236 
00237 bool    operator>(const String& str, const utf8* utf8_str)
00238 {
00239         return (str.compare(utf8_str) > 0);
00240 }
00241 
00242 bool    operator>(const utf8* utf8_str, const String& str)
00243 {
00244         return (str.compare(utf8_str) <= 0);
00245 }
00246 
00247 
00248 bool    operator<=(const String& str1, const String& str2)
00249 {
00250         return (str1.compare(str2) <= 0);
00251 }
00252 
00253 bool    operator<=(const String& str, const std::string& std_str)
00254 {
00255         return (str.compare(std_str) <= 0);
00256 }
00257 
00258 bool    operator<=(const std::string& std_str, const String& str)
00259 {
00260         return (str.compare(std_str) >= 0);
00261 }
00262 
00263 bool    operator<=(const String& str, const utf8* utf8_str)
00264 {
00265         return (str.compare(utf8_str) <= 0);
00266 }
00267 
00268 bool    operator<=(const utf8* utf8_str, const String& str)
00269 {
00270         return (str.compare(utf8_str) >= 0);
00271 }
00272 
00273 
00274 bool    operator>=(const String& str1, const String& str2)
00275 {
00276         return (str1.compare(str2) >= 0);
00277 }
00278 
00279 bool    operator>=(const String& str, const std::string& std_str)
00280 {
00281         return (str.compare(std_str) >= 0);
00282 }
00283 
00284 bool    operator>=(const std::string& std_str, const String& str)
00285 {
00286         return (str.compare(std_str) <= 0);
00287 }
00288 
00289 bool    operator>=(const String& str, const utf8* utf8_str)
00290 {
00291         return (str.compare(utf8_str) >= 0);
00292 }
00293 
00294 bool    operator>=(const utf8* utf8_str, const String& str)
00295 {
00296         return (str.compare(utf8_str) <= 0);
00297 }
00298 
00300 // c-string operators
00302 bool operator==(const String& str, const char* c_str)
00303 {
00304         return (str.compare(c_str) == 0);
00305 }
00306 
00307 bool operator==(const char* c_str, const String& str)
00308 {
00309         return (str.compare(c_str) == 0);
00310 }
00311 
00312 bool operator!=(const String& str, const char* c_str)
00313 {
00314         return (str.compare(c_str) != 0);
00315 }
00316 
00317 bool operator!=(const char* c_str, const String& str)
00318 {
00319         return (str.compare(c_str) != 0);
00320 }
00321 
00322 bool operator<(const String& str, const char* c_str)
00323 {
00324         return (str.compare(c_str) < 0);
00325 }
00326 
00327 bool operator<(const char* c_str, const String& str)
00328 {
00329         return (str.compare(c_str) >= 0);
00330 }
00331 
00332 bool operator>(const String& str, const char* c_str)
00333 {
00334         return (str.compare(c_str) > 0);
00335 }
00336 
00337 bool operator>(const char* c_str, const String& str)
00338 {
00339         return (str.compare(c_str) <= 0);
00340 }
00341 
00342 bool operator<=(const String& str, const char* c_str)
00343 {
00344         return (str.compare(c_str) <= 0);
00345 }
00346 
00347 bool operator<=(const char* c_str, const String& str)
00348 {
00349         return (str.compare(c_str) >= 0);
00350 }
00351 
00352 bool operator>=(const String& str, const char* c_str)
00353 {
00354         return (str.compare(c_str) >= 0);
00355 }
00356 
00357 bool operator>=(const char* c_str, const String& str)
00358 {
00359         return (str.compare(c_str) <= 0);
00360 }
00361 
00363 // Concatenation operator functions
00365 String  operator+(const String& str1, const String& str2)
00366 {
00367         String temp(str1);
00368         temp.append(str2);
00369         return temp;
00370 }
00371 
00372 String  operator+(const String& str, const std::string& std_str)
00373 {
00374         String temp(str);
00375         temp.append(std_str);
00376         return temp;
00377 }
00378 
00379 String  operator+(const std::string& std_str, const String& str)
00380 {
00381         String temp(std_str);
00382         temp.append(str);
00383         return temp;
00384 }
00385 
00386 String  operator+(const String& str, const utf8* utf8_str)
00387 {
00388         String temp(str);
00389         temp.append(utf8_str);
00390         return temp;
00391 }
00392 
00393 String  operator+(const utf8* utf8_str, const String& str)
00394 {
00395         String temp(utf8_str);
00396         temp.append(str);
00397         return temp;
00398 }
00399 
00400 String  operator+(const String& str, utf32 code_point)
00401 {
00402         String temp(str);
00403         temp.append(1, code_point);
00404         return temp;
00405 }
00406 
00407 String  operator+(utf32 code_point, const String& str)
00408 {
00409         String temp(1, code_point);
00410         temp.append(str);
00411         return temp;
00412 }
00413 
00414 String operator+(const String& str, const char* c_str)
00415 {
00416         String tmp(str);
00417         tmp.append(c_str);
00418         return tmp;
00419 }
00420 
00421 String operator+(const char* c_str, const String& str)
00422 {
00423         String tmp(c_str);
00424         tmp.append(str);
00425         return tmp;
00426 }
00427 
00429 // Output (stream) functions
00431 std::ostream& operator<<(std::ostream& s, const String& str)
00432 {
00433         return s << str.c_str();
00434 }
00435 
00437 // Modifying operations
00439 // swap the contents of str1 and str2
00440 void    swap(String& str1, String& str2)
00441 {
00442         str1.swap(str2);
00443 }
00444 
00445 
00446 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:34 2005 for Crazy Eddies GUI System by  doxygen 1.4.3