mirror of
https://github.com/minetest/irrlicht.git
synced 2025-07-01 15:50:27 +02:00
Reformat the code, using:
find -type f | # list all regular files grep -E '\.(h|cpp|mm)$' | # filter for source files grep -v '/mt_' | # filter out generated files grep -v '/vendor/' | # and vendored GL grep -v '/test/image_loader_test.cpp' | # and this file (has giant literals arrays) xargs -n 1 -P $(nproc) clang-format -i # reformat everything Co-authored-by: numzero <numzer0@yandex.ru>
This commit is contained in:
@ -43,48 +43,44 @@ typedef string<c8> stringc;
|
||||
//! Typedef for wide character strings
|
||||
typedef string<wchar_t> stringw;
|
||||
|
||||
|
||||
//! Returns a character converted to lower case
|
||||
static inline u32 locale_lower ( u32 x )
|
||||
static inline u32 locale_lower(u32 x)
|
||||
{
|
||||
// ansi
|
||||
return x >= 'A' && x <= 'Z' ? x + 0x20 : x;
|
||||
}
|
||||
|
||||
//! Returns a character converted to upper case
|
||||
static inline u32 locale_upper ( u32 x )
|
||||
static inline u32 locale_upper(u32 x)
|
||||
{
|
||||
// ansi
|
||||
return x >= 'a' && x <= 'z' ? x + ( 'A' - 'a' ) : x;
|
||||
return x >= 'a' && x <= 'z' ? x + ('A' - 'a') : x;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
class string
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T char_type;
|
||||
|
||||
//! Default constructor
|
||||
string()
|
||||
{}
|
||||
|
||||
{
|
||||
}
|
||||
|
||||
//! Copy constructor
|
||||
string(const string<T>& other)
|
||||
string(const string<T> &other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
//! Constructor from other string types
|
||||
template <class B>
|
||||
string(const string<B>& other)
|
||||
string(const string<B> &other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
|
||||
//! Constructs a string from a float
|
||||
explicit string(const double number)
|
||||
{
|
||||
@ -93,63 +89,56 @@ public:
|
||||
str = tmpbuf;
|
||||
}
|
||||
|
||||
|
||||
//! Constructs a string from an int
|
||||
explicit string(int number)
|
||||
{
|
||||
str = std::to_string(number);
|
||||
}
|
||||
|
||||
|
||||
//! Constructs a string from an unsigned int
|
||||
explicit string(unsigned int number)
|
||||
{
|
||||
str = std::to_string(number);
|
||||
}
|
||||
|
||||
|
||||
//! Constructs a string from a long
|
||||
explicit string(long number)
|
||||
{
|
||||
str = std::to_string(number);
|
||||
}
|
||||
|
||||
|
||||
//! Constructs a string from an unsigned long
|
||||
explicit string(unsigned long number)
|
||||
{
|
||||
str = std::to_string(number);
|
||||
}
|
||||
|
||||
|
||||
//! Constructor for copying a string from a pointer with a given length
|
||||
template <class B>
|
||||
string(const B* const c, u32 length)
|
||||
string(const B *const c, u32 length)
|
||||
{
|
||||
if (!c)
|
||||
return;
|
||||
|
||||
str.resize(length);
|
||||
for (u32 l = 0; l<length; ++l)
|
||||
for (u32 l = 0; l < length; ++l)
|
||||
str[l] = (T)c[l];
|
||||
}
|
||||
|
||||
|
||||
//! Constructor for Unicode and ASCII strings
|
||||
template <class B>
|
||||
string(const B* const c)
|
||||
string(const B *const c)
|
||||
{
|
||||
*this = c;
|
||||
}
|
||||
|
||||
|
||||
//! Destructor
|
||||
~string()
|
||||
{}
|
||||
|
||||
{
|
||||
}
|
||||
|
||||
//! Assignment operator
|
||||
string<T>& operator=(const string<T>& other)
|
||||
string<T> &operator=(const string<T> &other)
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
@ -160,37 +149,34 @@ public:
|
||||
|
||||
//! Assignment operator for other string types
|
||||
template <class B>
|
||||
string<T>& operator=(const string<B>& other)
|
||||
string<T> &operator=(const string<B> &other)
|
||||
{
|
||||
*this = other.c_str();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Assignment operator for strings, ASCII and Unicode
|
||||
template <class B>
|
||||
string<T>& operator=(const B* const c)
|
||||
string<T> &operator=(const B *const c)
|
||||
{
|
||||
if (!c)
|
||||
{
|
||||
if (!c) {
|
||||
clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
// no longer allowed!
|
||||
_IRR_DEBUG_BREAK_IF((void*)c == (void*)c_str());
|
||||
_IRR_DEBUG_BREAK_IF((void *)c == (void *)c_str());
|
||||
|
||||
u32 len = calclen(c);
|
||||
str.resize(len);
|
||||
for (u32 l = 0; l<len; ++l)
|
||||
for (u32 l = 0; l < len; ++l)
|
||||
str[l] = (T)c[l];
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Append operator for other strings
|
||||
string<T> operator+(const string<T>& other) const
|
||||
string<T> operator+(const string<T> &other) const
|
||||
{
|
||||
string<T> tmp(*this);
|
||||
tmp.append(other);
|
||||
@ -198,10 +184,9 @@ public:
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
//! Append operator for strings, ASCII and Unicode
|
||||
template <class B>
|
||||
string<T> operator+(const B* const c) const
|
||||
string<T> operator+(const B *const c) const
|
||||
{
|
||||
string<T> tmp(*this);
|
||||
tmp.append(c);
|
||||
@ -209,58 +194,50 @@ public:
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
//! Direct access operator
|
||||
T& operator [](const u32 index)
|
||||
T &operator[](const u32 index)
|
||||
{
|
||||
return str[index];
|
||||
}
|
||||
|
||||
|
||||
//! Direct access operator
|
||||
const T& operator [](const u32 index) const
|
||||
const T &operator[](const u32 index) const
|
||||
{
|
||||
return str[index];
|
||||
}
|
||||
|
||||
|
||||
//! Equality operator
|
||||
bool operator==(const T* const other) const
|
||||
bool operator==(const T *const other) const
|
||||
{
|
||||
if (!other)
|
||||
return false;
|
||||
return !cmp(c_str(), other);
|
||||
}
|
||||
|
||||
|
||||
//! Equality operator
|
||||
bool operator==(const string<T>& other) const
|
||||
bool operator==(const string<T> &other) const
|
||||
{
|
||||
return str == other.str;
|
||||
}
|
||||
|
||||
|
||||
//! Is smaller comparator
|
||||
bool operator<(const string<T>& other) const
|
||||
bool operator<(const string<T> &other) const
|
||||
{
|
||||
return str < other.str;
|
||||
}
|
||||
|
||||
|
||||
//! Inequality operator
|
||||
bool operator!=(const T* const other) const
|
||||
bool operator!=(const T *const other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
//! Inequality operator
|
||||
bool operator!=(const string<T>& other) const
|
||||
bool operator!=(const string<T> &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
//! Returns length of the string's content
|
||||
/** \return Length of the string's content in characters, excluding
|
||||
the trailing NUL. */
|
||||
@ -276,7 +253,7 @@ public:
|
||||
return str.empty();
|
||||
}
|
||||
|
||||
void clear(bool releaseMemory=true)
|
||||
void clear(bool releaseMemory = true)
|
||||
{
|
||||
if (releaseMemory) {
|
||||
stl_type empty;
|
||||
@ -288,39 +265,36 @@ public:
|
||||
|
||||
//! Returns character string
|
||||
/** \return pointer to C-style NUL terminated string. */
|
||||
const T* c_str() const
|
||||
const T *c_str() const
|
||||
{
|
||||
return str.c_str();
|
||||
}
|
||||
|
||||
|
||||
//! Makes the string lower case.
|
||||
string<T>& make_lower()
|
||||
string<T> &make_lower()
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](const T& c) {
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](const T &c) {
|
||||
return locale_lower(c);
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Makes the string upper case.
|
||||
string<T>& make_upper()
|
||||
string<T> &make_upper()
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](const T& c) {
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](const T &c) {
|
||||
return locale_upper(c);
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Compares the strings ignoring case.
|
||||
/** \param other: Other string to compare.
|
||||
\return True if the strings are equal ignoring case. */
|
||||
bool equals_ignore_case(const string<T>& other) const
|
||||
bool equals_ignore_case(const string<T> &other) const
|
||||
{
|
||||
const T* array = c_str();
|
||||
for(u32 i=0; array[i] && other[i]; ++i)
|
||||
const T *array = c_str();
|
||||
for (u32 i = 0; array[i] && other[i]; ++i)
|
||||
if (locale_lower(array[i]) != locale_lower(other[i]))
|
||||
return false;
|
||||
|
||||
@ -331,47 +305,44 @@ public:
|
||||
/** \param other: Other string to compare.
|
||||
\param sourcePos: where to start to compare in the string
|
||||
\return True if the strings are equal ignoring case. */
|
||||
bool equals_substring_ignore_case(const string<T>&other, const u32 sourcePos = 0 ) const
|
||||
bool equals_substring_ignore_case(const string<T> &other, const u32 sourcePos = 0) const
|
||||
{
|
||||
if ( sourcePos >= size() + 1 )
|
||||
if (sourcePos >= size() + 1)
|
||||
return false;
|
||||
|
||||
const T* array = c_str();
|
||||
const T *array = c_str();
|
||||
u32 i;
|
||||
for(i=0; array[sourcePos + i] && other[i]; ++i)
|
||||
for (i = 0; array[sourcePos + i] && other[i]; ++i)
|
||||
if (locale_lower(array[sourcePos + i]) != locale_lower(other[i]))
|
||||
return false;
|
||||
|
||||
return array[sourcePos + i] == 0 && other[i] == 0;
|
||||
}
|
||||
|
||||
|
||||
//! Compares the strings ignoring case.
|
||||
/** \param other: Other string to compare.
|
||||
\return True if this string is smaller ignoring case. */
|
||||
bool lower_ignore_case(const string<T>& other) const
|
||||
bool lower_ignore_case(const string<T> &other) const
|
||||
{
|
||||
const T* array = c_str();
|
||||
for(u32 i=0; array[i] && other[i]; ++i)
|
||||
{
|
||||
s32 diff = (s32) locale_lower ( array[i] ) - (s32) locale_lower ( other[i] );
|
||||
if ( diff )
|
||||
const T *array = c_str();
|
||||
for (u32 i = 0; array[i] && other[i]; ++i) {
|
||||
s32 diff = (s32)locale_lower(array[i]) - (s32)locale_lower(other[i]);
|
||||
if (diff)
|
||||
return diff < 0;
|
||||
}
|
||||
|
||||
return size() < other.size();
|
||||
}
|
||||
|
||||
|
||||
//! compares the first n characters of the strings
|
||||
/** \param other Other string to compare.
|
||||
\param n Number of characters to compare
|
||||
\return True if the n first characters of both strings are equal. */
|
||||
bool equalsn(const string<T>& other, u32 n) const
|
||||
bool equalsn(const string<T> &other, u32 n) const
|
||||
{
|
||||
const T* array = c_str();
|
||||
const T *array = c_str();
|
||||
u32 i;
|
||||
for(i=0; i < n && array[i] && other[i]; ++i)
|
||||
for (i = 0; i < n && array[i] && other[i]; ++i)
|
||||
if (array[i] != other[i])
|
||||
return false;
|
||||
|
||||
@ -380,18 +351,17 @@ public:
|
||||
return (i == n) || (size() == other.size());
|
||||
}
|
||||
|
||||
|
||||
//! compares the first n characters of the strings
|
||||
/** \param str Other string to compare.
|
||||
\param n Number of characters to compare
|
||||
\return True if the n first characters of both strings are equal. */
|
||||
bool equalsn(const T* const other, u32 n) const
|
||||
bool equalsn(const T *const other, u32 n) const
|
||||
{
|
||||
if (!other)
|
||||
return false;
|
||||
const T* array = c_str();
|
||||
const T *array = c_str();
|
||||
u32 i;
|
||||
for(i=0; i < n && array[i] && other[i]; ++i)
|
||||
for (i = 0; i < n && array[i] && other[i]; ++i)
|
||||
if (array[i] != other[i])
|
||||
return false;
|
||||
|
||||
@ -400,20 +370,18 @@ public:
|
||||
return (i == n) || (array[i] == 0 && other[i] == 0);
|
||||
}
|
||||
|
||||
|
||||
//! Appends a character to this string
|
||||
/** \param character: Character to append. */
|
||||
string<T>& append(T character)
|
||||
string<T> &append(T character)
|
||||
{
|
||||
str.append(1, character);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a char string to this string
|
||||
/** \param other: Char string to append. */
|
||||
/** \param length: The length of the string to append. */
|
||||
string<T>& append(const T* const other, u32 length=0xffffffff)
|
||||
string<T> &append(const T *const other, u32 length = 0xffffffff)
|
||||
{
|
||||
if (!other)
|
||||
return *this;
|
||||
@ -426,20 +394,18 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string to this string
|
||||
/** \param other: String to append. */
|
||||
string<T>& append(const string<T>& other)
|
||||
string<T> &append(const string<T> &other)
|
||||
{
|
||||
str.append(other.str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string of the length l to this string.
|
||||
/** \param other: other String to append to this string.
|
||||
\param length: How much characters of the other string to add to this one. */
|
||||
string<T>& append(const string<T>& other, u32 length)
|
||||
string<T> &append(const string<T> &other, u32 length)
|
||||
{
|
||||
if (other.size() < length)
|
||||
append(other);
|
||||
@ -452,10 +418,9 @@ public:
|
||||
//\param pos Insert the characters before this index
|
||||
//\param s String to insert. Must be at least of size n
|
||||
//\param n Number of characters from string s to use.
|
||||
string<T>& insert(u32 pos, const T* s, u32 n)
|
||||
string<T> &insert(u32 pos, const T *s, u32 n)
|
||||
{
|
||||
if ( pos < size()+1 )
|
||||
{
|
||||
if (pos < size() + 1) {
|
||||
str.insert(pos, s, n);
|
||||
}
|
||||
|
||||
@ -472,7 +437,6 @@ public:
|
||||
str.reserve(count - 1);
|
||||
}
|
||||
|
||||
|
||||
//! finds first occurrence of character in string
|
||||
/** \param c: Character to search for.
|
||||
\return Position where the character has been found,
|
||||
@ -490,7 +454,7 @@ public:
|
||||
this should be strlen(c)
|
||||
\return Position where one of the characters has been found,
|
||||
or -1 if not found. */
|
||||
s32 findFirstChar(const T* const c, u32 count=1) const
|
||||
s32 findFirstChar(const T *const c, u32 count = 1) const
|
||||
{
|
||||
if (!c || !count)
|
||||
return -1;
|
||||
@ -499,7 +463,6 @@ public:
|
||||
return pos_from_stl(r);
|
||||
}
|
||||
|
||||
|
||||
//! Finds first position of a character not in a given list.
|
||||
/** \param c: List of characters not to find. For example if the method
|
||||
should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
|
||||
@ -507,7 +470,7 @@ public:
|
||||
this should be strlen(c)
|
||||
\return Position where the character has been found,
|
||||
or -1 if not found. */
|
||||
s32 findFirstCharNotInList(const T* const c, u32 count=1) const
|
||||
s32 findFirstCharNotInList(const T *const c, u32 count = 1) const
|
||||
{
|
||||
if (!c || !count)
|
||||
return -1;
|
||||
@ -523,7 +486,7 @@ public:
|
||||
this should be strlen(c)
|
||||
\return Position where the character has been found,
|
||||
or -1 if not found. */
|
||||
s32 findLastCharNotInList(const T* const c, u32 count=1) const
|
||||
s32 findLastCharNotInList(const T *const c, u32 count = 1) const
|
||||
{
|
||||
if (!c || !count)
|
||||
return -1;
|
||||
@ -543,7 +506,6 @@ public:
|
||||
return pos_from_stl(r);
|
||||
}
|
||||
|
||||
|
||||
//! finds last occurrence of character in string
|
||||
/** \param c: Character to search for.
|
||||
\param start: start to search reverse ( default = -1, on end )
|
||||
@ -562,7 +524,7 @@ public:
|
||||
this should be strlen(c)
|
||||
\return Position where one of the characters has been found,
|
||||
or -1 if not found. */
|
||||
s32 findLastChar(const T* const c, u32 count=1) const
|
||||
s32 findLastChar(const T *const c, u32 count = 1) const
|
||||
{
|
||||
if (!c || !count)
|
||||
return -1;
|
||||
@ -571,16 +533,14 @@ public:
|
||||
return pos_from_stl(r);
|
||||
}
|
||||
|
||||
|
||||
//! finds another string in this string
|
||||
/** \param str: Another string
|
||||
\param start: Start position of the search
|
||||
\return Positions where the string has been found,
|
||||
or -1 if not found. */
|
||||
s32 find(const T* const other, const u32 start = 0) const
|
||||
s32 find(const T *const other, const u32 start = 0) const
|
||||
{
|
||||
if (other && *other)
|
||||
{
|
||||
if (other && *other) {
|
||||
auto r = str.find(other, start);
|
||||
return pos_from_stl(r);
|
||||
}
|
||||
@ -588,16 +548,15 @@ public:
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
//! Returns a substring
|
||||
/** \param begin Start of substring.
|
||||
\param length Length of substring.
|
||||
\param make_lower copy only lower case */
|
||||
string<T> subString(u32 begin, s32 length, bool make_lower = false ) const
|
||||
string<T> subString(u32 begin, s32 length, bool make_lower = false) const
|
||||
{
|
||||
// if start after string
|
||||
// or no proper substring length
|
||||
if ((length <= 0) || (begin>=size()))
|
||||
if ((length <= 0) || (begin >= size()))
|
||||
return string<T>("");
|
||||
|
||||
string<T> o = str.substr(begin, length);
|
||||
@ -606,142 +565,126 @@ public:
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a character to this string
|
||||
/** \param c Character to append. */
|
||||
string<T>& operator += (T c)
|
||||
string<T> &operator+=(T c)
|
||||
{
|
||||
append(c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a char string to this string
|
||||
/** \param c Char string to append. */
|
||||
string<T>& operator += (const T* const c)
|
||||
string<T> &operator+=(const T *const c)
|
||||
{
|
||||
append(c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string to this string
|
||||
/** \param other String to append. */
|
||||
string<T>& operator += (const string<T>& other)
|
||||
string<T> &operator+=(const string<T> &other)
|
||||
{
|
||||
append(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string representation of a number to this string
|
||||
/** \param i Number to append. */
|
||||
string<T>& operator += (const int i)
|
||||
string<T> &operator+=(const int i)
|
||||
{
|
||||
append(string<T>(i));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string representation of a number to this string
|
||||
/** \param i Number to append. */
|
||||
string<T>& operator += (const unsigned int i)
|
||||
string<T> &operator+=(const unsigned int i)
|
||||
{
|
||||
append(string<T>(i));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string representation of a number to this string
|
||||
/** \param i Number to append. */
|
||||
string<T>& operator += (const long i)
|
||||
string<T> &operator+=(const long i)
|
||||
{
|
||||
append(string<T>(i));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string representation of a number to this string
|
||||
/** \param i Number to append. */
|
||||
string<T>& operator += (const unsigned long i)
|
||||
string<T> &operator+=(const unsigned long i)
|
||||
{
|
||||
append(string<T>(i));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string representation of a number to this string
|
||||
/** \param i Number to append. */
|
||||
string<T>& operator += (const double i)
|
||||
string<T> &operator+=(const double i)
|
||||
{
|
||||
append(string<T>(i));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Appends a string representation of a number to this string
|
||||
/** \param i Number to append. */
|
||||
string<T>& operator += (const float i)
|
||||
string<T> &operator+=(const float i)
|
||||
{
|
||||
append(string<T>(i));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Replaces all characters of a special type with another one
|
||||
/** \param toReplace Character to replace.
|
||||
\param replaceWith Character replacing the old one. */
|
||||
string<T>& replace(T toReplace, T replaceWith)
|
||||
string<T> &replace(T toReplace, T replaceWith)
|
||||
{
|
||||
std::replace(str.begin(), str.end(), toReplace, replaceWith);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Replaces all instances of a string with another one.
|
||||
/** \param toReplace The string to replace.
|
||||
\param replaceWith The string replacing the old one. */
|
||||
string<T>& replace(const string<T>& toReplace, const string<T>& replaceWith)
|
||||
string<T> &replace(const string<T> &toReplace, const string<T> &replaceWith)
|
||||
{
|
||||
size_type pos = 0;
|
||||
while ((pos = str.find(toReplace.str, pos)) != npos) {
|
||||
str.replace(pos, toReplace.size(), replaceWith.str);
|
||||
pos += replaceWith.size();
|
||||
}
|
||||
while ((pos = str.find(toReplace.str, pos)) != npos) {
|
||||
str.replace(pos, toReplace.size(), replaceWith.str);
|
||||
pos += replaceWith.size();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Removes a character from a string.
|
||||
/** \param c: Character to remove. */
|
||||
string<T>& remove(T c)
|
||||
string<T> &remove(T c)
|
||||
{
|
||||
str.erase(std::remove(str.begin(), str.end(), c), str.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Removes a string from the string.
|
||||
/** \param toRemove: String to remove. */
|
||||
string<T>& remove(const string<T>& toRemove)
|
||||
string<T> &remove(const string<T> &toRemove)
|
||||
{
|
||||
u32 size = toRemove.size();
|
||||
if ( size == 0 )
|
||||
if (size == 0)
|
||||
return *this;
|
||||
u32 pos = 0;
|
||||
u32 found = 0;
|
||||
for (u32 i=0; i<str.size(); ++i)
|
||||
{
|
||||
for (u32 i = 0; i < str.size(); ++i) {
|
||||
u32 j = 0;
|
||||
while (j < size)
|
||||
{
|
||||
while (j < size) {
|
||||
if (str[i + j] != toRemove[j])
|
||||
break;
|
||||
++j;
|
||||
}
|
||||
if (j == size)
|
||||
{
|
||||
if (j == size) {
|
||||
found += size;
|
||||
i += size - 1;
|
||||
continue;
|
||||
@ -753,10 +696,9 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Removes characters from a string.
|
||||
/** \param characters: Characters to remove. */
|
||||
string<T>& removeChars(const string<T> & characters)
|
||||
string<T> &removeChars(const string<T> &characters)
|
||||
{
|
||||
if (characters.size() == 0)
|
||||
return *this;
|
||||
@ -766,34 +708,33 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! Trims the string.
|
||||
/** Removes the specified characters (by default, Latin-1 whitespace)
|
||||
from the beginning and the end of the string. */
|
||||
string<T>& trim(const string<T> & whitespace = " \t\n\r")
|
||||
string<T> &trim(const string<T> &whitespace = " \t\n\r")
|
||||
{
|
||||
// find start and end of the substring without the specified characters
|
||||
const s32 begin = findFirstCharNotInList(whitespace.c_str(), whitespace.size());
|
||||
if (begin == -1)
|
||||
return (*this="");
|
||||
return (*this = "");
|
||||
|
||||
const s32 end = findLastCharNotInList(whitespace.c_str(), whitespace.size());
|
||||
|
||||
return (*this = subString(begin, (end +1) - begin));
|
||||
return (*this = subString(begin, (end + 1) - begin));
|
||||
}
|
||||
|
||||
//! Erases a character from the string.
|
||||
/** May be slow, because all elements
|
||||
following after the erased element have to be copied.
|
||||
\param index: Index of element to be erased. */
|
||||
string<T>& erase(u32 index)
|
||||
string<T> &erase(u32 index)
|
||||
{
|
||||
str.erase(str.begin() + index);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! verify the existing string.
|
||||
string<T>& validate()
|
||||
string<T> &validate()
|
||||
{
|
||||
// truncate to existing null
|
||||
u32 len = calclen(c_str());
|
||||
@ -828,31 +769,27 @@ public:
|
||||
characters between the delimiters are returned.
|
||||
\return The number of resulting substrings
|
||||
*/
|
||||
template<class container>
|
||||
u32 split(container& ret, const T* const delimiter, u32 countDelimiters=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
|
||||
template <class container>
|
||||
u32 split(container &ret, const T *const delimiter, u32 countDelimiters = 1, bool ignoreEmptyTokens = true, bool keepSeparators = false) const
|
||||
{
|
||||
if (!delimiter)
|
||||
return 0;
|
||||
|
||||
const u32 oldSize=static_cast<u32>(ret.size());
|
||||
const u32 oldSize = static_cast<u32>(ret.size());
|
||||
|
||||
u32 tokenStartIdx = 0;
|
||||
for (u32 i=0; i<size()+1; ++i)
|
||||
{
|
||||
for (u32 j=0; j<countDelimiters; ++j)
|
||||
{
|
||||
if (str[i] == delimiter[j])
|
||||
{
|
||||
for (u32 i = 0; i < size() + 1; ++i) {
|
||||
for (u32 j = 0; j < countDelimiters; ++j) {
|
||||
if (str[i] == delimiter[j]) {
|
||||
if (i - tokenStartIdx > 0)
|
||||
ret.push_back(string<T>(&str[tokenStartIdx], i - tokenStartIdx));
|
||||
else if ( !ignoreEmptyTokens )
|
||||
else if (!ignoreEmptyTokens)
|
||||
ret.push_back(string<T>());
|
||||
if ( keepSeparators )
|
||||
{
|
||||
if (keepSeparators) {
|
||||
ret.push_back(string<T>(&str[i], 1));
|
||||
}
|
||||
|
||||
tokenStartIdx = i+1;
|
||||
tokenStartIdx = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -862,66 +799,74 @@ public:
|
||||
else if (!ignoreEmptyTokens)
|
||||
ret.push_back(string<T>());
|
||||
|
||||
return static_cast<u32>(ret.size()-oldSize);
|
||||
return static_cast<u32>(ret.size() - oldSize);
|
||||
}
|
||||
|
||||
// This function should not be used and is only kept for "CGUIFileOpenDialog::pathToStringW".
|
||||
friend size_t multibyteToWString(stringw& destination, const stringc &source);
|
||||
friend size_t multibyteToWString(stringw &destination, const stringc &source);
|
||||
|
||||
friend size_t utf8ToWString(stringw &destination, const char *source);
|
||||
friend size_t wStringToUTF8(stringc &destination, const wchar_t *source);
|
||||
|
||||
private:
|
||||
|
||||
typedef std::basic_string<T> stl_type;
|
||||
|
||||
//! Private constructor
|
||||
string(stl_type &&str) : str(str)
|
||||
{}
|
||||
string(stl_type &&str) :
|
||||
str(str)
|
||||
{
|
||||
}
|
||||
|
||||
//! strlen wrapper
|
||||
template <typename U>
|
||||
static inline u32 calclen(const U* p) {
|
||||
static inline u32 calclen(const U *p)
|
||||
{
|
||||
u32 len = 0;
|
||||
while (*p++)
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
static inline u32 calclen(const char* p) {
|
||||
static inline u32 calclen(const char *p)
|
||||
{
|
||||
return static_cast<u32>(strlen(p));
|
||||
}
|
||||
static inline u32 calclen(const wchar_t* p) {
|
||||
static inline u32 calclen(const wchar_t *p)
|
||||
{
|
||||
return static_cast<u32>(wcslen(p));
|
||||
}
|
||||
|
||||
//! strcmp wrapper
|
||||
template <typename U>
|
||||
static inline int cmp(const U* p, const U* p2) {
|
||||
static inline int cmp(const U *p, const U *p2)
|
||||
{
|
||||
while (*p && *p == *p2)
|
||||
p++, p2++;
|
||||
return (int)*p - (int)*p2;
|
||||
}
|
||||
static inline int cmp(const char* p, const char* p2) {
|
||||
static inline int cmp(const char *p, const char *p2)
|
||||
{
|
||||
return strcmp(p, p2);
|
||||
}
|
||||
static inline int cmp(const wchar_t* p, const wchar_t* p2) {
|
||||
static inline int cmp(const wchar_t *p, const wchar_t *p2)
|
||||
{
|
||||
return wcscmp(p, p2);
|
||||
}
|
||||
|
||||
typedef typename stl_type::size_type size_type;
|
||||
static const size_type npos = stl_type::npos;
|
||||
|
||||
static inline s32 pos_from_stl(size_type pos) {
|
||||
static inline s32 pos_from_stl(size_type pos)
|
||||
{
|
||||
return pos == npos ? -1 : (s32)pos;
|
||||
}
|
||||
static inline size_type pos_to_stl(s32 pos) {
|
||||
static inline size_type pos_to_stl(s32 pos)
|
||||
{
|
||||
return pos == -1 ? npos : (size_type)pos;
|
||||
}
|
||||
|
||||
stl_type str;
|
||||
};
|
||||
|
||||
|
||||
//! Convert multibyte string to wide-character string
|
||||
/** Wrapper around mbstowcs from standard library, but directly using Irrlicht string class.
|
||||
What the function does exactly depends on the LC_CTYPE of the current c locale.
|
||||
@ -930,41 +875,34 @@ What the function does exactly depends on the LC_CTYPE of the current c locale.
|
||||
\return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed
|
||||
|
||||
This function should not be used and is only kept for "CGUIFileOpenDialog::pathToStringW". */
|
||||
inline size_t multibyteToWString(stringw& destination, const core::stringc& source)
|
||||
inline size_t multibyteToWString(stringw &destination, const core::stringc &source)
|
||||
{
|
||||
u32 sourceSize = source.size();
|
||||
|
||||
if ( sourceSize )
|
||||
{
|
||||
destination.str.resize(sourceSize+1);
|
||||
if (sourceSize) {
|
||||
destination.str.resize(sourceSize + 1);
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4996) // 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead.
|
||||
#pragma warning(disable : 4996) // 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead.
|
||||
#endif
|
||||
const size_t written = mbstowcs(&destination[0], source.c_str(), (size_t)sourceSize);
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
if ( written != (size_t)-1 )
|
||||
{
|
||||
if (written != (size_t)-1) {
|
||||
destination.str.resize(written);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Likely character which got converted until the invalid character was encountered are in destination now.
|
||||
// And it seems even 0-terminated, but I found no documentation anywhere that this (the 0-termination) is guaranteed :-(
|
||||
destination.clear();
|
||||
}
|
||||
return written;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
destination.clear();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline size_t utf8ToWString(stringw &destination, const char *source)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
|
||||
@ -989,8 +927,5 @@ inline size_t wStringToUTF8(stringc &destination, const stringw &source)
|
||||
return wStringToUTF8(destination, source.c_str());
|
||||
}
|
||||
|
||||
|
||||
} // end namespace core
|
||||
} // end namespace irr
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user