From bb414773d192fac8cecd6b586a0130bdc0196d71 Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Fri, 26 Feb 2010 09:36:38 +0000 Subject: [PATCH] Added Insert and Find functions to sf::String Added implicit constructors to sf::String for converting from single characters (char, wchar_t and Uint32) git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1429 4e206d99-4929-0410-ac5d-dfc041789085 --- include/SFML/System/String.hpp | 145 +++++++++++++++++++-------------- src/SFML/Graphics/Shader.cpp | 1 + src/SFML/System/String.cpp | 102 +++++++++++------------ 3 files changed, 131 insertions(+), 117 deletions(-) diff --git a/include/SFML/System/String.hpp b/include/SFML/System/String.hpp index 90a9eff4d..729aa2d68 100644 --- a/include/SFML/System/String.hpp +++ b/include/SFML/System/String.hpp @@ -50,6 +50,11 @@ public : typedef std::basic_string::iterator Iterator; ///< Iterator type typedef std::basic_string::const_iterator ConstIterator; ///< Constant iterator type + //////////////////////////////////////////////////////////// + // Static member data + //////////////////////////////////////////////////////////// + static const std::size_t InvalidPos; ///< Represents an invalid position in the string + //////////////////////////////////////////////////////////// /// \brief Default constructor /// @@ -58,6 +63,47 @@ public : //////////////////////////////////////////////////////////// String(); + //////////////////////////////////////////////////////////// + /// \brief Construct from a single ANSI character + /// + /// The source character is converted to UTF-32 according + /// to the current locale. See the other constructor for + /// explicitely passing the locale to use. + /// + /// \param ansiString ANSI character to convert + /// + //////////////////////////////////////////////////////////// + String(char ansiChar); + + //////////////////////////////////////////////////////////// + /// \brief Construct from a single ANSI character and a locale + /// + /// The source character is converted to UTF-32 according + /// to the given locale. If you want to use the current global + /// locale, rather use the other constructor. + /// + /// \param ansiChar ANSI character to convert + /// \param locale Locale to use for conversion + /// + //////////////////////////////////////////////////////////// + String(char ansiChar, const std::locale& locale); + + //////////////////////////////////////////////////////////// + /// \brief Construct from single wide character + /// + /// \param wideChar Wide character to convert + /// + //////////////////////////////////////////////////////////// + String(wchar_t wideChar); + + //////////////////////////////////////////////////////////// + /// \brief Construct from single UTF-32 character + /// + /// \param utf32Char UTF-32 character to convert + /// + //////////////////////////////////////////////////////////// + String(Uint32 utf32Char); + //////////////////////////////////////////////////////////// /// \brief Construct from a null-terminated C-style ANSI string /// @@ -160,6 +206,8 @@ public : /// /// \return Converted ANSI string /// + /// \see ToAnsiString, operator std::wstring + /// //////////////////////////////////////////////////////////// operator std::string() const; @@ -173,6 +221,8 @@ public : /// /// \return Converted wide string /// + /// \see ToWideString, operator std::string + /// //////////////////////////////////////////////////////////// operator std::wstring() const; @@ -187,6 +237,8 @@ public : /// /// \return Converted ANSI string /// + /// \see ToWideString, operator std::string + /// //////////////////////////////////////////////////////////// std::string ToAnsiString() const; @@ -204,6 +256,8 @@ public : /// /// \return Converted ANSI string /// + /// \see ToWideString, operator std::string + /// //////////////////////////////////////////////////////////// std::string ToAnsiString(const std::locale& locale) const; @@ -215,6 +269,8 @@ public : /// /// \return Converted wide string /// + /// \see ToAnsiString, operator std::wstring + /// //////////////////////////////////////////////////////////// std::wstring ToWideString() const; @@ -228,36 +284,6 @@ public : //////////////////////////////////////////////////////////// String& operator =(const String& right); - //////////////////////////////////////////////////////////// - /// \brief Overload of += operator to append an ANSI character - /// - /// \param right Character to append - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - String& operator +=(char right); - - //////////////////////////////////////////////////////////// - /// \brief Overload of += operator to append a wide character - /// - /// \param right Character to append - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - String& operator +=(wchar_t right); - - //////////////////////////////////////////////////////////// - /// \brief Overload of += operator to append an UTF-32 character - /// - /// \param right Character to append - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - String& operator +=(Uint32 right); - //////////////////////////////////////////////////////////// /// \brief Overload of += operator to append an UTF-32 string /// @@ -336,6 +362,32 @@ public : //////////////////////////////////////////////////////////// void Erase(std::size_t position, std::size_t count = 1); + //////////////////////////////////////////////////////////// + /// \brief Insert one or more characters into the string + /// + /// This function inserts the characters of \a str + /// into the string, starting from \a position. + /// + /// \param position Position of insertion + /// \param str Characters to insert + /// + //////////////////////////////////////////////////////////// + void Insert(std::size_t position, const String& str); + + //////////////////////////////////////////////////////////// + /// \brief Find a sequence of one or more characters in the string + /// + /// This function searches for the characters of \a str + /// into the string, starting from \a start. + /// + /// \param str Characters to find + /// \param start Where to begin searching + /// + /// \return Position of \a str in the string, or String::InvalidPos if not found + /// + //////////////////////////////////////////////////////////// + std::size_t Find(const String& str, std::size_t start = 0) const; + //////////////////////////////////////////////////////////// /// \brief Get a pointer to the C-style array of characters /// @@ -474,39 +526,6 @@ SFML_API bool operator <=(const String& left, const String& right); //////////////////////////////////////////////////////////// SFML_API bool operator >=(const String& left, const String& right); -//////////////////////////////////////////////////////////// -/// \brief Overload of binary + operator to concatenate a string and an ANSI character -/// -/// \param left Source string -/// \param right Character to concatenate -/// -/// \return Concatenated string -/// -//////////////////////////////////////////////////////////// -SFML_API String operator +(const String& left, char right); - -//////////////////////////////////////////////////////////// -/// \brief Overload of binary + operator to concatenate a string and a wide character -/// -/// \param left Source string -/// \param right Character to concatenate -/// -/// \return Concatenated string -/// -//////////////////////////////////////////////////////////// -SFML_API String operator +(const String& left, wchar_t right); - -//////////////////////////////////////////////////////////// -/// \brief Overload of binary + operator to concatenate a string and a UTF-32 character -/// -/// \param left Source string -/// \param right Character to concatenate -/// -/// \return Concatenated string -/// -//////////////////////////////////////////////////////////// -SFML_API String operator +(const String& left, Uint32 right); - //////////////////////////////////////////////////////////// /// \brief Overload of binary + operator to concatenate two strings /// diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index 162810492..55719d52a 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -40,6 +40,7 @@ namespace sf //////////////////////////////////////////////////////////// const Image Shader::CurrentTexture; + //////////////////////////////////////////////////////////// Shader::Shader() : myShaderProgram (0), diff --git a/src/SFML/System/String.cpp b/src/SFML/System/String.cpp index 6c478dc76..d1c913473 100644 --- a/src/SFML/System/String.cpp +++ b/src/SFML/System/String.cpp @@ -33,12 +33,46 @@ namespace sf { +//////////////////////////////////////////////////////////// +// Static member data +//////////////////////////////////////////////////////////// +const std::size_t String::InvalidPos = std::basic_string::npos; + + //////////////////////////////////////////////////////////// String::String() { } +//////////////////////////////////////////////////////////// +String::String(char ansiChar) +{ + myString += Utf32::DecodeAnsi(ansiChar); +} + + +//////////////////////////////////////////////////////////// +String::String(char ansiChar, const std::locale& locale) +{ + myString += Utf32::DecodeAnsi(ansiChar, locale); +} + + +//////////////////////////////////////////////////////////// +String::String(wchar_t wideChar) +{ + myString += Utf32::DecodeWide(wideChar); +} + + +//////////////////////////////////////////////////////////// +String::String(Uint32 utf32Char) +{ + myString += utf32Char; +} + + //////////////////////////////////////////////////////////// String::String(const char* ansiString) { @@ -194,30 +228,6 @@ String& String::operator =(const String& right) } -//////////////////////////////////////////////////////////// -String& String::operator +=(char right) -{ - myString += Utf32::DecodeAnsi(right); - return *this; -} - - -//////////////////////////////////////////////////////////// -String& String::operator +=(wchar_t right) -{ - myString += Utf32::DecodeWide(right); - return *this; -} - - -//////////////////////////////////////////////////////////// -String& String::operator +=(Uint32 right) -{ - myString += right; - return *this; -} - - //////////////////////////////////////////////////////////// String& String::operator +=(const String& right) { @@ -268,6 +278,20 @@ void String::Erase(std::size_t position, std::size_t count) } +//////////////////////////////////////////////////////////// +void String::Insert(std::size_t position, const String& str) +{ + myString.insert(position, str.myString); +} + + +//////////////////////////////////////////////////////////// +std::size_t String::Find(const String& str, std::size_t start) const +{ + return myString.find(str.myString, start); +} + + //////////////////////////////////////////////////////////// const Uint32* String::GetData() const { @@ -345,36 +369,6 @@ bool operator >=(const String& left, const String& right) } -//////////////////////////////////////////////////////////// -String operator +(const String& left, char right) -{ - String string = left; - string += right; - - return string; -} - - -//////////////////////////////////////////////////////////// -String operator +(const String& left, wchar_t right) -{ - String string = left; - string += right; - - return string; -} - - -//////////////////////////////////////////////////////////// -String operator +(const String& left, Uint32 right) -{ - String string = left; - string += right; - - return string; -} - - //////////////////////////////////////////////////////////// String operator +(const String& left, const String& right) {