-
Notifications
You must be signed in to change notification settings - Fork 0
/
hstring.cpp
213 lines (161 loc) · 3.57 KB
/
hstring.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <string.h>
#include "HString.h"
HString::HString():
AString(),
itsNumber (0),
itsLocation (NULL)
{
}
HString::HString(const HString & rhs):
AString(rhs.GetStr())
{
fillLoc();
}
HString::HString(const AString & rhs):
AString(rhs)
{
fillLoc();
}
HString::HString(const char * const str):
AString(str)
{
fillLoc();
}
HString::HString(const char ch):
AString(ch)
{
fillLoc();
}
HString::~HString()
{
itsNumber = 0;
delete [] itsLocation;
}
HString& HString::operator=(const HString & rhs)
{
if (this == &rhs)
return *this;
itsLength=rhs.GetLen();
delete [] itsString;
itsString = new char[itsLength+1];
strcpy(itsString,rhs.GetStr());
delete[] itsLocation;
// fillLoc(); //비효율적.
itsNumber=rhs.GetNum();
itsLocation = new int[itsNumber/LocRate+1];
for(size_t i=0;i<itsNumber/LocRate+1;i++)
itsLocation[i]=rhs.GetLoc(i*LocRate);
return *this;
}
void HString::operator+=(const HString& rhs)
{
itsLength+=rhs.GetLen();
char *temp = new char[itsLength+1];
strcpy(temp,itsString);
strcat(temp,rhs.GetStr());
delete [] itsString;
itsString = temp;
delete[] itsLocation;
fillLoc();
}
void HString::operator+=(const char ch)
{
char * temp = new char[itsLength+2];
strcpy(temp,itsString);
temp[itsLength++] = ch;
temp[itsLength] = '\0';
delete [] itsString;
itsString = temp;
// 꼭 위치를 추가 해야하는 상황에서만 추가.
if( itsNumber % LocRate == 0 )
{
delete[] itsLocation;
fillLoc();
}
else
itsNumber++;
}
// LocRate(e.g. 10개) 단위로 저장된 위치부터 원하는 위치를 찾아준다.
int HString::GetLoc(int p) const
{
if( !itsNumber || p<0 || p>=itsNumber )
return -1;
int start = itsLocation[p / LocRate];
for(int count=0,i=0;count<LocRate;count++,i++)
{
if( p % LocRate == count )
return (int)(start+i);
if(Is2byteCharacter(itsString[start+i]))
i++;
}
return -1;
}
void HString::Clear()
{
AString::Clear();
//char * temp = new char[1];
//*temp = '\0';
//delete[] itsString;
//itsString = temp;
//itsLength = 0;
fillLoc();
}
HString HString::SubStr( int start, int end ) const
{
if( !itsNumber || start >= itsNumber || end < 0 || start > end )
return "";
start = ( start <= 0 ) ? 0 :GetLoc(start);
end = ( end < itsNumber )? GetLoc(end) : GetLoc(itsNumber-1);
if(Is2byteCharacter(itsString[end]))
end++;
int SubStrLen = end-start+1;
char *temp = new char[SubStrLen+1];
strncpy(temp,&itsString[start],SubStrLen);
temp[SubStrLen] = '\0';
HString ret( temp );
delete[] temp;
return ret;
}
int HString::Find( char ch, int position ) const
{
char *ptr;
if( !itsNumber || position >= itsNumber || Is2byteCharacter(ch) )
return -1;
if( position < 0 )
position = 0;
if((ptr=strchr(&itsString[GetLoc(position)],ch)) == NULL)
return -1;
int n = ptr-itsString;
for(int i=position;i<itsNumber;i++)
if(GetLoc(i)==n)
return (int)i;
return -1;
}
int HString::Find( HString str, int position ) const
{
char * ptr;
if( !itsNumber || position >= itsNumber )
return -1;
if( position < 0 )
position = 0;
if( (ptr=strstr(&itsString[GetLoc(position)],str.GetStr())) == NULL)
return -1;
int n = ptr-itsString;
for(int i=position;i<itsNumber;i++)
if(GetLoc(i)==n)
return i;
return -1;
}
// LocRate(e.g. 10개) 마다 위치를 한번씩 저장한다. 문자열 처음부터 끝까지 선형 접근한다.
void HString::fillLoc()
{
itsLocation = new int[itsLength/LocRate+1];
itsNumber=0;
for(int count=0,i=0;i<itsLength;i++,itsNumber++)
{
if(itsNumber%LocRate==0)
itsLocation[count++]=i;
if(Is2byteCharacter(itsString[i]))
i++;
}
}