5#ifndef JPP_STRING_HPP_INCLUDED
6#define JPP_STRING_HPP_INCLUDED
8#include <jimcpp/core/engine/jpp_types.hpp>
9#include <jimcpp/core/engine/irrAllocator.hpp>
10#include <jimcpp/core/engine/irrMath.hpp>
36template <
typename T,
typename TAlloc = irrAllocator<T> >
38static size_t multibyteToWString(string<wchar_t>& destination,
const char* source,
u32 sourceSize);
47static eLocaleID locale_current = JPP_LOCALE_ANSI;
48static inline void locale_set ( eLocaleID
id )
54static inline u32 locale_lower (
u32 x )
56 switch ( locale_current )
58 case JPP_LOCALE_GERMAN:
63 return x >=
'A' && x <=
'Z' ? x + 0x20 : x;
67static inline u32 locale_upper (
u32 x )
69 switch ( locale_current )
71 case JPP_LOCALE_GERMAN:
77 return x >=
'a' && x <=
'z' ? x + (
'A' -
'a' ) : x;
93template <
typename T,
typename TAlloc>
104 :
array(0), allocated(1), used(1)
106 array = allocator.allocate(1);
113 :
array(0), allocated(0), used(0)
119 template <
class B,
class A>
121 :
array(0), allocated(0), used(0)
129 :
array(0), allocated(0), used(0)
131 std::ostringstream
tmpbuf;
133 *
this =
tmpbuf.str().data();
139 :
array(0), allocated(0), used(0)
187 :
array(0), allocated(0), used(0)
218 :
array(0), allocated(0), used(0)
266 :
array(0), allocated(0), used(0)
298 :
array(0), allocated(0), used(0)
307 allocated = used =
length+1;
308 array = allocator.allocate(used);
320 :
array(0), allocated(0), used(0)
331 allocator.deallocate(
array);
341 used =
other.size()+1;
344 allocator.deallocate(
array);
346 array = allocator.allocate(used);
350 for (
u32 i=0; i<used; ++i, ++
p)
357 template <
class B,
class A>
360 *
this =
other.data();
373 array = allocator.allocate(1);
381 if ((
void*)c == (
void*)
array)
399 array = allocator.allocate(used);
436 JPP_DEBUG_BREAK_IF(index>=used)
444 JPP_DEBUG_BREAK_IF(index>=used)
471 return used ==
other.used;
485 return (used <
other.used);
492 return !(*
this ==
str);
499 return !(*
this ==
other);
515 return (
size() == 0);
538 std::basic_string<T>
str()
const
545 explicit operator std::basic_string<T> ()
const
574 if (locale_lower(
array[i]) != locale_lower(
other[i]))
577 return used ==
other.used;
610 return used <
other.used;
627 return (i ==
n) || (used ==
other.used);
640 for(i=0; i <
n &&
array[i] &&
str[i]; ++i)
646 return (i ==
n) || (
array[i] == 0 &&
str[i] == 0);
654 if (used + 1 > allocated)
655 reallocate(used + 1);
684 if (used +
len > allocated)
685 reallocate(used +
len);
703 if (
other.size() == 0)
709 if (used +
len > allocated)
710 reallocate(used +
len);
726 if (
other.size() == 0)
735 if (used +
length > allocated)
736 reallocate(used +
length);
762 const u32 end = used+
n-1;
763 for (
u32 i=0; i<used-pos; ++i)
769 for (
u32 i=0; i<
n; ++i)
782 if (count < allocated)
795 for (
u32 i=0; i<used-1; ++i)
814 for (
u32 i=0; i<used-1; ++i)
815 for (
u32 j=0;
j<count; ++
j)
836 for (
u32 i=0; i<used-1; ++i)
839 for (
j=0;
j<count; ++
j)
863 for (
s32 i=(
s32)(used-2); i>=0; --i)
866 for (
j=0;
j<count; ++
j)
900 for (
s32 i=start; i>=0; --i)
919 for (
s32 i=(
s32)used-2; i>=0; --i)
920 for (
u32 j=0;
j<count; ++
j)
946 for (
u32 i=start; i<used-
len; ++i)
982 o.array[i] =
array[i+begin];
987 o.array[i] = locale_lower (
array[i+begin] );
1083 for (
u32 i=0; i<used-1; ++i)
1123 for (
u32 pos = 0; pos < used; ++i, ++pos)
1168 if (used +
len > allocated)
1169 reallocate(used +
len);
1180 while (
ptr != start)
1205 for (
u32 i=0; i<used-1; ++i)
1230 for (
u32 i=0; i<used-1; ++i)
1263 for (
u32 i=0; i<used-1; ++i)
1301 return (*
this =
subString(begin, (end +1) - begin));
1316 if ( i > 0 && (
u32)i < used-2 )
1346 JPP_DEBUG_BREAK_IF(index>=used)
1348 for (
u32 i=index+1; i<used; ++i)
1359 for (
u32 i=0; i<allocated; ++i)
1369 if ( allocated > 0 )
1385 return used > 1 ?
array[used-2] : 0;
1407 template<
class container>
1416 for (
u32 i=0; i<used; ++i)
1460 if (allocated < used)
1498static inline size_t multibyteToWString(string<wchar_t>& destination,
const char* source)
1500 const u32 s = source ? (
u32)strlen(source) : 0;
1501 return multibyteToWString(destination, source, s);
1510#if defined(_MSC_VER)
1511#pragma warning(push)
1512#pragma warning(disable: 4996)
1515#if defined(_MSC_VER)
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.hpp:23
Very simple string class with some useful features.
Definition irrString.hpp:95
string< T, TAlloc > & remove(T c)
Removes characters from a string.
Definition irrString.hpp:1201
T & operator[](const u32 index)
Direct access operator.
Definition irrString.hpp:434
string< T, TAlloc > & make_lower()
Makes the string lower case.
Definition irrString.hpp:551
s32 findFirstChar(const T *const c, u32 count=1) const
finds first occurrence of a character of a list in string
Definition irrString.hpp:809
string< T, TAlloc > & erase(u32 index)
Erases a character from the string.
Definition irrString.hpp:1344
bool lower_ignore_case(const string< T, TAlloc > &other) const
Compares the strings ignoring case.
Definition irrString.hpp:601
string< T, TAlloc > & replace(T toReplace, T replaceWith)
Replaces all characters of a special type with another one.
Definition irrString.hpp:1081
s32 findLastCharNotInList(const B *const c, u32 count=1) const
Finds last position of a character not in a given list.
Definition irrString.hpp:858
string< T, TAlloc > & remove(const string< T, TAlloc > &toRemove)
Removes a string from the string.
Definition irrString.hpp:1223
string< T, TAlloc > operator+(const B *const c) const
Append operator for strings, ASCII and Unicode.
Definition irrString.hpp:424
void reserve(u32 count)
Reserves some memory.
Definition irrString.hpp:780
bool operator!=(const string< T, TAlloc > &other) const
Inequality operator.
Definition irrString.hpp:497
string(unsigned long number)
Constructs a string from an unsigned long.
Definition irrString.hpp:265
string(const string< T, TAlloc > &other)
Constructor.
Definition irrString.hpp:112
string< T, TAlloc > & append(const string< T, TAlloc > &other)
Appends a string to this string.
Definition irrString.hpp:701
string< T, TAlloc > & make_upper()
Makes the string upper case.
Definition irrString.hpp:560
string< T, TAlloc > & operator=(const string< B, A > &other)
Assignment operator for other string types.
Definition irrString.hpp:358
string(const double number)
Constructs a string from a float.
Definition irrString.hpp:128
string< T, TAlloc > & append(T character)
Appends a character to this string.
Definition irrString.hpp:652
bool equalsn(const string< T, TAlloc > &other, u32 n) const
compares the first n characters of the strings
Definition irrString.hpp:618
bool operator==(const T *const str) const
Equality operator.
Definition irrString.hpp:450
~string()
Destructor.
Definition irrString.hpp:329
bool operator==(const string< T, TAlloc > &other) const
Equality operator.
Definition irrString.hpp:465
bool empty() const
Definition irrString.hpp:513
string< T, TAlloc > & operator+=(T c)
Appends a character to this string.
Definition irrString.hpp:999
string< T, TAlloc > & trim(const string< T, TAlloc > &whitespace=" \t\n\r")
Trims the string.
Definition irrString.hpp:1292
bool equals_ignore_case(const string< T, TAlloc > &other) const
Compares the strings ignoring case.
Definition irrString.hpp:571
T lastChar() const
gets the last char of a string or null
Definition irrString.hpp:1383
u32 size() const
Returns length of the string's content.
Definition irrString.hpp:506
s32 findFirstCharNotInList(const B *const c, u32 count=1) const
Finds first position of a character not in a given list.
Definition irrString.hpp:831
std::basic_string< T > str() const
Returns std::string or std::wstring. The traits_type and allocator_type of the returned std::basic_st...
Definition irrString.hpp:538
string< T, TAlloc > & replace(const string< T, TAlloc > &toReplace, const string< T, TAlloc > &replaceWith)
Replaces all instances of a string with another one.
Definition irrString.hpp:1093
string< T, TAlloc > & eraseTrailingFloatZeros(char decimalPoint='.')
Erase 0's at the end when a string ends with a floating point number.
Definition irrString.hpp:1313
string()
Default constructor.
Definition irrString.hpp:103
string(const string< B, A > &other)
Constructor from other string types.
Definition irrString.hpp:120
string< T, TAlloc > & append(const T *const other, u32 length=0xffffffff)
Appends a char string to this string.
Definition irrString.hpp:669
string(unsigned int number)
Constructs a string from an unsigned int.
Definition irrString.hpp:186
string< T, TAlloc > & append(const string< T, TAlloc > &other, u32 length)
Appends a string of the length l to this string.
Definition irrString.hpp:724
string< T, TAlloc > & validate()
verify the existing string.
Definition irrString.hpp:1356
bool operator<(const string< T, TAlloc > &other) const
Is smaller comparator.
Definition irrString.hpp:476
s32 findLastChar(const T *const c, u32 count=1) const
finds last occurrence of a character of a list in string
Definition irrString.hpp:914
bool equalsn(const T *const str, u32 n) const
compares the first n characters of the strings
Definition irrString.hpp:635
bool equals_substring_ignore_case(const string< T, TAlloc > &other, const s32 sourcePos=0) const
Compares the strings ignoring case.
Definition irrString.hpp:584
u32 split(container &ret, const T *const delimiter, u32 countDelimiters=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
Split string into parts (tokens).
Definition irrString.hpp:1408
string< T > subString(u32 begin, s32 length, bool make_lower=false) const
Returns a substring.
Definition irrString.hpp:966
s32 findLast(T c, s32 start=-1) const
finds last occurrence of character in string
Definition irrString.hpp:897
friend size_t multibyteToWString(string< wchar_t > &destination, const char *source, u32 sourceSize)
Internally used by the other multibyteToWString functions.
Definition irrString.hpp:1505
s32 findFirst(T c) const
finds first occurrence of character in string
Definition irrString.hpp:793
string< T, TAlloc > & operator=(const string< T, TAlloc > &other)
Assignment operator.
Definition irrString.hpp:336
bool operator!=(const T *const str) const
Inequality operator.
Definition irrString.hpp:490
string(const B *const c, u32 length)
Constructor for copying a string from a pointer with a given length.
Definition irrString.hpp:297
string(long number)
Constructs a string from a long.
Definition irrString.hpp:217
string< T, TAlloc > & insert(u32 pos, const char *s, u32 n)
Insert a certain amount of characters into the string before the given index.
Definition irrString.hpp:755
string(const B *const c)
Constructor for Unicode and ASCII strings.
Definition irrString.hpp:319
string< T, TAlloc > & removeChars(const string< T, TAlloc > &characters)
Removes characters from a string.
Definition irrString.hpp:1256
string< T, TAlloc > operator+(const string< T, TAlloc > &other) const
Append operator for other strings.
Definition irrString.hpp:413
string(int number)
Constructs a string from an int.
Definition irrString.hpp:138
string< T, TAlloc > & operator=(const B *const c)
Assignment operator for strings, ASCII and Unicode.
Definition irrString.hpp:367
const T * data() const
Returns character string.
Definition irrString.hpp:530
s32 find(const B *const str, const u32 start=0) const
finds another string in this string
Definition irrString.hpp:934
s32 findNext(T c, u32 startPos) const
finds next occurrence of character in string
Definition irrString.hpp:882
JIMCPP_API void wcharToUtf8(const wchar_t *in, char *out, const u64 len)
Convert this wchar string to utf-8.
JIMCPP_API void utf8ToWchar(const char *in, wchar_t *out, const u64 len)
Convert this utf-8-encoded string to the platform's wchar.
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.hpp:164
s32 isdigit(s32 c)
some standard function ( to remove dependencies )
Definition coreutil.hpp:197
As of Jimcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
signed int s32
32 bit signed variable.
Definition irrTypes.hpp:72
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64
char c8
8 bit character variable.
Definition irrTypes.hpp:37