-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringBuilder.cpp
150 lines (125 loc) · 2.83 KB
/
StringBuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "StringBuilder.h"
#include <string.h>
#ifndef NULL
#define NULL 0
#endif //NULL
StringBuilder::StringBuilder(MallocFuncPtr pMallocFunc, FreeFuncPtr pFreeFunc)
{
m_pMallocFunc = pMallocFunc;
m_pFreeFunc = pFreeFunc;
m_iPosition = 0;
m_oFirstPool.m_pNext = NULL;
m_pCurrentPool = &m_oFirstPool;
m_iCurrentPoolIndex = 0;
}
StringBuilder::~StringBuilder()
{
Clear(true);
}
void StringBuilder::Clear(bool bFreePools)
{
m_iPosition = 0;
m_pCurrentPool = &m_oFirstPool;
m_iCurrentPoolIndex = 0;
if (bFreePools)
{
Pool* pPool = m_oFirstPool.m_pNext;
while (NULL != pPool)
{
Pool* pNextPool = pPool->m_pNext;
m_pFreeFunc(pPool);
pPool = pNextPool;
}
}
}
void StringBuilder::Append(char cChar)
{
AppendInternal(&cChar, 1);
}
void StringBuilder::Append(const char* pString)
{
AppendInternal(pString, (uint32)strlen(pString));
}
void StringBuilder::CopyTo(char* pDest)
{
if (pDest != NULL)
{
Pool* pCurrentPool = &m_oFirstPool;
for (uint32 iPos = 0; iPos < m_iPosition; iPos += c_iPoolSize)
{
int iLen = m_iPosition - iPos;
if (iLen > c_iPoolSize)
iLen = c_iPoolSize;
memcpy(pDest + iPos, pCurrentPool->m_oData, iLen);
pCurrentPool = pCurrentPool->m_pNext;
}
*(pDest + m_iPosition) = 0;
}
}
char* StringBuilder::Export()
{
if (m_iPosition == 0)
return NULL;
char* pOut = (char*)m_pMallocFunc(m_iPosition + 1);
CopyTo(pOut);
return pOut;
}
void StringBuilder::operator +=(char cChar)
{
Append(cChar);
}
void StringBuilder::operator +=(const char* pString)
{
Append(pString);
}
#ifdef __STRINGBUILDER_USE_STD_STRING__
void StringBuilder::Append(const std::string& oString)
{
AppendInternal(oString.c_str(), (uint32)oString.length());
}
void StringBuilder::CopyTo(std::string& sOut)
{
sOut.resize(m_iPosition);
char* pOut = (char*)sOut.data();
CopyTo(pOut);
}
void StringBuilder::operator +=(const std::string& oString)
{
Append(pString);
}
#endif //__STRINGBUILDER_USE_STD_STRING__
void StringBuilder::AppendInternal(const char* pString, uint32 iLen)
{
if (iLen == 0)
return;
// Copy pString to pools
uint32 iPoolPos = m_iPosition - m_iCurrentPoolIndex * c_iPoolSize;
uint32 iStrPos = 0;
while (iStrPos < iLen)
{
if (iPoolPos >= c_iPoolSize)
{
if (m_pCurrentPool->m_pNext == NULL)
{
Pool* pNewPool = (Pool*)m_pMallocFunc(sizeof(Pool));
pNewPool->m_pNext = NULL;
m_pCurrentPool->m_pNext = pNewPool;
m_pCurrentPool = pNewPool;
}
else
{
m_pCurrentPool = m_pCurrentPool->m_pNext;
}
++m_iCurrentPoolIndex;
iPoolPos = iPoolPos - c_iPoolSize;
}
int iMaxCopySize = c_iPoolSize - iPoolPos;
int iToCopySize = iLen - iStrPos;
if (iToCopySize > iMaxCopySize)
iToCopySize = iMaxCopySize;
memcpy(m_pCurrentPool->m_oData + iPoolPos, pString + iStrPos, iToCopySize);
iStrPos += iToCopySize;
iPoolPos += iToCopySize;
}
m_iPosition += iLen;
}