NXEngine v1.0.0.6
[NXEngine.git] / common / DString.cpp
blob27f779bfc1abb2d9ea4d521a98038658efe943b2
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
6 #include "DString.h"
7 #include "DString.fdh"
10 DString::DString()
14 DString::DString(const char *str)
16 SetTo(str);
19 DString::DString(const char *str, int length)
21 SetTo(str, length);
24 DString::DString(DString &other)
26 SetTo(other.String(), other.Length());
30 void c------------------------------() {}
33 void DString::SetTo(const char *str, int length)
35 fBuffer.SetTo((uint8_t *)str, length);
38 void DString::SetTo(const char *str)
40 fBuffer.SetTo((uint8_t *)str, strlen(str));
43 void DString::SetTo(DString *other)
45 fBuffer.SetTo(other->fBuffer.Data(), other->fBuffer.Length());
48 void DString::SetTo(DString &other)
50 fBuffer.SetTo(other.fBuffer.Data(), other.fBuffer.Length());
54 void c------------------------------() {}
57 void DString::AppendString(const char *str)
59 fBuffer.AppendData((uint8_t *)str, strlen(str));
62 void DString::AppendString(const char *str, int length)
64 fBuffer.AppendData((uint8_t *)str, length);
67 void DString::AppendChar(uchar ch)
69 fBuffer.AppendData((uint8_t *)&ch, 1);
73 void c------------------------------() {}
76 void DString::ReplaceString(const char *repstr_old, const char *repstr_new)
78 DString newString;
79 char *str = String();
80 char *ptr = str;
81 int oldLength, newLength;
82 char *hit;
84 for(;;)
86 hit = strstr(ptr, repstr_old);
87 if (!hit)
89 // first time around the loop? if so then we don't need
90 // to do any copying as we won't modify ourselves anyway.
91 if (ptr == str)
92 return;
94 newString.AppendString(ptr);
95 break;
98 // defer calling the strlens until we're sure we need them
99 // (by now we know there is an occurance of repstr_old within the original string).
100 if (ptr == str)
102 oldLength = strlen(repstr_old);
103 if (oldLength == 0) return;
105 newLength = strlen(repstr_new);
108 // add substring up to the hit
109 newString.AppendString(ptr, (hit - ptr));
110 newString.AppendString(repstr_new, newLength);
112 ptr = (hit + oldLength);
115 SetTo(newString);
119 char *str = String();
120 char *hit = strstr(str, oldstring);
121 if (!hit) return; // avoid strlens in common case of no hits
123 int oldlen = strlen(oldstring);
124 int newlen = strlen(newstring);
126 for(;;)
128 DString temp(str, (hit - str));
129 temp.AppendString(newstring);
130 temp.AppendString(hit + oldlen);
132 SetTo(temp.String());
134 int index = (hit - str) + newlen;
135 str = String() + index;
137 hit = strstr(str, oldstring);
138 if (!hit) break;
143 void c------------------------------() {}
146 void DString::EnsureAlloc(int min_required)
148 fBuffer.EnsureAlloc(min_required);
151 void DString::ReplaceUnprintableChars()
153 fBuffer.ReplaceUnprintableChars();
157 void c------------------------------() {}
160 void DString::Clear()
162 fBuffer.Clear();
165 int DString::Length()
167 return fBuffer.Length();
170 char *DString::String()
172 return fBuffer.String();