Newton Dynamics  4.00
dString.h
1 /* Copyright (c) <2003-2019> <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 __DSTRING_H_
14 #define __DSTRING_H_
15 
16 #include "dCoreStdafx.h"
17 #include "dClassAlloc.h"
18 
19 class dString: public dClassAlloc
20 {
21  class dStringAllocator;
22  public:
23  D_CORE_API dString ();
24  D_CORE_API dString (char chr);
25  D_CORE_API dString (const dString& src);
26  D_CORE_API dString (const char* const data);
27  D_CORE_API dString (const char* const data, dInt32 maxSize);
28  D_CORE_API dString (dInt32 val);
29  D_CORE_API dString (dUnsigned64 val);
30  D_CORE_API ~dString ();
31 
32  char& operator[] (dInt32 index);
33  char operator[] (dInt32 index) const;
34 
35  D_CORE_API dString& operator= (const dString& src);
36  bool operator== (const dString& src) const;
37  bool operator!= (const dString& src) const;
38  bool operator< (const dString& src) const;
39  bool operator> (const dString& src) const;
40  bool operator<= (const dString& src) const;
41  bool operator>= (const dString& src) const;
42 
43  D_CORE_API void operator+= (const char* const src);
44  void operator+= (const dString& src);
45 
46  dString operator+ (const char* const src) const;
47  dString operator+ (const dString& src) const;
48 
49  D_CORE_API dInt32 Find (char ch, dInt32 from = 0) const;
50  dInt32 Find (const dString& subString, dInt32 from = 0) const;
51  D_CORE_API dInt32 Find (const char* const subString, dInt32 from = 0, dInt32 lenght = 0x7ffffff) const;
52 
53  D_CORE_API void Replace (dInt32 start, dInt32 size, const char* const str, dInt32 strSize);
54  void Replace (dInt32 start, dInt32 size, const dString& 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 dInt32 ToInteger() const;
62  D_CORE_API dFloat64 ToFloat() const;
63  D_CORE_API dUnsigned64 ToInteger64() const;
64 
65  dInt32 Size() const;
66  dInt32 Capacity() const;
67  D_CORE_API void Expand (dInt32 size);
68 
69  D_CORE_API void LoadFile (FILE* const file);
70  dString SubString(dInt32 start = 0, dInt32 size = 0x7fffffff) const;
71 
72  const char* GetStr () const;
73 
74  private:
75  D_CORE_API dInt32 CalculateSize (const char* const data) const;
76  dInt32 Compare (const char* const str0, const char* const str1) const;
77  void CopyData (char* const dst, const char* const src, dInt32 size) const;
78 
79  D_CORE_API dInt32 Find (const char* const subString, dInt32 stringSize, dInt32 from, dInt32 lenght) const;
80 
81  protected:
82  char* AllocMem(dInt32 size);
83  void FreeMem (char* const ptr);
84  D_CORE_API dString (const dString& src, const char* const concatenate, dInt32 maxSize);
85 
86  char* m_string;
87  dInt32 m_size;
88  dInt32 m_capacity;
89 
90  private:
91  dStringAllocator& GetAllocator() const;
92 };
93 
94 inline char& dString::operator[] (dInt32 index)
95 {
96  dAssert (m_string);
97  dAssert (index >= 0);
98  dAssert (index < m_size);
99  return m_string[index];
100 }
101 
102 inline char dString::operator[] (dInt32 index) const
103 {
104  dAssert (m_string);
105  dAssert (index >= 0);
106  dAssert (index < m_size);
107  return m_string[index];
108 }
109 
110 inline const char* dString::GetStr () const
111 {
112  return m_string;
113 }
114 
115 inline dInt32 dString::Size() const
116 {
117  return m_size;
118 }
119 
120 inline dInt32 dString::Find (const char* const subString, dInt32 from, dInt32 lenght) const
121 {
122  return Find (subString, CalculateSize(subString), from, lenght);
123 }
124 
125 inline dInt32 dString::Find (const dString& subStream, dInt32 from) const
126 {
127  dAssert (subStream.m_string);
128  return Find (subStream.m_string, subStream.m_size, from, subStream.m_size);
129 }
130 
131 inline void dString::Replace (dInt32 start, dInt32 size, const dString& str)
132 {
133  Replace(start, size, str.m_string, str.m_size);
134 }
135 
136 inline void dString::operator+= (const dString& src)
137 {
138  *this += src.m_string;
139 }
140 
141 inline dString dString::operator+ (const dString& src) const
142 {
143  return dString (*this, src.m_string, src.m_size);
144 }
145 
146 inline dString dString::operator+ (const char* const copy) const
147 {
148  return dString (*this, copy, CalculateSize (copy));
149 }
150 
151 
152 inline dInt32 dString::Capacity() const
153 {
154  return m_capacity;
155 }
156 
157 inline void dString::CopyData (char* const dst, const char* const src, dInt32 size) const
158 {
159  dAssert (dst);
160  dAssert (src);
161  memcpy (dst, src, size);
162 }
163 
164 inline dInt32 dString::Compare (const char* const str0, const char* const str1) const
165 {
166  dAssert (str0);
167  dAssert (str1);
168  return strcmp (str0, str1);
169 }
170 
171 
172 inline bool dString::operator== (const dString& src) const
173 {
174  return Compare (m_string, src.m_string) == 0;
175 }
176 
177 inline bool dString::operator!= (const dString& src) const
178 {
179  return Compare (m_string, src.m_string) != 0;
180 }
181 
182 
183 inline bool dString::operator< (const dString& src) const
184 {
185  return Compare (m_string, src.m_string) < 0;
186 }
187 
188 inline bool dString::operator> (const dString& src) const
189 {
190  return Compare (m_string, src.m_string) > 0;
191 }
192 
193 inline bool dString::operator<= (const dString& src) const
194 {
195  return Compare (m_string, src.m_string) <= 0;
196 }
197 
198 inline bool dString::operator>= (const dString& src) const
199 {
200  return Compare (m_string, src.m_string) >= 0;
201 }
202 
203 inline dString dString::SubString(dInt32 start, dInt32 size) const
204 {
205  dAssert (m_string);
206  return dString (&m_string[start], size);
207 }
208 
209 
210 #endif
211 
212 
213 
dString::dStringAllocator
Definition: dString.cpp:23
dString
Definition: dString.h:20
dClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: dClassAlloc.h:29