00001 00002 // 00003 // SFML - Simple and Fast Multimedia Library 00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) 00005 // 00006 // This software is provided 'as-is', without any express or implied warranty. 00007 // In no event will the authors be held liable for any damages arising from the use of this software. 00008 // 00009 // Permission is granted to anyone to use this software for any purpose, 00010 // including commercial applications, and to alter it and redistribute it freely, 00011 // subject to the following restrictions: 00012 // 00013 // 1. The origin of this software must not be misrepresented; 00014 // you must not claim that you wrote the original software. 00015 // If you use this software in a product, an acknowledgment 00016 // in the product documentation would be appreciated but is not required. 00017 // 00018 // 2. Altered source versions must be plainly marked as such, 00019 // and must not be misrepresented as being the original software. 00020 // 00021 // 3. This notice may not be removed or altered from any source distribution. 00022 // 00024 00026 // Headers 00028 #include <SFML/System/String.hpp> 00029 #include <SFML/System/Utf.hpp> 00030 #include <iterator> 00031 #include <string.h> 00032 00033 00034 namespace sf 00035 { 00037 // Static member data 00039 const std::size_t String::InvalidPos = std::basic_string<Uint32>::npos; 00040 00041 00043 String::String() 00044 { 00045 } 00046 00047 00049 String::String(char ansiChar) 00050 { 00051 myString += Utf32::DecodeAnsi(ansiChar); 00052 } 00053 00054 00056 String::String(char ansiChar, const std::locale& locale) 00057 { 00058 myString += Utf32::DecodeAnsi(ansiChar, locale); 00059 } 00060 00061 00063 String::String(wchar_t wideChar) 00064 { 00065 myString += Utf32::DecodeWide(wideChar); 00066 } 00067 00068 00070 String::String(Uint32 utf32Char) 00071 { 00072 myString += utf32Char; 00073 } 00074 00075 00077 String::String(const char* ansiString) 00078 { 00079 if (ansiString) 00080 { 00081 std::size_t length = strlen(ansiString); 00082 if (length > 0) 00083 { 00084 myString.reserve(length + 1); 00085 Utf32::FromAnsi(ansiString, ansiString + length, std::back_inserter(myString)); 00086 } 00087 } 00088 } 00089 00090 00092 String::String(const std::string& ansiString) 00093 { 00094 myString.reserve(ansiString.length() + 1); 00095 Utf32::FromAnsi(ansiString.begin(), ansiString.end(), std::back_inserter(myString)); 00096 } 00097 00098 00100 String::String(const char* ansiString, const std::locale& locale) 00101 { 00102 if (ansiString) 00103 { 00104 std::size_t length = strlen(ansiString); 00105 if (length > 0) 00106 { 00107 myString.reserve(length + 1); 00108 Utf32::FromAnsi(ansiString, ansiString + length, std::back_inserter(myString), locale); 00109 } 00110 } 00111 } 00112 00113 00115 String::String(const std::string& ansiString, const std::locale& locale) 00116 { 00117 myString.reserve(ansiString.length() + 1); 00118 Utf32::FromAnsi(ansiString.begin(), ansiString.end(), std::back_inserter(myString), locale); 00119 } 00120 00121 00123 String::String(const wchar_t* wideString) 00124 { 00125 if (wideString) 00126 { 00127 std::size_t length = wcslen(wideString); 00128 if (length > 0) 00129 { 00130 myString.reserve(length + 1); 00131 Utf32::FromWide(wideString, wideString + length, std::back_inserter(myString)); 00132 } 00133 } 00134 } 00135 00136 00138 String::String(const std::wstring& wideString) 00139 { 00140 myString.reserve(wideString.length() + 1); 00141 Utf32::FromWide(wideString.begin(), wideString.end(), std::back_inserter(myString)); 00142 } 00143 00144 00146 String::String(const Uint32* utf32String) 00147 { 00148 if (utf32String) 00149 myString = utf32String; 00150 } 00151 00152 00154 String::String(const std::basic_string<Uint32>& utf32String) : 00155 myString(utf32String) 00156 { 00157 } 00158 00159 00161 String::String(const String& copy) : 00162 myString(copy.myString) 00163 { 00164 } 00165 00166 00168 String::operator std::string() const 00169 { 00170 return ToAnsiString(); 00171 } 00172 00173 00175 String::operator std::wstring() const 00176 { 00177 return ToWideString(); 00178 } 00179 00180 00182 std::string String::ToAnsiString() const 00183 { 00184 // Prepare the output string 00185 std::string output; 00186 output.reserve(myString.length() + 1); 00187 00188 // Convert 00189 Utf32::ToAnsi(myString.begin(), myString.end(), std::back_inserter(output), 0); 00190 00191 return output; 00192 } 00193 00194 00196 std::string String::ToAnsiString(const std::locale& locale) const 00197 { 00198 // Prepare the output string 00199 std::string output; 00200 output.reserve(myString.length() + 1); 00201 00202 // Convert 00203 Utf32::ToAnsi(myString.begin(), myString.end(), std::back_inserter(output), 0, locale); 00204 00205 return output; 00206 } 00207 00208 00210 std::wstring String::ToWideString() const 00211 { 00212 // Prepare the output string 00213 std::wstring output; 00214 output.reserve(myString.length() + 1); 00215 00216 // Convert 00217 Utf32::ToWide(myString.begin(), myString.end(), std::back_inserter(output), 0); 00218 00219 return output; 00220 } 00221 00222 00224 String& String::operator =(const String& right) 00225 { 00226 myString = right.myString; 00227 return *this; 00228 } 00229 00230 00232 String& String::operator +=(const String& right) 00233 { 00234 myString += right.myString; 00235 return *this; 00236 } 00237 00238 00240 Uint32 String::operator [](std::size_t index) const 00241 { 00242 return myString[index]; 00243 } 00244 00245 00247 Uint32& String::operator [](std::size_t index) 00248 { 00249 return myString[index]; 00250 } 00251 00252 00254 void String::Clear() 00255 { 00256 myString.clear(); 00257 } 00258 00259 00261 std::size_t String::GetSize() const 00262 { 00263 return myString.size(); 00264 } 00265 00266 00268 bool String::IsEmpty() const 00269 { 00270 return myString.empty(); 00271 } 00272 00273 00275 void String::Erase(std::size_t position, std::size_t count) 00276 { 00277 myString.erase(position, count); 00278 } 00279 00280 00282 void String::Insert(std::size_t position, const String& str) 00283 { 00284 myString.insert(position, str.myString); 00285 } 00286 00287 00289 std::size_t String::Find(const String& str, std::size_t start) const 00290 { 00291 return myString.find(str.myString, start); 00292 } 00293 00294 00296 const Uint32* String::GetData() const 00297 { 00298 return myString.c_str(); 00299 } 00300 00301 00303 String::Iterator String::Begin() 00304 { 00305 return myString.begin(); 00306 } 00307 00308 00310 String::ConstIterator String::Begin() const 00311 { 00312 return myString.begin(); 00313 } 00314 00315 00317 String::Iterator String::End() 00318 { 00319 return myString.end(); 00320 } 00321 00322 00324 String::ConstIterator String::End() const 00325 { 00326 return myString.end(); 00327 } 00328 00329 00331 bool operator ==(const String& left, const String& right) 00332 { 00333 return left.myString == right.myString; 00334 } 00335 00336 00338 bool operator !=(const String& left, const String& right) 00339 { 00340 return !(left == right); 00341 } 00342 00343 00345 bool operator <(const String& left, const String& right) 00346 { 00347 return left.myString < right.myString; 00348 } 00349 00350 00352 bool operator >(const String& left, const String& right) 00353 { 00354 return right < left; 00355 } 00356 00357 00359 bool operator <=(const String& left, const String& right) 00360 { 00361 return !(right < left); 00362 } 00363 00364 00366 bool operator >=(const String& left, const String& right) 00367 { 00368 return !(left < right); 00369 } 00370 00371 00373 String operator +(const String& left, const String& right) 00374 { 00375 String string = left; 00376 string += right; 00377 00378 return string; 00379 } 00380 00381 } // namespace sf
:: Copyright © 2007-2008 Laurent Gomila, all rights reserved :: Documentation generated by doxygen 1.5.2 ::