2020-01-03 20:05:16 +01:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
|
|
|
|
|
|
|
|
#ifndef __IRR_STRING_H_INCLUDED__
|
|
|
|
#define __IRR_STRING_H_INCLUDED__
|
|
|
|
|
|
|
|
#include "irrTypes.h"
|
2022-12-22 23:55:35 +01:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cwchar>
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace core
|
|
|
|
{
|
|
|
|
|
|
|
|
//! Very simple string class with some useful features.
|
|
|
|
/** string<c8> and string<wchar_t> both accept Unicode AND ASCII/Latin-1,
|
|
|
|
so you can assign Unicode to string<c8> and ASCII/Latin-1 to string<wchar_t>
|
|
|
|
(and the other way round) if you want to.
|
|
|
|
|
|
|
|
However, note that the conversation between both is not done using any encoding.
|
|
|
|
This means that c8 strings are treated as ASCII/Latin-1, not UTF-8, and
|
|
|
|
are simply expanded to the equivalent wchar_t, while Unicode/wchar_t
|
|
|
|
characters are truncated to 8-bit ASCII/Latin-1 characters, discarding all
|
|
|
|
other information in the wchar_t.
|
|
|
|
|
|
|
|
Helper functions for converting between UTF-8 and wchar_t are provided
|
|
|
|
outside the string class for explicit use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// forward declarations
|
2022-12-22 23:55:35 +01:00
|
|
|
template <typename T>
|
2020-01-03 20:05:16 +01:00
|
|
|
class string;
|
|
|
|
static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
|
2021-08-30 21:44:56 +02:00
|
|
|
static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
//! Returns a character converted to lower case
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
// ansi
|
|
|
|
return x >= 'a' && x <= 'z' ? x + ( 'A' - 'a' ) : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
template <typename T>
|
2020-01-03 20:05:16 +01:00
|
|
|
class string
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef T char_type;
|
|
|
|
|
|
|
|
//! Default constructor
|
|
|
|
string()
|
2022-12-22 23:55:35 +01:00
|
|
|
{}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
//! Copy constructor
|
|
|
|
string(const string<T>& other)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Constructor from other string types
|
2022-12-22 23:55:35 +01:00
|
|
|
template <class B>
|
|
|
|
string(const string<B>& other)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Constructs a string from a float
|
|
|
|
explicit string(const double number)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
c8 tmpbuf[32];
|
|
|
|
snprintf_irr(tmpbuf, sizeof(tmpbuf), "%0.6f", number);
|
|
|
|
str = tmpbuf;
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Constructs a string from an int
|
|
|
|
explicit string(int number)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str = std::to_string(number);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Constructs a string from an unsigned int
|
|
|
|
explicit string(unsigned int number)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str = std::to_string(number);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Constructs a string from a long
|
|
|
|
explicit string(long number)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str = std::to_string(number);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Constructs a string from an unsigned long
|
|
|
|
explicit string(unsigned long number)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str = std::to_string(number);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Constructor for copying a string from a pointer with a given length
|
|
|
|
template <class B>
|
|
|
|
string(const B* const c, u32 length)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
str.resize(length);
|
2020-01-03 20:05:16 +01:00
|
|
|
for (u32 l = 0; l<length; ++l)
|
2022-12-22 23:55:35 +01:00
|
|
|
str[l] = (T)c[l];
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Constructor for Unicode and ASCII strings
|
|
|
|
template <class B>
|
|
|
|
string(const B* const c)
|
|
|
|
{
|
|
|
|
*this = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Destructor
|
|
|
|
~string()
|
2022-12-22 23:55:35 +01:00
|
|
|
{}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
//! Assignment operator
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator=(const string<T>& other)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
str = other.str;
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Assignment operator for other string types
|
2022-12-22 23:55:35 +01:00
|
|
|
template <class B>
|
|
|
|
string<T>& operator=(const string<B>& other)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
*this = other.c_str();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Assignment operator for strings, ASCII and Unicode
|
|
|
|
template <class B>
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator=(const B* const c)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
clear();
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
// no longer allowed!
|
|
|
|
_IRR_DEBUG_BREAK_IF((void*)c == (void*)c_str());
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
u32 len = calclen(c);
|
|
|
|
str.resize(len);
|
2020-01-03 20:05:16 +01:00
|
|
|
for (u32 l = 0; l<len; ++l)
|
2022-12-22 23:55:35 +01:00
|
|
|
str[l] = (T)c[l];
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Append operator for other strings
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T> operator+(const string<T>& other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T> tmp(*this);
|
|
|
|
tmp.append(other);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
return tmp;
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Append operator for strings, ASCII and Unicode
|
|
|
|
template <class B>
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T> operator+(const B* const c) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T> tmp(*this);
|
|
|
|
tmp.append(c);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
return tmp;
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Direct access operator
|
|
|
|
T& operator [](const u32 index)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return str[index];
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Direct access operator
|
|
|
|
const T& operator [](const u32 index) const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return str[index];
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Equality operator
|
2022-12-22 23:55:35 +01:00
|
|
|
bool operator==(const T* const other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if (!other)
|
2020-01-03 20:05:16 +01:00
|
|
|
return false;
|
2022-12-22 23:55:35 +01:00
|
|
|
return !cmp(c_str(), other);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Equality operator
|
2022-12-22 23:55:35 +01:00
|
|
|
bool operator==(const string<T>& other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return str == other.str;
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Is smaller comparator
|
2022-12-22 23:55:35 +01:00
|
|
|
bool operator<(const string<T>& other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return str < other.str;
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Inequality operator
|
2022-12-22 23:55:35 +01:00
|
|
|
bool operator!=(const T* const other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return !(*this == other);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Inequality operator
|
2022-12-22 23:55:35 +01:00
|
|
|
bool operator!=(const string<T>& other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Returns length of the string's content
|
|
|
|
/** \return Length of the string's content in characters, excluding
|
|
|
|
the trailing NUL. */
|
|
|
|
u32 size() const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return str.size();
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Informs if the string is empty or not.
|
|
|
|
//! \return True if the string is empty, false if not.
|
|
|
|
bool empty() const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return str.empty();
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear(bool releaseMemory=true)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if (releaseMemory) {
|
|
|
|
stl_type empty;
|
|
|
|
std::swap(str, empty);
|
|
|
|
} else {
|
|
|
|
str.clear();
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Returns character string
|
|
|
|
/** \return pointer to C-style NUL terminated string. */
|
|
|
|
const T* c_str() const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return str.c_str();
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Makes the string lower case.
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& make_lower()
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
std::transform(str.begin(), str.end(), str.begin(), [](const T& c) {
|
|
|
|
return locale_lower(c);
|
|
|
|
});
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Makes the string upper case.
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& make_upper()
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
std::transform(str.begin(), str.end(), str.begin(), [](const T& c) {
|
|
|
|
return locale_upper(c);
|
|
|
|
});
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Compares the strings ignoring case.
|
|
|
|
/** \param other: Other string to compare.
|
|
|
|
\return True if the strings are equal ignoring case. */
|
2022-12-22 23:55:35 +01:00
|
|
|
bool equals_ignore_case(const string<T>& other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
const T* array = c_str();
|
2020-01-03 20:05:16 +01:00
|
|
|
for(u32 i=0; array[i] && other[i]; ++i)
|
2022-12-22 23:55:35 +01:00
|
|
|
if (locale_lower(array[i]) != locale_lower(other[i]))
|
2020-01-03 20:05:16 +01:00
|
|
|
return false;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
return size() == other.size();
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Compares the strings ignoring case.
|
|
|
|
/** \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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
bool equals_substring_ignore_case(const string<T>&other, const u32 sourcePos = 0 ) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if ( sourcePos >= size() + 1 )
|
2020-01-03 20:05:16 +01:00
|
|
|
return false;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
const T* array = c_str();
|
2020-01-03 20:05:16 +01:00
|
|
|
u32 i;
|
2022-12-22 23:55:35 +01:00
|
|
|
for(i=0; array[sourcePos + i] && other[i]; ++i)
|
|
|
|
if (locale_lower(array[sourcePos + i]) != locale_lower(other[i]))
|
2020-01-03 20:05:16 +01:00
|
|
|
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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
bool lower_ignore_case(const string<T>& other) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
const T* array = c_str();
|
|
|
|
for(u32 i=0; array[i] && other[i]; ++i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
s32 diff = (s32) locale_lower ( array[i] ) - (s32) locale_lower ( other[i] );
|
2020-01-03 20:05:16 +01:00
|
|
|
if ( diff )
|
|
|
|
return diff < 0;
|
|
|
|
}
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
return size() < other.size();
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! 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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
bool equalsn(const string<T>& other, u32 n) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
const T* array = c_str();
|
2020-01-03 20:05:16 +01:00
|
|
|
u32 i;
|
|
|
|
for(i=0; i < n && array[i] && other[i]; ++i)
|
|
|
|
if (array[i] != other[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// if one (or both) of the strings was smaller then they
|
|
|
|
// are only equal if they have the same length
|
2022-12-22 23:55:35 +01:00
|
|
|
return (i == n) || (size() == other.size());
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! 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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
bool equalsn(const T* const other, u32 n) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if (!other)
|
2020-01-03 20:05:16 +01:00
|
|
|
return false;
|
2022-12-22 23:55:35 +01:00
|
|
|
const T* array = c_str();
|
2020-01-03 20:05:16 +01:00
|
|
|
u32 i;
|
2022-12-22 23:55:35 +01:00
|
|
|
for(i=0; i < n && array[i] && other[i]; ++i)
|
|
|
|
if (array[i] != other[i])
|
2020-01-03 20:05:16 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// if one (or both) of the strings was smaller then they
|
|
|
|
// are only equal if they have the same length
|
2022-12-22 23:55:35 +01:00
|
|
|
return (i == n) || (array[i] == 0 && other[i] == 0);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a character to this string
|
|
|
|
/** \param character: Character to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& append(T character)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str.append(1, character);
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a char string to this string
|
|
|
|
/** \param other: Char string to append. */
|
|
|
|
/** \param length: The length of the string to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& append(const T* const other, u32 length=0xffffffff)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (!other)
|
|
|
|
return *this;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
u32 len = calclen(other);
|
2020-01-03 20:05:16 +01:00
|
|
|
if (len > length)
|
|
|
|
len = length;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
str.append(other, len);
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string to this string
|
|
|
|
/** \param other: String to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& append(const string<T>& other)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str.append(other.str);
|
2020-01-03 20:05:16 +01:00
|
|
|
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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& append(const string<T>& other, u32 length)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (other.size() < length)
|
|
|
|
append(other);
|
2022-12-22 23:55:35 +01:00
|
|
|
else
|
|
|
|
str.append(other.c_str(), length);
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Insert a certain amount of characters into the string before the given index
|
|
|
|
//\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.
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& insert(u32 pos, const T* s, u32 n)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if ( pos < size()+1 )
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str.insert(pos, s, n);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Reserves some memory.
|
2022-12-22 23:55:35 +01:00
|
|
|
/** \param count: Amount of characters to reserve, including
|
|
|
|
the trailing NUL. */
|
2020-01-03 20:05:16 +01:00
|
|
|
void reserve(u32 count)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if (count == 0)
|
2020-01-03 20:05:16 +01:00
|
|
|
return;
|
2022-12-22 23:55:35 +01:00
|
|
|
str.reserve(count - 1);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! finds first occurrence of character in string
|
|
|
|
/** \param c: Character to search for.
|
|
|
|
\return Position where the character has been found,
|
|
|
|
or -1 if not found. */
|
|
|
|
s32 findFirst(T c) const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.find(c);
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! finds first occurrence of a character of a list in string
|
|
|
|
/** \param c: List of characters to find. For example if the method
|
|
|
|
should find the first occurrence of 'a' or 'b', this parameter should be "ab".
|
|
|
|
\param count: Amount of characters in the list. Usually,
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (!c || !count)
|
|
|
|
return -1;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.find_first_of(c, 0, count);
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! 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".
|
|
|
|
\param count: Amount of characters in the list. Usually,
|
|
|
|
this should be strlen(c)
|
|
|
|
\return Position where the character has been found,
|
|
|
|
or -1 if not found. */
|
2022-12-22 23:55:35 +01:00
|
|
|
s32 findFirstCharNotInList(const T* const c, u32 count=1) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (!c || !count)
|
|
|
|
return -1;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.find_first_not_of(c, 0, count);
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Finds last 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".
|
|
|
|
\param count: Amount of characters in the list. Usually,
|
|
|
|
this should be strlen(c)
|
|
|
|
\return Position where the character has been found,
|
|
|
|
or -1 if not found. */
|
2022-12-22 23:55:35 +01:00
|
|
|
s32 findLastCharNotInList(const T* const c, u32 count=1) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (!c || !count)
|
|
|
|
return -1;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.find_last_not_of(c, npos, count);
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! finds next occurrence of character in string
|
|
|
|
/** \param c: Character to search for.
|
|
|
|
\param startPos: Position in string to start searching.
|
|
|
|
\return Position where the character has been found,
|
|
|
|
or -1 if not found. */
|
|
|
|
s32 findNext(T c, u32 startPos) const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.find(c, startPos);
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! finds last occurrence of character in string
|
|
|
|
/** \param c: Character to search for.
|
|
|
|
\param start: start to search reverse ( default = -1, on end )
|
|
|
|
\return Position where the character has been found,
|
|
|
|
or -1 if not found. */
|
|
|
|
s32 findLast(T c, s32 start = -1) const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.rfind(c, pos_to_stl(start));
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! finds last occurrence of a character of a list in string
|
|
|
|
/** \param c: List of strings to find. For example if the method
|
|
|
|
should find the last occurrence of 'a' or 'b', this parameter should be "ab".
|
|
|
|
\param count: Amount of characters in the list. Usually,
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (!c || !count)
|
|
|
|
return -1;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.find_last_of(c, npos, count);
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! 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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
s32 find(const T* const other, const u32 start = 0) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if (other && *other)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
auto r = str.find(other, start);
|
|
|
|
return pos_from_stl(r);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
// if start after string
|
|
|
|
// or no proper substring length
|
|
|
|
if ((length <= 0) || (begin>=size()))
|
|
|
|
return string<T>("");
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T> o = str.substr(begin, length);
|
|
|
|
if (make_lower)
|
|
|
|
o.make_lower();
|
2020-01-03 20:05:16 +01:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a character to this string
|
|
|
|
/** \param c Character to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (T c)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
append(c);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a char string to this string
|
|
|
|
/** \param c Char string to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const T* const c)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
append(c);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string to this string
|
|
|
|
/** \param other String to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const string<T>& other)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
append(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string representation of a number to this string
|
|
|
|
/** \param i Number to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const int i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
append(string<T>(i));
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string representation of a number to this string
|
|
|
|
/** \param i Number to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const unsigned int i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
append(string<T>(i));
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string representation of a number to this string
|
|
|
|
/** \param i Number to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const long i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
append(string<T>(i));
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string representation of a number to this string
|
|
|
|
/** \param i Number to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const unsigned long i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
append(string<T>(i));
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string representation of a number to this string
|
|
|
|
/** \param i Number to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const double i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
append(string<T>(i));
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Appends a string representation of a number to this string
|
|
|
|
/** \param i Number to append. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& operator += (const float i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
append(string<T>(i));
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Replaces all characters of a special type with another one
|
|
|
|
/** \param toReplace Character to replace.
|
|
|
|
\param replaceWith Character replacing the old one. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& replace(T toReplace, T replaceWith)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
std::replace(str.begin(), str.end(), toReplace, replaceWith);
|
2020-01-03 20:05:16 +01:00
|
|
|
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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& replace(const string<T>& toReplace, const string<T>& replaceWith)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
size_type pos = 0;
|
|
|
|
while ((pos = str.find(toReplace.str, pos)) != npos) {
|
|
|
|
str.replace(pos, toReplace.size(), replaceWith.str);
|
|
|
|
pos += replaceWith.size();
|
|
|
|
}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
//! Removes a character from a string.
|
2020-01-03 20:05:16 +01:00
|
|
|
/** \param c: Character to remove. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& remove(T c)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str.erase(std::remove(str.begin(), str.end(), c), str.end());
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Removes a string from the string.
|
|
|
|
/** \param toRemove: String to remove. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& remove(const string<T>& toRemove)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
u32 size = toRemove.size();
|
|
|
|
if ( size == 0 )
|
|
|
|
return *this;
|
|
|
|
u32 pos = 0;
|
|
|
|
u32 found = 0;
|
2022-12-22 23:55:35 +01:00
|
|
|
for (u32 i=0; i<str.size(); ++i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
u32 j = 0;
|
|
|
|
while (j < size)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if (str[i + j] != toRemove[j])
|
2020-01-03 20:05:16 +01:00
|
|
|
break;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
if (j == size)
|
|
|
|
{
|
|
|
|
found += size;
|
|
|
|
i += size - 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
str[pos++] = str[i];
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
2022-12-22 23:55:35 +01:00
|
|
|
str.resize(str.size() - found);
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Removes characters from a string.
|
|
|
|
/** \param characters: Characters to remove. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& removeChars(const string<T> & characters)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (characters.size() == 0)
|
|
|
|
return *this;
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
for (u32 i = 0; i < characters.size(); i++)
|
|
|
|
remove(characters[i]);
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Trims the string.
|
|
|
|
/** Removes the specified characters (by default, Latin-1 whitespace)
|
|
|
|
from the beginning and the end of the string. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& trim(const string<T> & whitespace = " \t\n\r")
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
// find start and end of the substring without the specified characters
|
2022-12-22 23:55:35 +01:00
|
|
|
const s32 begin = findFirstCharNotInList(whitespace.c_str(), whitespace.size());
|
2020-01-03 20:05:16 +01:00
|
|
|
if (begin == -1)
|
|
|
|
return (*this="");
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
const s32 end = findLastCharNotInList(whitespace.c_str(), whitespace.size());
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
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. */
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& erase(u32 index)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
str.erase(str.begin() + index);
|
2020-01-03 20:05:16 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! verify the existing string.
|
2022-12-22 23:55:35 +01:00
|
|
|
string<T>& validate()
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
// truncate to existing null
|
|
|
|
u32 len = calclen(c_str());
|
|
|
|
if (len != size())
|
|
|
|
str.resize(len);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! gets the last char of a string or null
|
|
|
|
T lastChar() const
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
return !str.empty() ? str.back() : 0;
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Split string into parts (tokens).
|
|
|
|
/** This method will split a string at certain delimiter characters
|
|
|
|
into the container passed in as reference. The type of the container
|
|
|
|
has to be given as template parameter. It must provide a push_back and
|
|
|
|
a size method.
|
|
|
|
\param ret The result container. Tokens are added, the container is not cleared.
|
|
|
|
\param delimiter C-style string of delimiter characters
|
|
|
|
\param countDelimiters Number of delimiter characters
|
|
|
|
\param ignoreEmptyTokens Flag to avoid empty substrings in the result
|
|
|
|
container. If two delimiters occur without a character in between or an
|
|
|
|
empty substring would be placed in the result. Or if a delimiter is the last
|
|
|
|
character an empty substring would be added at the end. If this flag is set,
|
|
|
|
only non-empty strings are stored.
|
|
|
|
\param keepSeparators Flag which allows to add the separator to the
|
|
|
|
result string. If this flag is true, the concatenation of the
|
|
|
|
substrings results in the original string. Otherwise, only the
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (!delimiter)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const u32 oldSize=ret.size();
|
|
|
|
|
|
|
|
u32 tokenStartIdx = 0;
|
2022-12-22 23:55:35 +01:00
|
|
|
for (u32 i=0; i<size()+1; ++i)
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
for (u32 j=0; j<countDelimiters; ++j)
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
if (str[i] == delimiter[j])
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (i - tokenStartIdx > 0)
|
2022-12-22 23:55:35 +01:00
|
|
|
ret.push_back(string<T>(&str[tokenStartIdx], i - tokenStartIdx));
|
2020-01-03 20:05:16 +01:00
|
|
|
else if ( !ignoreEmptyTokens )
|
2022-12-22 23:55:35 +01:00
|
|
|
ret.push_back(string<T>());
|
2020-01-03 20:05:16 +01:00
|
|
|
if ( keepSeparators )
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
ret.push_back(string<T>(&str[i], 1));
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tokenStartIdx = i+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 23:55:35 +01:00
|
|
|
if (size() > tokenStartIdx)
|
|
|
|
ret.push_back(string<T>(&str[tokenStartIdx], size() - tokenStartIdx));
|
|
|
|
else if (!ignoreEmptyTokens)
|
|
|
|
ret.push_back(string<T>());
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
return ret.size()-oldSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
|
2021-08-30 21:44:56 +02:00
|
|
|
friend size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
typedef std::basic_string<T> stl_type;
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
//! Private constructor
|
|
|
|
string(stl_type &&str) : str(str)
|
|
|
|
{}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
//! strlen wrapper
|
|
|
|
template <typename U>
|
|
|
|
static inline u32 calclen(const U* p) {
|
|
|
|
u32 len = 0;
|
|
|
|
while (*p++)
|
|
|
|
len++;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
static inline u32 calclen(const char* p) {
|
|
|
|
return strlen(p);
|
|
|
|
}
|
|
|
|
static inline u32 calclen(const wchar_t* p) {
|
|
|
|
return wcslen(p);
|
|
|
|
}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
//! strcmp wrapper
|
|
|
|
template <typename U>
|
|
|
|
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) {
|
|
|
|
return strcmp(p, p2);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
2022-12-22 23:55:35 +01:00
|
|
|
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;
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
static inline s32 pos_from_stl(size_type pos) {
|
|
|
|
return pos == npos ? -1 : (s32)pos;
|
|
|
|
}
|
|
|
|
static inline size_type pos_to_stl(s32 pos) {
|
|
|
|
return pos == -1 ? npos : (size_type)pos;
|
|
|
|
}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-12-22 23:55:35 +01:00
|
|
|
stl_type str;
|
2020-01-03 20:05:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//! Typedef for character strings
|
|
|
|
typedef string<c8> stringc;
|
|
|
|
|
|
|
|
//! Typedef for wide character strings
|
|
|
|
typedef string<wchar_t> stringw;
|
|
|
|
|
|
|
|
//! 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.
|
|
|
|
\param destination Wide-character string receiving the converted source
|
|
|
|
\param source multibyte string
|
|
|
|
\return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed */
|
|
|
|
static inline size_t multibyteToWString(string<wchar_t>& destination, const core::string<c8>& source)
|
|
|
|
{
|
|
|
|
return multibyteToWString(destination, source.c_str(), (u32)source.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Convert multibyte string to wide-character string
|
|
|
|
/** Wrapper around mbstowcs from standard library, but directly writing to Irrlicht string class.
|
|
|
|
What the function does exactly depends on the LC_CTYPE of the current c locale.
|
|
|
|
\param destination Wide-character string receiving the converted source
|
|
|
|
\param source multibyte string
|
|
|
|
\return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed. */
|
|
|
|
static inline size_t multibyteToWString(string<wchar_t>& destination, const char* source)
|
|
|
|
{
|
|
|
|
const u32 s = source ? (u32)strlen(source) : 0;
|
|
|
|
return multibyteToWString(destination, source, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Internally used by the other multibyteToWString functions
|
|
|
|
static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize)
|
|
|
|
{
|
|
|
|
if ( sourceSize )
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
destination.str.resize(sourceSize+1);
|
2020-01-03 20:05:16 +01:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4996) // 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead.
|
|
|
|
#endif
|
2022-12-22 23:55:35 +01:00
|
|
|
const size_t written = mbstowcs(&destination[0], source, (size_t)sourceSize);
|
2020-01-03 20:05:16 +01:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
if ( written != (size_t)-1 )
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
destination.str.resize(written);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
|
|
|
destination.clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 21:44:56 +02:00
|
|
|
//! Same as multibyteToWString, but the other way around
|
|
|
|
static inline size_t wStringToMultibyte(string<c8>& destination, const core::string<wchar_t>& source)
|
|
|
|
{
|
|
|
|
return wStringToMultibyte(destination, source.c_str(), (u32)source.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Same as multibyteToWString, but the other way around
|
|
|
|
static inline size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source)
|
|
|
|
{
|
|
|
|
const u32 s = source ? (u32)wcslen(source) : 0;
|
|
|
|
return wStringToMultibyte(destination, source, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Same as multibyteToWString, but the other way around
|
|
|
|
static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize)
|
|
|
|
{
|
|
|
|
if ( sourceSize )
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
destination.str.resize(sizeof(wchar_t)*sourceSize+1);
|
2021-08-30 21:44:56 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe. Consider using wcstombs_s instead.
|
|
|
|
#endif
|
2022-12-22 23:55:35 +01:00
|
|
|
const size_t written = wcstombs(&destination[0], source, destination.size());
|
2021-08-30 21:44:56 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
if ( written != (size_t)-1 )
|
|
|
|
{
|
2022-12-22 23:55:35 +01:00
|
|
|
destination.str.resize(written);
|
2021-08-30 21:44:56 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
|
|
|
destination.clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
} // end namespace core
|
|
|
|
} // end namespace irr
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|