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
This commit is contained in:
LaurentGom 2010-02-26 09:36:38 +00:00
parent 6f6481ef38
commit bb414773d1
3 changed files with 131 additions and 117 deletions

View File

@ -50,6 +50,11 @@ public :
typedef std::basic_string<Uint32>::iterator Iterator; ///< Iterator type typedef std::basic_string<Uint32>::iterator Iterator; ///< Iterator type
typedef std::basic_string<Uint32>::const_iterator ConstIterator; ///< Constant iterator type typedef std::basic_string<Uint32>::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 /// \brief Default constructor
/// ///
@ -58,6 +63,47 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
String(); 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 /// \brief Construct from a null-terminated C-style ANSI string
/// ///
@ -160,6 +206,8 @@ public :
/// ///
/// \return Converted ANSI string /// \return Converted ANSI string
/// ///
/// \see ToAnsiString, operator std::wstring
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
operator std::string() const; operator std::string() const;
@ -173,6 +221,8 @@ public :
/// ///
/// \return Converted wide string /// \return Converted wide string
/// ///
/// \see ToWideString, operator std::string
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
operator std::wstring() const; operator std::wstring() const;
@ -187,6 +237,8 @@ public :
/// ///
/// \return Converted ANSI string /// \return Converted ANSI string
/// ///
/// \see ToWideString, operator std::string
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::string ToAnsiString() const; std::string ToAnsiString() const;
@ -204,6 +256,8 @@ public :
/// ///
/// \return Converted ANSI string /// \return Converted ANSI string
/// ///
/// \see ToWideString, operator std::string
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::string ToAnsiString(const std::locale& locale) const; std::string ToAnsiString(const std::locale& locale) const;
@ -215,6 +269,8 @@ public :
/// ///
/// \return Converted wide string /// \return Converted wide string
/// ///
/// \see ToAnsiString, operator std::wstring
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::wstring ToWideString() const; std::wstring ToWideString() const;
@ -228,36 +284,6 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
String& operator =(const String& right); 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 /// \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); 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 /// \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); 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 /// \brief Overload of binary + operator to concatenate two strings
/// ///

View File

@ -40,6 +40,7 @@ namespace sf
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const Image Shader::CurrentTexture; const Image Shader::CurrentTexture;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Shader::Shader() : Shader::Shader() :
myShaderProgram (0), myShaderProgram (0),

View File

@ -33,12 +33,46 @@
namespace sf namespace sf
{ {
////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
const std::size_t String::InvalidPos = std::basic_string<Uint32>::npos;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
String::String() 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) 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) 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 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) String operator +(const String& left, const String& right)
{ {