Newton Dynamics  4.00
ndString.h
1 /* Copyright (c) <2003-2022> <Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely
10 */
11 
12 
13 #ifndef __NDSTRING_H_
14 #define __NDSTRING_H_
15 
16 #include "ndCoreStdafx.h"
17 #include "ndClassAlloc.h"
18 
19 class ndString: public ndClassAlloc
20 {
21  class ndStringAllocator;
22  public:
23  D_CORE_API ndString ();
24  D_CORE_API ndString (char chr);
25  D_CORE_API ndString (const ndString& src);
26  D_CORE_API ndString (const char* const data);
27  D_CORE_API ndString (const char* const data, ndInt32 maxSize);
28  D_CORE_API ndString (ndInt32 val);
29  D_CORE_API ndString (ndUnsigned64 val);
30  D_CORE_API ~ndString ();
31 
32  char& operator[] (ndInt32 index);
33  char operator[] (ndInt32 index) const;
34 
35  D_CORE_API ndString& operator= (const ndString& src);
36  bool operator== (const ndString& src) const;
37  bool operator!= (const ndString& src) const;
38  bool operator< (const ndString& src) const;
39  bool operator> (const ndString& src) const;
40  bool operator<= (const ndString& src) const;
41  bool operator>= (const ndString& src) const;
42 
43  D_CORE_API void operator+= (const char* const src);
44  void operator+= (const ndString& src);
45 
46  ndString operator+ (const char* const src) const;
47  ndString operator+ (const ndString& src) const;
48 
49  D_CORE_API ndInt32 Find (char ch, ndInt32 from = 0) const;
50  ndInt32 Find (const ndString& subString, ndInt32 from = 0) const;
51  D_CORE_API ndInt32 Find (const char* const subString, ndInt32 from = 0, ndInt32 lenght = 0x7ffffff) const;
52 
53  D_CORE_API void Replace (ndInt32 start, ndInt32 size, const char* const str, ndInt32 strSize);
54  void Replace (ndInt32 start, ndInt32 size, const ndString& str);
55 
56  void Clear();
57  void Empty();
58 
59  D_CORE_API void ToUpper();
60  D_CORE_API void ToLower();
61  D_CORE_API ndInt32 ToInteger() const;
62  D_CORE_API ndFloat64 ToFloat() const;
63  D_CORE_API ndUnsigned64 ToInteger64() const;
64 
65  ndInt32 Size() const;
66  ndInt32 Capacity() const;
67  D_CORE_API void Expand (ndInt32 size);
68 
69  D_CORE_API void LoadFile (FILE* const file);
70  ndString SubString(ndInt32 start = 0, ndInt32 size = 0x7fffffff) const;
71 
72  const char* GetStr () const;
73 
74  private:
75  D_CORE_API ndInt32 CalculateSize (const char* const data) const;
76  ndInt32 Compare (const char* const str0, const char* const str1) const;
77  void CopyData (char* const dst, const char* const src, ndInt32 size) const;
78 
79  D_CORE_API ndInt32 Find (const char* const subString, ndInt32 stringSize, ndInt32 from, ndInt32 lenght) const;
80 
81  protected:
82  char* AllocMem(ndInt32 size);
83  void FreeMem (char* const ptr);
84  D_CORE_API ndString (const ndString& src, const char* const concatenate, ndInt32 maxSize);
85 
86  char* m_string;
87  ndInt32 m_size;
88  ndInt32 m_capacity;
89 
90  private:
91  ndStringAllocator& GetAllocator() const;
92 };
93 
94 inline char& ndString::operator[] (ndInt32 index)
95 {
96  ndAssert (m_string);
97  ndAssert (index >= 0);
98  ndAssert (index < m_size);
99  return m_string[index];
100 }
101 
102 inline char ndString::operator[] (ndInt32 index) const
103 {
104  ndAssert (m_string);
105  ndAssert (index >= 0);
106  ndAssert (index < m_size);
107  return m_string[index];
108 }
109 
110 inline const char* ndString::GetStr () const
111 {
112  return m_string;
113 }
114 
115 inline ndInt32 ndString::Size() const
116 {
117  return m_size;
118 }
119 
120 inline ndInt32 ndString::Find (const char* const subString, ndInt32 from, ndInt32 lenght) const
121 {
122  return Find (subString, CalculateSize(subString), from, lenght);
123 }
124 
125 inline ndInt32 ndString::Find (const ndString& subStream, ndInt32 from) const
126 {
127  ndAssert (subStream.m_string);
128  return Find (subStream.m_string, subStream.m_size, from, subStream.m_size);
129 }
130 
131 inline void ndString::Replace (ndInt32 start, ndInt32 size, const ndString& str)
132 {
133  Replace(start, size, str.m_string, str.m_size);
134 }
135 
136 inline void ndString::operator+= (const ndString& src)
137 {
138  *this += src.m_string;
139 }
140 
141 inline ndString ndString::operator+ (const ndString& src) const
142 {
143  return ndString (*this, src.m_string, src.m_size);
144 }
145 
146 inline ndString ndString::operator+ (const char* const copy) const
147 {
148  return ndString (*this, copy, CalculateSize (copy));
149 }
150 
151 inline ndInt32 ndString::Capacity() const
152 {
153  return m_capacity;
154 }
155 
156 inline void ndString::CopyData (char* const dst, const char* const src, ndInt32 size) const
157 {
158  ndAssert (dst);
159  ndAssert (src);
160  memcpy (dst, src, size_t(size));
161 }
162 
163 inline ndInt32 ndString::Compare (const char* const str0, const char* const str1) const
164 {
165  ndAssert (str0);
166  ndAssert (str1);
167  return strcmp (str0, str1);
168 }
169 
170 inline bool ndString::operator== (const ndString& src) const
171 {
172  return Compare (m_string, src.m_string) == 0;
173 }
174 
175 inline bool ndString::operator!= (const ndString& src) const
176 {
177  return Compare (m_string, src.m_string) != 0;
178 }
179 
180 
181 inline bool ndString::operator< (const ndString& src) const
182 {
183  return Compare (m_string, src.m_string) < 0;
184 }
185 
186 inline bool ndString::operator> (const ndString& src) const
187 {
188  return Compare (m_string, src.m_string) > 0;
189 }
190 
191 inline bool ndString::operator<= (const ndString& src) const
192 {
193  return Compare (m_string, src.m_string) <= 0;
194 }
195 
196 inline bool ndString::operator>= (const ndString& src) const
197 {
198  return Compare (m_string, src.m_string) >= 0;
199 }
200 
201 inline ndString ndString::SubString(ndInt32 start, ndInt32 size) const
202 {
203  ndAssert (m_string);
204  return ndString (&m_string[start], size);
205 }
206 
207 
208 #endif
209 
210 
211 
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndString
Definition: ndString.h:20
ndString::ndStringAllocator
Definition: ndString.cpp:25