xref: /third_party/lzma/CPP/Common/MyString.cpp (revision 370b324c)
1370b324cSopenharmony_ci// Common/MyString.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#ifdef _WIN32
6370b324cSopenharmony_ci#include <wchar.h>
7370b324cSopenharmony_ci#else
8370b324cSopenharmony_ci#include <ctype.h>
9370b324cSopenharmony_ci#endif
10370b324cSopenharmony_ci
11370b324cSopenharmony_ci#include "IntToString.h"
12370b324cSopenharmony_ci
13370b324cSopenharmony_ci#if !defined(_UNICODE) || !defined(USE_UNICODE_FSTRING)
14370b324cSopenharmony_ci#include "StringConvert.h"
15370b324cSopenharmony_ci#endif
16370b324cSopenharmony_ci
17370b324cSopenharmony_ci#include "MyString.h"
18370b324cSopenharmony_ci
19370b324cSopenharmony_ci#define MY_STRING_NEW(_T_, _size_) new _T_[_size_]
20370b324cSopenharmony_ci// #define MY_STRING_NEW(_T_, _size_) ((_T_ *)my_new((size_t)(_size_) * sizeof(_T_)))
21370b324cSopenharmony_ci
22370b324cSopenharmony_ci/*
23370b324cSopenharmony_ciinline const char* MyStringGetNextCharPointer(const char *p) throw()
24370b324cSopenharmony_ci{
25370b324cSopenharmony_ci  #if defined(_WIN32) && !defined(UNDER_CE)
26370b324cSopenharmony_ci  return CharNextA(p);
27370b324cSopenharmony_ci  #else
28370b324cSopenharmony_ci  return p + 1;
29370b324cSopenharmony_ci  #endif
30370b324cSopenharmony_ci}
31370b324cSopenharmony_ci*/
32370b324cSopenharmony_ci
33370b324cSopenharmony_ci#define MY_STRING_NEW_char(_size_) MY_STRING_NEW(char, (_size_))
34370b324cSopenharmony_ci#define MY_STRING_NEW_wchar_t(_size_) MY_STRING_NEW(wchar_t, (_size_))
35370b324cSopenharmony_ci
36370b324cSopenharmony_ci
37370b324cSopenharmony_ciint FindCharPosInString(const char *s, char c) throw()
38370b324cSopenharmony_ci{
39370b324cSopenharmony_ci  for (const char *p = s;; p++)
40370b324cSopenharmony_ci  {
41370b324cSopenharmony_ci    if (*p == c)
42370b324cSopenharmony_ci      return (int)(p - s);
43370b324cSopenharmony_ci    if (*p == 0)
44370b324cSopenharmony_ci      return -1;
45370b324cSopenharmony_ci    // MyStringGetNextCharPointer(p);
46370b324cSopenharmony_ci  }
47370b324cSopenharmony_ci}
48370b324cSopenharmony_ci
49370b324cSopenharmony_ciint FindCharPosInString(const wchar_t *s, wchar_t c) throw()
50370b324cSopenharmony_ci{
51370b324cSopenharmony_ci  for (const wchar_t *p = s;; p++)
52370b324cSopenharmony_ci  {
53370b324cSopenharmony_ci    if (*p == c)
54370b324cSopenharmony_ci      return (int)(p - s);
55370b324cSopenharmony_ci    if (*p == 0)
56370b324cSopenharmony_ci      return -1;
57370b324cSopenharmony_ci  }
58370b324cSopenharmony_ci}
59370b324cSopenharmony_ci
60370b324cSopenharmony_ci/*
61370b324cSopenharmony_civoid MyStringUpper_Ascii(char *s) throw()
62370b324cSopenharmony_ci{
63370b324cSopenharmony_ci  for (;;)
64370b324cSopenharmony_ci  {
65370b324cSopenharmony_ci    char c = *s;
66370b324cSopenharmony_ci    if (c == 0)
67370b324cSopenharmony_ci      return;
68370b324cSopenharmony_ci    *s++ = MyCharUpper_Ascii(c);
69370b324cSopenharmony_ci  }
70370b324cSopenharmony_ci}
71370b324cSopenharmony_ci
72370b324cSopenharmony_civoid MyStringUpper_Ascii(wchar_t *s) throw()
73370b324cSopenharmony_ci{
74370b324cSopenharmony_ci  for (;;)
75370b324cSopenharmony_ci  {
76370b324cSopenharmony_ci    wchar_t c = *s;
77370b324cSopenharmony_ci    if (c == 0)
78370b324cSopenharmony_ci      return;
79370b324cSopenharmony_ci    *s++ = MyCharUpper_Ascii(c);
80370b324cSopenharmony_ci  }
81370b324cSopenharmony_ci}
82370b324cSopenharmony_ci*/
83370b324cSopenharmony_ci
84370b324cSopenharmony_civoid MyStringLower_Ascii(char *s) throw()
85370b324cSopenharmony_ci{
86370b324cSopenharmony_ci  for (;;)
87370b324cSopenharmony_ci  {
88370b324cSopenharmony_ci    char c = *s;
89370b324cSopenharmony_ci    if (c == 0)
90370b324cSopenharmony_ci      return;
91370b324cSopenharmony_ci    *s++ = MyCharLower_Ascii(c);
92370b324cSopenharmony_ci  }
93370b324cSopenharmony_ci}
94370b324cSopenharmony_ci
95370b324cSopenharmony_civoid MyStringLower_Ascii(wchar_t *s) throw()
96370b324cSopenharmony_ci{
97370b324cSopenharmony_ci  for (;;)
98370b324cSopenharmony_ci  {
99370b324cSopenharmony_ci    wchar_t c = *s;
100370b324cSopenharmony_ci    if (c == 0)
101370b324cSopenharmony_ci      return;
102370b324cSopenharmony_ci    *s++ = MyCharLower_Ascii(c);
103370b324cSopenharmony_ci  }
104370b324cSopenharmony_ci}
105370b324cSopenharmony_ci
106370b324cSopenharmony_ci#ifdef _WIN32
107370b324cSopenharmony_ci
108370b324cSopenharmony_ci#ifdef _UNICODE
109370b324cSopenharmony_ci
110370b324cSopenharmony_ci// wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); }
111370b324cSopenharmony_ci// wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); }
112370b324cSopenharmony_ci// for WinCE - FString - char
113370b324cSopenharmony_ci// const char *MyStringGetPrevCharPointer(const char * /* base */, const char *p) { return p - 1; }
114370b324cSopenharmony_ci
115370b324cSopenharmony_ci#else
116370b324cSopenharmony_ci
117370b324cSopenharmony_ci// const char * MyStringGetPrevCharPointer(const char *base, const char *p) throw() { return CharPrevA(base, p); }
118370b324cSopenharmony_ci// char * MyStringUpper(char *s) { return CharUpperA(s); }
119370b324cSopenharmony_ci// char * MyStringLower(char *s) { return CharLowerA(s); }
120370b324cSopenharmony_ci
121370b324cSopenharmony_ciwchar_t MyCharUpper_WIN(wchar_t c) throw()
122370b324cSopenharmony_ci{
123370b324cSopenharmony_ci  wchar_t *res = CharUpperW((LPWSTR)(UINT_PTR)(unsigned)c);
124370b324cSopenharmony_ci  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
125370b324cSopenharmony_ci    return (wchar_t)(unsigned)(UINT_PTR)res;
126370b324cSopenharmony_ci  const int kBufSize = 4;
127370b324cSopenharmony_ci  char s[kBufSize + 1];
128370b324cSopenharmony_ci  int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufSize, 0, 0);
129370b324cSopenharmony_ci  if (numChars == 0 || numChars > kBufSize)
130370b324cSopenharmony_ci    return c;
131370b324cSopenharmony_ci  s[numChars] = 0;
132370b324cSopenharmony_ci  ::CharUpperA(s);
133370b324cSopenharmony_ci  ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
134370b324cSopenharmony_ci  return c;
135370b324cSopenharmony_ci}
136370b324cSopenharmony_ci
137370b324cSopenharmony_ci/*
138370b324cSopenharmony_ciwchar_t MyCharLower_WIN(wchar_t c)
139370b324cSopenharmony_ci{
140370b324cSopenharmony_ci  wchar_t *res = CharLowerW((LPWSTR)(UINT_PTR)(unsigned)c);
141370b324cSopenharmony_ci  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
142370b324cSopenharmony_ci    return (wchar_t)(unsigned)(UINT_PTR)res;
143370b324cSopenharmony_ci  const int kBufSize = 4;
144370b324cSopenharmony_ci  char s[kBufSize + 1];
145370b324cSopenharmony_ci  int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufSize, 0, 0);
146370b324cSopenharmony_ci  if (numChars == 0 || numChars > kBufSize)
147370b324cSopenharmony_ci    return c;
148370b324cSopenharmony_ci  s[numChars] = 0;
149370b324cSopenharmony_ci  ::CharLowerA(s);
150370b324cSopenharmony_ci  ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
151370b324cSopenharmony_ci  return c;
152370b324cSopenharmony_ci}
153370b324cSopenharmony_ci*/
154370b324cSopenharmony_ci
155370b324cSopenharmony_ci/*
156370b324cSopenharmony_ciwchar_t * MyStringUpper(wchar_t *s)
157370b324cSopenharmony_ci{
158370b324cSopenharmony_ci  if (s == 0)
159370b324cSopenharmony_ci    return 0;
160370b324cSopenharmony_ci  wchar_t *res = CharUpperW(s);
161370b324cSopenharmony_ci  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
162370b324cSopenharmony_ci    return res;
163370b324cSopenharmony_ci  AString a = UnicodeStringToMultiByte(s);
164370b324cSopenharmony_ci  a.MakeUpper();
165370b324cSopenharmony_ci  MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
166370b324cSopenharmony_ci  return s;
167370b324cSopenharmony_ci}
168370b324cSopenharmony_ci*/
169370b324cSopenharmony_ci
170370b324cSopenharmony_ci/*
171370b324cSopenharmony_ciwchar_t * MyStringLower(wchar_t *s)
172370b324cSopenharmony_ci{
173370b324cSopenharmony_ci  if (s == 0)
174370b324cSopenharmony_ci    return 0;
175370b324cSopenharmony_ci  wchar_t *res = CharLowerW(s);
176370b324cSopenharmony_ci  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
177370b324cSopenharmony_ci    return res;
178370b324cSopenharmony_ci  AString a = UnicodeStringToMultiByte(s);
179370b324cSopenharmony_ci  a.MakeLower();
180370b324cSopenharmony_ci  MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
181370b324cSopenharmony_ci  return s;
182370b324cSopenharmony_ci}
183370b324cSopenharmony_ci*/
184370b324cSopenharmony_ci
185370b324cSopenharmony_ci#endif
186370b324cSopenharmony_ci
187370b324cSopenharmony_ci#endif
188370b324cSopenharmony_ci
189370b324cSopenharmony_cibool IsString1PrefixedByString2(const char *s1, const char *s2) throw()
190370b324cSopenharmony_ci{
191370b324cSopenharmony_ci  for (;;)
192370b324cSopenharmony_ci  {
193370b324cSopenharmony_ci    const unsigned char c2 = (unsigned char)*s2++; if (c2 == 0) return true;
194370b324cSopenharmony_ci    const unsigned char c1 = (unsigned char)*s1++; if (c1 != c2) return false;
195370b324cSopenharmony_ci  }
196370b324cSopenharmony_ci}
197370b324cSopenharmony_ci
198370b324cSopenharmony_cibool StringsAreEqualNoCase(const wchar_t *s1, const wchar_t *s2) throw()
199370b324cSopenharmony_ci{
200370b324cSopenharmony_ci  for (;;)
201370b324cSopenharmony_ci  {
202370b324cSopenharmony_ci    const wchar_t c1 = *s1++;
203370b324cSopenharmony_ci    const wchar_t c2 = *s2++;
204370b324cSopenharmony_ci    if (c1 != c2 && MyCharUpper(c1) != MyCharUpper(c2)) return false;
205370b324cSopenharmony_ci    if (c1 == 0) return true;
206370b324cSopenharmony_ci  }
207370b324cSopenharmony_ci}
208370b324cSopenharmony_ci
209370b324cSopenharmony_ci// ---------- ASCII ----------
210370b324cSopenharmony_ci
211370b324cSopenharmony_cibool AString::IsPrefixedBy_Ascii_NoCase(const char *s) const throw()
212370b324cSopenharmony_ci{
213370b324cSopenharmony_ci  const char *s1 = _chars;
214370b324cSopenharmony_ci  for (;;)
215370b324cSopenharmony_ci  {
216370b324cSopenharmony_ci    const char c2 = *s++;
217370b324cSopenharmony_ci    if (c2 == 0)
218370b324cSopenharmony_ci      return true;
219370b324cSopenharmony_ci    const char c1 = *s1++;
220370b324cSopenharmony_ci    if (MyCharLower_Ascii(c1) !=
221370b324cSopenharmony_ci        MyCharLower_Ascii(c2))
222370b324cSopenharmony_ci      return false;
223370b324cSopenharmony_ci  }
224370b324cSopenharmony_ci}
225370b324cSopenharmony_ci
226370b324cSopenharmony_cibool UString::IsPrefixedBy_Ascii_NoCase(const char *s) const throw()
227370b324cSopenharmony_ci{
228370b324cSopenharmony_ci  const wchar_t *s1 = _chars;
229370b324cSopenharmony_ci  for (;;)
230370b324cSopenharmony_ci  {
231370b324cSopenharmony_ci    const char c2 = *s++;
232370b324cSopenharmony_ci    if (c2 == 0)
233370b324cSopenharmony_ci      return true;
234370b324cSopenharmony_ci    const wchar_t c1 = *s1++;
235370b324cSopenharmony_ci    if (MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2))
236370b324cSopenharmony_ci      return false;
237370b324cSopenharmony_ci  }
238370b324cSopenharmony_ci}
239370b324cSopenharmony_ci
240370b324cSopenharmony_cibool StringsAreEqual_Ascii(const char *u, const char *a) throw()
241370b324cSopenharmony_ci{
242370b324cSopenharmony_ci  for (;;)
243370b324cSopenharmony_ci  {
244370b324cSopenharmony_ci    const char c = *a;
245370b324cSopenharmony_ci    if (c != *u)
246370b324cSopenharmony_ci      return false;
247370b324cSopenharmony_ci    if (c == 0)
248370b324cSopenharmony_ci      return true;
249370b324cSopenharmony_ci    a++;
250370b324cSopenharmony_ci    u++;
251370b324cSopenharmony_ci  }
252370b324cSopenharmony_ci}
253370b324cSopenharmony_ci
254370b324cSopenharmony_cibool StringsAreEqual_Ascii(const wchar_t *u, const char *a) throw()
255370b324cSopenharmony_ci{
256370b324cSopenharmony_ci  for (;;)
257370b324cSopenharmony_ci  {
258370b324cSopenharmony_ci    const unsigned char c = (unsigned char)*a;
259370b324cSopenharmony_ci    if (c != *u)
260370b324cSopenharmony_ci      return false;
261370b324cSopenharmony_ci    if (c == 0)
262370b324cSopenharmony_ci      return true;
263370b324cSopenharmony_ci    a++;
264370b324cSopenharmony_ci    u++;
265370b324cSopenharmony_ci  }
266370b324cSopenharmony_ci}
267370b324cSopenharmony_ci
268370b324cSopenharmony_cibool StringsAreEqualNoCase_Ascii(const char *s1, const char *s2) throw()
269370b324cSopenharmony_ci{
270370b324cSopenharmony_ci  for (;;)
271370b324cSopenharmony_ci  {
272370b324cSopenharmony_ci    const char c1 = *s1++;
273370b324cSopenharmony_ci    const char c2 = *s2++;
274370b324cSopenharmony_ci    if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2))
275370b324cSopenharmony_ci      return false;
276370b324cSopenharmony_ci    if (c1 == 0)
277370b324cSopenharmony_ci      return true;
278370b324cSopenharmony_ci  }
279370b324cSopenharmony_ci}
280370b324cSopenharmony_ci
281370b324cSopenharmony_cibool StringsAreEqualNoCase_Ascii(const wchar_t *s1, const wchar_t *s2) throw()
282370b324cSopenharmony_ci{
283370b324cSopenharmony_ci  for (;;)
284370b324cSopenharmony_ci  {
285370b324cSopenharmony_ci    const wchar_t c1 = *s1++;
286370b324cSopenharmony_ci    const wchar_t c2 = *s2++;
287370b324cSopenharmony_ci    if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2))
288370b324cSopenharmony_ci      return false;
289370b324cSopenharmony_ci    if (c1 == 0)
290370b324cSopenharmony_ci      return true;
291370b324cSopenharmony_ci  }
292370b324cSopenharmony_ci}
293370b324cSopenharmony_ci
294370b324cSopenharmony_cibool StringsAreEqualNoCase_Ascii(const wchar_t *s1, const char *s2) throw()
295370b324cSopenharmony_ci{
296370b324cSopenharmony_ci  for (;;)
297370b324cSopenharmony_ci  {
298370b324cSopenharmony_ci    const wchar_t c1 = *s1++;
299370b324cSopenharmony_ci    const char c2 = *s2++;
300370b324cSopenharmony_ci    if (c1 != (unsigned char)c2 && (c1 > 0x7F || MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2)))
301370b324cSopenharmony_ci      return false;
302370b324cSopenharmony_ci    if (c1 == 0)
303370b324cSopenharmony_ci      return true;
304370b324cSopenharmony_ci  }
305370b324cSopenharmony_ci}
306370b324cSopenharmony_ci
307370b324cSopenharmony_cibool IsString1PrefixedByString2(const wchar_t *s1, const wchar_t *s2) throw()
308370b324cSopenharmony_ci{
309370b324cSopenharmony_ci  for (;;)
310370b324cSopenharmony_ci  {
311370b324cSopenharmony_ci    const wchar_t c2 = *s2++; if (c2 == 0) return true;
312370b324cSopenharmony_ci    const wchar_t c1 = *s1++; if (c1 != c2) return false;
313370b324cSopenharmony_ci  }
314370b324cSopenharmony_ci}
315370b324cSopenharmony_ci
316370b324cSopenharmony_cibool IsString1PrefixedByString2(const wchar_t *s1, const char *s2) throw()
317370b324cSopenharmony_ci{
318370b324cSopenharmony_ci  for (;;)
319370b324cSopenharmony_ci  {
320370b324cSopenharmony_ci    const unsigned char c2 = (unsigned char)(*s2++); if (c2 == 0) return true;
321370b324cSopenharmony_ci    const wchar_t c1 = *s1++; if (c1 != c2) return false;
322370b324cSopenharmony_ci  }
323370b324cSopenharmony_ci}
324370b324cSopenharmony_ci
325370b324cSopenharmony_cibool IsString1PrefixedByString2_NoCase_Ascii(const char *s1, const char *s2) throw()
326370b324cSopenharmony_ci{
327370b324cSopenharmony_ci  for (;;)
328370b324cSopenharmony_ci  {
329370b324cSopenharmony_ci    const char c2 = *s2++; if (c2 == 0) return true;
330370b324cSopenharmony_ci    const char c1 = *s1++;
331370b324cSopenharmony_ci    if (c1 != c2 && MyCharLower_Ascii(c1) != MyCharLower_Ascii(c2))
332370b324cSopenharmony_ci      return false;
333370b324cSopenharmony_ci  }
334370b324cSopenharmony_ci}
335370b324cSopenharmony_ci
336370b324cSopenharmony_cibool IsString1PrefixedByString2_NoCase_Ascii(const wchar_t *s1, const char *s2) throw()
337370b324cSopenharmony_ci{
338370b324cSopenharmony_ci  for (;;)
339370b324cSopenharmony_ci  {
340370b324cSopenharmony_ci    const char c2 = *s2++; if (c2 == 0) return true;
341370b324cSopenharmony_ci    const wchar_t c1 = *s1++;
342370b324cSopenharmony_ci    if (c1 != (unsigned char)c2 && MyCharLower_Ascii(c1) != (unsigned char)MyCharLower_Ascii(c2))
343370b324cSopenharmony_ci      return false;
344370b324cSopenharmony_ci  }
345370b324cSopenharmony_ci}
346370b324cSopenharmony_ci
347370b324cSopenharmony_cibool IsString1PrefixedByString2_NoCase(const wchar_t *s1, const wchar_t *s2) throw()
348370b324cSopenharmony_ci{
349370b324cSopenharmony_ci  for (;;)
350370b324cSopenharmony_ci  {
351370b324cSopenharmony_ci    const wchar_t c2 = *s2++; if (c2 == 0) return true;
352370b324cSopenharmony_ci    const wchar_t c1 = *s1++;
353370b324cSopenharmony_ci    if (c1 != c2 && MyCharUpper(c1) != MyCharUpper(c2))
354370b324cSopenharmony_ci      return false;
355370b324cSopenharmony_ci  }
356370b324cSopenharmony_ci}
357370b324cSopenharmony_ci
358370b324cSopenharmony_ci// NTFS order: uses upper case
359370b324cSopenharmony_ciint MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2) throw()
360370b324cSopenharmony_ci{
361370b324cSopenharmony_ci  for (;;)
362370b324cSopenharmony_ci  {
363370b324cSopenharmony_ci    const wchar_t c1 = *s1++;
364370b324cSopenharmony_ci    const wchar_t c2 = *s2++;
365370b324cSopenharmony_ci    if (c1 != c2)
366370b324cSopenharmony_ci    {
367370b324cSopenharmony_ci      const wchar_t u1 = MyCharUpper(c1);
368370b324cSopenharmony_ci      const wchar_t u2 = MyCharUpper(c2);
369370b324cSopenharmony_ci      if (u1 < u2) return -1;
370370b324cSopenharmony_ci      if (u1 > u2) return 1;
371370b324cSopenharmony_ci    }
372370b324cSopenharmony_ci    if (c1 == 0) return 0;
373370b324cSopenharmony_ci  }
374370b324cSopenharmony_ci}
375370b324cSopenharmony_ci
376370b324cSopenharmony_ci/*
377370b324cSopenharmony_ciint MyStringCompareNoCase_N(const wchar_t *s1, const wchar_t *s2, unsigned num)
378370b324cSopenharmony_ci{
379370b324cSopenharmony_ci  for (; num != 0; num--)
380370b324cSopenharmony_ci  {
381370b324cSopenharmony_ci    wchar_t c1 = *s1++;
382370b324cSopenharmony_ci    wchar_t c2 = *s2++;
383370b324cSopenharmony_ci    if (c1 != c2)
384370b324cSopenharmony_ci    {
385370b324cSopenharmony_ci      wchar_t u1 = MyCharUpper(c1);
386370b324cSopenharmony_ci      wchar_t u2 = MyCharUpper(c2);
387370b324cSopenharmony_ci      if (u1 < u2) return -1;
388370b324cSopenharmony_ci      if (u1 > u2) return 1;
389370b324cSopenharmony_ci    }
390370b324cSopenharmony_ci    if (c1 == 0) return 0;
391370b324cSopenharmony_ci  }
392370b324cSopenharmony_ci  return 0;
393370b324cSopenharmony_ci}
394370b324cSopenharmony_ci*/
395370b324cSopenharmony_ci
396370b324cSopenharmony_ci// ---------- AString ----------
397370b324cSopenharmony_ci
398370b324cSopenharmony_civoid AString::InsertSpace(unsigned &index, unsigned size)
399370b324cSopenharmony_ci{
400370b324cSopenharmony_ci  Grow(size);
401370b324cSopenharmony_ci  MoveItems(index + size, index);
402370b324cSopenharmony_ci}
403370b324cSopenharmony_ci
404370b324cSopenharmony_ci#define k_Alloc_Len_Limit (0x40000000 - 2)
405370b324cSopenharmony_ci
406370b324cSopenharmony_civoid AString::ReAlloc(unsigned newLimit)
407370b324cSopenharmony_ci{
408370b324cSopenharmony_ci  // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, (size_t)_len + 1);
409370b324cSopenharmony_ci  char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1);
410370b324cSopenharmony_ci  memcpy(newBuf, _chars, (size_t)_len + 1);
411370b324cSopenharmony_ci  MY_STRING_DELETE(_chars)
412370b324cSopenharmony_ci  _chars = newBuf;
413370b324cSopenharmony_ci  _limit = newLimit;
414370b324cSopenharmony_ci}
415370b324cSopenharmony_ci
416370b324cSopenharmony_civoid AString::ReAlloc2(unsigned newLimit)
417370b324cSopenharmony_ci{
418370b324cSopenharmony_ci  if (newLimit > k_Alloc_Len_Limit) throw 20130220;
419370b324cSopenharmony_ci  // MY_STRING_REALLOC(_chars, char, (size_t)newLimit + 1, 0);
420370b324cSopenharmony_ci  char *newBuf = MY_STRING_NEW_char((size_t)newLimit + 1);
421370b324cSopenharmony_ci  newBuf[0] = 0;
422370b324cSopenharmony_ci  MY_STRING_DELETE(_chars)
423370b324cSopenharmony_ci  _chars = newBuf;
424370b324cSopenharmony_ci  _limit = newLimit;
425370b324cSopenharmony_ci  _len = 0;
426370b324cSopenharmony_ci}
427370b324cSopenharmony_ci
428370b324cSopenharmony_civoid AString::SetStartLen(unsigned len)
429370b324cSopenharmony_ci{
430370b324cSopenharmony_ci  _chars = NULL;
431370b324cSopenharmony_ci  _chars = MY_STRING_NEW_char((size_t)len + 1);
432370b324cSopenharmony_ci  _len = len;
433370b324cSopenharmony_ci  _limit = len;
434370b324cSopenharmony_ci}
435370b324cSopenharmony_ci
436370b324cSopenharmony_civoid AString::Grow_1()
437370b324cSopenharmony_ci{
438370b324cSopenharmony_ci  unsigned next = _len;
439370b324cSopenharmony_ci  next += next / 2;
440370b324cSopenharmony_ci  next += 16;
441370b324cSopenharmony_ci  next &= ~(unsigned)15;
442370b324cSopenharmony_ci  next--;
443370b324cSopenharmony_ci  if (next < _len || next > k_Alloc_Len_Limit)
444370b324cSopenharmony_ci    next = k_Alloc_Len_Limit;
445370b324cSopenharmony_ci  if (next <= _len)
446370b324cSopenharmony_ci    throw 20130220;
447370b324cSopenharmony_ci  ReAlloc(next);
448370b324cSopenharmony_ci  // Grow(1);
449370b324cSopenharmony_ci}
450370b324cSopenharmony_ci
451370b324cSopenharmony_civoid AString::Grow(unsigned n)
452370b324cSopenharmony_ci{
453370b324cSopenharmony_ci  const unsigned freeSize = _limit - _len;
454370b324cSopenharmony_ci  if (n <= freeSize)
455370b324cSopenharmony_ci    return;
456370b324cSopenharmony_ci  unsigned next = _len + n;
457370b324cSopenharmony_ci  next += next / 2;
458370b324cSopenharmony_ci  next += 16;
459370b324cSopenharmony_ci  next &= ~(unsigned)15;
460370b324cSopenharmony_ci  next--;
461370b324cSopenharmony_ci  if (next < _len || next > k_Alloc_Len_Limit)
462370b324cSopenharmony_ci    next = k_Alloc_Len_Limit;
463370b324cSopenharmony_ci  if (next <= _len || next - _len < n)
464370b324cSopenharmony_ci    throw 20130220;
465370b324cSopenharmony_ci  ReAlloc(next);
466370b324cSopenharmony_ci}
467370b324cSopenharmony_ci
468370b324cSopenharmony_ciAString::AString(unsigned num, const char *s)
469370b324cSopenharmony_ci{
470370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
471370b324cSopenharmony_ci  if (num > len)
472370b324cSopenharmony_ci    num = len;
473370b324cSopenharmony_ci  SetStartLen(num);
474370b324cSopenharmony_ci  memcpy(_chars, s, num);
475370b324cSopenharmony_ci  _chars[num] = 0;
476370b324cSopenharmony_ci}
477370b324cSopenharmony_ci
478370b324cSopenharmony_ciAString::AString(unsigned num, const AString &s)
479370b324cSopenharmony_ci{
480370b324cSopenharmony_ci  if (num > s._len)
481370b324cSopenharmony_ci    num = s._len;
482370b324cSopenharmony_ci  SetStartLen(num);
483370b324cSopenharmony_ci  memcpy(_chars, s._chars, num);
484370b324cSopenharmony_ci  _chars[num] = 0;
485370b324cSopenharmony_ci}
486370b324cSopenharmony_ci
487370b324cSopenharmony_ciAString::AString(const AString &s, char c)
488370b324cSopenharmony_ci{
489370b324cSopenharmony_ci  SetStartLen(s.Len() + 1);
490370b324cSopenharmony_ci  char *chars = _chars;
491370b324cSopenharmony_ci  unsigned len = s.Len();
492370b324cSopenharmony_ci  memcpy(chars, s, len);
493370b324cSopenharmony_ci  chars[len] = c;
494370b324cSopenharmony_ci  chars[(size_t)len + 1] = 0;
495370b324cSopenharmony_ci}
496370b324cSopenharmony_ci
497370b324cSopenharmony_ciAString::AString(const char *s1, unsigned num1, const char *s2, unsigned num2)
498370b324cSopenharmony_ci{
499370b324cSopenharmony_ci  SetStartLen(num1 + num2);
500370b324cSopenharmony_ci  char *chars = _chars;
501370b324cSopenharmony_ci  memcpy(chars, s1, num1);
502370b324cSopenharmony_ci  memcpy(chars + num1, s2, num2 + 1);
503370b324cSopenharmony_ci}
504370b324cSopenharmony_ci
505370b324cSopenharmony_ciAString operator+(const AString &s1, const AString &s2) { return AString(s1, s1.Len(), s2, s2.Len()); }
506370b324cSopenharmony_ciAString operator+(const AString &s1, const char    *s2) { return AString(s1, s1.Len(), s2, MyStringLen(s2)); }
507370b324cSopenharmony_ciAString operator+(const char    *s1, const AString &s2) { return AString(s1, MyStringLen(s1), s2, s2.Len()); }
508370b324cSopenharmony_ci
509370b324cSopenharmony_cistatic const unsigned kStartStringCapacity = 4;
510370b324cSopenharmony_ci
511370b324cSopenharmony_ciAString::AString()
512370b324cSopenharmony_ci{
513370b324cSopenharmony_ci  _chars = NULL;
514370b324cSopenharmony_ci  _chars = MY_STRING_NEW_char(kStartStringCapacity);
515370b324cSopenharmony_ci  _len = 0;
516370b324cSopenharmony_ci  _limit = kStartStringCapacity - 1;
517370b324cSopenharmony_ci  _chars[0] = 0;
518370b324cSopenharmony_ci}
519370b324cSopenharmony_ci
520370b324cSopenharmony_ciAString::AString(char c)
521370b324cSopenharmony_ci{
522370b324cSopenharmony_ci  SetStartLen(1);
523370b324cSopenharmony_ci  char *chars = _chars;
524370b324cSopenharmony_ci  chars[0] = c;
525370b324cSopenharmony_ci  chars[1] = 0;
526370b324cSopenharmony_ci}
527370b324cSopenharmony_ci
528370b324cSopenharmony_ciAString::AString(const char *s)
529370b324cSopenharmony_ci{
530370b324cSopenharmony_ci  SetStartLen(MyStringLen(s));
531370b324cSopenharmony_ci  MyStringCopy(_chars, s);
532370b324cSopenharmony_ci}
533370b324cSopenharmony_ci
534370b324cSopenharmony_ciAString::AString(const AString &s)
535370b324cSopenharmony_ci{
536370b324cSopenharmony_ci  SetStartLen(s._len);
537370b324cSopenharmony_ci  MyStringCopy(_chars, s._chars);
538370b324cSopenharmony_ci}
539370b324cSopenharmony_ci
540370b324cSopenharmony_ciAString &AString::operator=(char c)
541370b324cSopenharmony_ci{
542370b324cSopenharmony_ci  if (1 > _limit)
543370b324cSopenharmony_ci  {
544370b324cSopenharmony_ci    char *newBuf = MY_STRING_NEW_char(1 + 1);
545370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
546370b324cSopenharmony_ci    _chars = newBuf;
547370b324cSopenharmony_ci    _limit = 1;
548370b324cSopenharmony_ci  }
549370b324cSopenharmony_ci  _len = 1;
550370b324cSopenharmony_ci  char *chars = _chars;
551370b324cSopenharmony_ci  chars[0] = c;
552370b324cSopenharmony_ci  chars[1] = 0;
553370b324cSopenharmony_ci  return *this;
554370b324cSopenharmony_ci}
555370b324cSopenharmony_ci
556370b324cSopenharmony_ciAString &AString::operator=(const char *s)
557370b324cSopenharmony_ci{
558370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
559370b324cSopenharmony_ci  if (len > _limit)
560370b324cSopenharmony_ci  {
561370b324cSopenharmony_ci    char *newBuf = MY_STRING_NEW_char((size_t)len + 1);
562370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
563370b324cSopenharmony_ci    _chars = newBuf;
564370b324cSopenharmony_ci    _limit = len;
565370b324cSopenharmony_ci  }
566370b324cSopenharmony_ci  _len = len;
567370b324cSopenharmony_ci  MyStringCopy(_chars, s);
568370b324cSopenharmony_ci  return *this;
569370b324cSopenharmony_ci}
570370b324cSopenharmony_ci
571370b324cSopenharmony_ciAString &AString::operator=(const AString &s)
572370b324cSopenharmony_ci{
573370b324cSopenharmony_ci  if (&s == this)
574370b324cSopenharmony_ci    return *this;
575370b324cSopenharmony_ci  unsigned len = s._len;
576370b324cSopenharmony_ci  if (len > _limit)
577370b324cSopenharmony_ci  {
578370b324cSopenharmony_ci    char *newBuf = MY_STRING_NEW_char((size_t)len + 1);
579370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
580370b324cSopenharmony_ci    _chars = newBuf;
581370b324cSopenharmony_ci    _limit = len;
582370b324cSopenharmony_ci  }
583370b324cSopenharmony_ci  _len = len;
584370b324cSopenharmony_ci  MyStringCopy(_chars, s._chars);
585370b324cSopenharmony_ci  return *this;
586370b324cSopenharmony_ci}
587370b324cSopenharmony_ci
588370b324cSopenharmony_civoid AString::SetFromWStr_if_Ascii(const wchar_t *s)
589370b324cSopenharmony_ci{
590370b324cSopenharmony_ci  unsigned len = 0;
591370b324cSopenharmony_ci  {
592370b324cSopenharmony_ci    for (;; len++)
593370b324cSopenharmony_ci    {
594370b324cSopenharmony_ci      wchar_t c = s[len];
595370b324cSopenharmony_ci      if (c == 0)
596370b324cSopenharmony_ci        break;
597370b324cSopenharmony_ci      if (c >= 0x80)
598370b324cSopenharmony_ci        return;
599370b324cSopenharmony_ci    }
600370b324cSopenharmony_ci  }
601370b324cSopenharmony_ci  if (len > _limit)
602370b324cSopenharmony_ci  {
603370b324cSopenharmony_ci    char *newBuf = MY_STRING_NEW_char((size_t)len + 1);
604370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
605370b324cSopenharmony_ci    _chars = newBuf;
606370b324cSopenharmony_ci    _limit = len;
607370b324cSopenharmony_ci  }
608370b324cSopenharmony_ci  _len = len;
609370b324cSopenharmony_ci  char *dest = _chars;
610370b324cSopenharmony_ci  unsigned i;
611370b324cSopenharmony_ci  for (i = 0; i < len; i++)
612370b324cSopenharmony_ci    dest[i] = (char)s[i];
613370b324cSopenharmony_ci  dest[i] = 0;
614370b324cSopenharmony_ci}
615370b324cSopenharmony_ci
616370b324cSopenharmony_ci/*
617370b324cSopenharmony_civoid AString::SetFromBstr_if_Ascii(BSTR s)
618370b324cSopenharmony_ci{
619370b324cSopenharmony_ci  unsigned len = ::SysStringLen(s);
620370b324cSopenharmony_ci  {
621370b324cSopenharmony_ci    for (unsigned i = 0; i < len; i++)
622370b324cSopenharmony_ci      if (s[i] <= 0 || s[i] >= 0x80)
623370b324cSopenharmony_ci        return;
624370b324cSopenharmony_ci  }
625370b324cSopenharmony_ci  if (len > _limit)
626370b324cSopenharmony_ci  {
627370b324cSopenharmony_ci    char *newBuf = MY_STRING_NEW_char((size_t)len + 1);
628370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
629370b324cSopenharmony_ci    _chars = newBuf;
630370b324cSopenharmony_ci    _limit = len;
631370b324cSopenharmony_ci  }
632370b324cSopenharmony_ci  _len = len;
633370b324cSopenharmony_ci  char *dest = _chars;
634370b324cSopenharmony_ci  unsigned i;
635370b324cSopenharmony_ci  for (i = 0; i < len; i++)
636370b324cSopenharmony_ci    dest[i] = (char)s[i];
637370b324cSopenharmony_ci  dest[i] = 0;
638370b324cSopenharmony_ci}
639370b324cSopenharmony_ci*/
640370b324cSopenharmony_ci
641370b324cSopenharmony_civoid AString::Add_Space() { operator+=(' '); }
642370b324cSopenharmony_civoid AString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); }
643370b324cSopenharmony_civoid AString::Add_LF() { operator+=('\n'); }
644370b324cSopenharmony_civoid AString::Add_Slash() { operator+=('/'); }
645370b324cSopenharmony_civoid AString::Add_Dot() { operator+=('.'); }
646370b324cSopenharmony_civoid AString::Add_Minus() { operator+=('-'); }
647370b324cSopenharmony_ci
648370b324cSopenharmony_ciAString &AString::operator+=(const char *s)
649370b324cSopenharmony_ci{
650370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
651370b324cSopenharmony_ci  Grow(len);
652370b324cSopenharmony_ci  MyStringCopy(_chars + _len, s);
653370b324cSopenharmony_ci  _len += len;
654370b324cSopenharmony_ci  return *this;
655370b324cSopenharmony_ci}
656370b324cSopenharmony_ci
657370b324cSopenharmony_civoid AString::Add_OptSpaced(const char *s)
658370b324cSopenharmony_ci{
659370b324cSopenharmony_ci  Add_Space_if_NotEmpty();
660370b324cSopenharmony_ci  (*this) += s;
661370b324cSopenharmony_ci}
662370b324cSopenharmony_ci
663370b324cSopenharmony_ciAString &AString::operator+=(const AString &s)
664370b324cSopenharmony_ci{
665370b324cSopenharmony_ci  Grow(s._len);
666370b324cSopenharmony_ci  MyStringCopy(_chars + _len, s._chars);
667370b324cSopenharmony_ci  _len += s._len;
668370b324cSopenharmony_ci  return *this;
669370b324cSopenharmony_ci}
670370b324cSopenharmony_ci
671370b324cSopenharmony_civoid AString::Add_UInt32(UInt32 v)
672370b324cSopenharmony_ci{
673370b324cSopenharmony_ci  Grow(10);
674370b324cSopenharmony_ci  _len = (unsigned)(ConvertUInt32ToString(v, _chars + _len) - _chars);
675370b324cSopenharmony_ci}
676370b324cSopenharmony_ci
677370b324cSopenharmony_civoid UString::Add_UInt64(UInt64 v)
678370b324cSopenharmony_ci{
679370b324cSopenharmony_ci  Grow(20);
680370b324cSopenharmony_ci  _len = (unsigned)(ConvertUInt64ToString(v, _chars + _len) - _chars);
681370b324cSopenharmony_ci}
682370b324cSopenharmony_ci
683370b324cSopenharmony_civoid AString::AddFrom(const char *s, unsigned len) // no check
684370b324cSopenharmony_ci{
685370b324cSopenharmony_ci  if (len != 0)
686370b324cSopenharmony_ci  {
687370b324cSopenharmony_ci    Grow(len);
688370b324cSopenharmony_ci    memcpy(_chars + _len, s, len);
689370b324cSopenharmony_ci    len += _len;
690370b324cSopenharmony_ci    _chars[len] = 0;
691370b324cSopenharmony_ci    _len = len;
692370b324cSopenharmony_ci  }
693370b324cSopenharmony_ci}
694370b324cSopenharmony_ci
695370b324cSopenharmony_civoid AString::SetFrom(const char *s, unsigned len) // no check
696370b324cSopenharmony_ci{
697370b324cSopenharmony_ci  if (len > _limit)
698370b324cSopenharmony_ci  {
699370b324cSopenharmony_ci    char *newBuf = MY_STRING_NEW_char((size_t)len + 1);
700370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
701370b324cSopenharmony_ci    _chars = newBuf;
702370b324cSopenharmony_ci    _limit = len;
703370b324cSopenharmony_ci  }
704370b324cSopenharmony_ci  if (len != 0)
705370b324cSopenharmony_ci    memcpy(_chars, s, len);
706370b324cSopenharmony_ci  _chars[len] = 0;
707370b324cSopenharmony_ci  _len = len;
708370b324cSopenharmony_ci}
709370b324cSopenharmony_ci
710370b324cSopenharmony_civoid AString::SetFrom_CalcLen(const char *s, unsigned len) // no check
711370b324cSopenharmony_ci{
712370b324cSopenharmony_ci  unsigned i;
713370b324cSopenharmony_ci  for (i = 0; i < len; i++)
714370b324cSopenharmony_ci    if (s[i] == 0)
715370b324cSopenharmony_ci      break;
716370b324cSopenharmony_ci  SetFrom(s, i);
717370b324cSopenharmony_ci}
718370b324cSopenharmony_ci
719370b324cSopenharmony_ciint AString::Find(const char *s, unsigned startIndex) const throw()
720370b324cSopenharmony_ci{
721370b324cSopenharmony_ci  const char *fs = strstr(_chars + startIndex, s);
722370b324cSopenharmony_ci  if (!fs)
723370b324cSopenharmony_ci    return -1;
724370b324cSopenharmony_ci  return (int)(fs - _chars);
725370b324cSopenharmony_ci
726370b324cSopenharmony_ci  /*
727370b324cSopenharmony_ci  if (s[0] == 0)
728370b324cSopenharmony_ci    return startIndex;
729370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
730370b324cSopenharmony_ci  const char *p = _chars + startIndex;
731370b324cSopenharmony_ci  for (;; p++)
732370b324cSopenharmony_ci  {
733370b324cSopenharmony_ci    const char c = *p;
734370b324cSopenharmony_ci    if (c != s[0])
735370b324cSopenharmony_ci    {
736370b324cSopenharmony_ci      if (c == 0)
737370b324cSopenharmony_ci        return -1;
738370b324cSopenharmony_ci      continue;
739370b324cSopenharmony_ci    }
740370b324cSopenharmony_ci    unsigned i;
741370b324cSopenharmony_ci    for (i = 1; i < len; i++)
742370b324cSopenharmony_ci      if (p[i] != s[i])
743370b324cSopenharmony_ci        break;
744370b324cSopenharmony_ci    if (i == len)
745370b324cSopenharmony_ci      return (int)(p - _chars);
746370b324cSopenharmony_ci  }
747370b324cSopenharmony_ci  */
748370b324cSopenharmony_ci}
749370b324cSopenharmony_ci
750370b324cSopenharmony_ciint AString::ReverseFind(char c) const throw()
751370b324cSopenharmony_ci{
752370b324cSopenharmony_ci  if (_len == 0)
753370b324cSopenharmony_ci    return -1;
754370b324cSopenharmony_ci  const char *p = _chars + _len - 1;
755370b324cSopenharmony_ci  for (;;)
756370b324cSopenharmony_ci  {
757370b324cSopenharmony_ci    if (*p == c)
758370b324cSopenharmony_ci      return (int)(p - _chars);
759370b324cSopenharmony_ci    if (p == _chars)
760370b324cSopenharmony_ci      return -1;
761370b324cSopenharmony_ci    p--; // p = GetPrevCharPointer(_chars, p);
762370b324cSopenharmony_ci  }
763370b324cSopenharmony_ci}
764370b324cSopenharmony_ci
765370b324cSopenharmony_ciint AString::ReverseFind_PathSepar() const throw()
766370b324cSopenharmony_ci{
767370b324cSopenharmony_ci  if (_len == 0)
768370b324cSopenharmony_ci    return -1;
769370b324cSopenharmony_ci  const char *p = _chars + _len - 1;
770370b324cSopenharmony_ci  for (;;)
771370b324cSopenharmony_ci  {
772370b324cSopenharmony_ci    const char c = *p;
773370b324cSopenharmony_ci    if (IS_PATH_SEPAR(c))
774370b324cSopenharmony_ci      return (int)(p - _chars);
775370b324cSopenharmony_ci    if (p == _chars)
776370b324cSopenharmony_ci      return -1;
777370b324cSopenharmony_ci    p--;
778370b324cSopenharmony_ci  }
779370b324cSopenharmony_ci}
780370b324cSopenharmony_ci
781370b324cSopenharmony_civoid AString::TrimLeft() throw()
782370b324cSopenharmony_ci{
783370b324cSopenharmony_ci  const char *p = _chars;
784370b324cSopenharmony_ci  for (;; p++)
785370b324cSopenharmony_ci  {
786370b324cSopenharmony_ci    char c = *p;
787370b324cSopenharmony_ci    if (c != ' ' && c != '\n' && c != '\t')
788370b324cSopenharmony_ci      break;
789370b324cSopenharmony_ci  }
790370b324cSopenharmony_ci  unsigned pos = (unsigned)(p - _chars);
791370b324cSopenharmony_ci  if (pos != 0)
792370b324cSopenharmony_ci  {
793370b324cSopenharmony_ci    MoveItems(0, pos);
794370b324cSopenharmony_ci    _len -= pos;
795370b324cSopenharmony_ci  }
796370b324cSopenharmony_ci}
797370b324cSopenharmony_ci
798370b324cSopenharmony_civoid AString::TrimRight() throw()
799370b324cSopenharmony_ci{
800370b324cSopenharmony_ci  const char *p = _chars;
801370b324cSopenharmony_ci  unsigned i;
802370b324cSopenharmony_ci  for (i = _len; i != 0; i--)
803370b324cSopenharmony_ci  {
804370b324cSopenharmony_ci    char c = p[(size_t)i - 1];
805370b324cSopenharmony_ci    if (c != ' ' && c != '\n' && c != '\t')
806370b324cSopenharmony_ci      break;
807370b324cSopenharmony_ci  }
808370b324cSopenharmony_ci  if (i != _len)
809370b324cSopenharmony_ci  {
810370b324cSopenharmony_ci    _chars[i] = 0;
811370b324cSopenharmony_ci    _len = i;
812370b324cSopenharmony_ci  }
813370b324cSopenharmony_ci}
814370b324cSopenharmony_ci
815370b324cSopenharmony_civoid AString::InsertAtFront(char c)
816370b324cSopenharmony_ci{
817370b324cSopenharmony_ci  if (_limit == _len)
818370b324cSopenharmony_ci    Grow_1();
819370b324cSopenharmony_ci  MoveItems(1, 0);
820370b324cSopenharmony_ci  _chars[0] = c;
821370b324cSopenharmony_ci  _len++;
822370b324cSopenharmony_ci}
823370b324cSopenharmony_ci
824370b324cSopenharmony_ci/*
825370b324cSopenharmony_civoid AString::Insert(unsigned index, char c)
826370b324cSopenharmony_ci{
827370b324cSopenharmony_ci  InsertSpace(index, 1);
828370b324cSopenharmony_ci  _chars[index] = c;
829370b324cSopenharmony_ci  _len++;
830370b324cSopenharmony_ci}
831370b324cSopenharmony_ci*/
832370b324cSopenharmony_ci
833370b324cSopenharmony_civoid AString::Insert(unsigned index, const char *s)
834370b324cSopenharmony_ci{
835370b324cSopenharmony_ci  unsigned num = MyStringLen(s);
836370b324cSopenharmony_ci  if (num != 0)
837370b324cSopenharmony_ci  {
838370b324cSopenharmony_ci    InsertSpace(index, num);
839370b324cSopenharmony_ci    memcpy(_chars + index, s, num);
840370b324cSopenharmony_ci    _len += num;
841370b324cSopenharmony_ci  }
842370b324cSopenharmony_ci}
843370b324cSopenharmony_ci
844370b324cSopenharmony_civoid AString::Insert(unsigned index, const AString &s)
845370b324cSopenharmony_ci{
846370b324cSopenharmony_ci  unsigned num = s.Len();
847370b324cSopenharmony_ci  if (num != 0)
848370b324cSopenharmony_ci  {
849370b324cSopenharmony_ci    InsertSpace(index, num);
850370b324cSopenharmony_ci    memcpy(_chars + index, s, num);
851370b324cSopenharmony_ci    _len += num;
852370b324cSopenharmony_ci  }
853370b324cSopenharmony_ci}
854370b324cSopenharmony_ci
855370b324cSopenharmony_civoid AString::RemoveChar(char ch) throw()
856370b324cSopenharmony_ci{
857370b324cSopenharmony_ci  char *src = _chars;
858370b324cSopenharmony_ci
859370b324cSopenharmony_ci  for (;;)
860370b324cSopenharmony_ci  {
861370b324cSopenharmony_ci    char c = *src++;
862370b324cSopenharmony_ci    if (c == 0)
863370b324cSopenharmony_ci      return;
864370b324cSopenharmony_ci    if (c == ch)
865370b324cSopenharmony_ci      break;
866370b324cSopenharmony_ci  }
867370b324cSopenharmony_ci
868370b324cSopenharmony_ci  char *dest = src - 1;
869370b324cSopenharmony_ci
870370b324cSopenharmony_ci  for (;;)
871370b324cSopenharmony_ci  {
872370b324cSopenharmony_ci    char c = *src++;
873370b324cSopenharmony_ci    if (c == 0)
874370b324cSopenharmony_ci      break;
875370b324cSopenharmony_ci    if (c != ch)
876370b324cSopenharmony_ci      *dest++ = c;
877370b324cSopenharmony_ci  }
878370b324cSopenharmony_ci
879370b324cSopenharmony_ci  *dest = 0;
880370b324cSopenharmony_ci  _len = (unsigned)(dest - _chars);
881370b324cSopenharmony_ci}
882370b324cSopenharmony_ci
883370b324cSopenharmony_ci// !!!!!!!!!!!!!!! test it if newChar = '\0'
884370b324cSopenharmony_civoid AString::Replace(char oldChar, char newChar) throw()
885370b324cSopenharmony_ci{
886370b324cSopenharmony_ci  if (oldChar == newChar)
887370b324cSopenharmony_ci    return; // 0;
888370b324cSopenharmony_ci  // unsigned number = 0;
889370b324cSopenharmony_ci  int pos = 0;
890370b324cSopenharmony_ci  char *chars = _chars;
891370b324cSopenharmony_ci  while ((unsigned)pos < _len)
892370b324cSopenharmony_ci  {
893370b324cSopenharmony_ci    pos = Find(oldChar, (unsigned)pos);
894370b324cSopenharmony_ci    if (pos < 0)
895370b324cSopenharmony_ci      break;
896370b324cSopenharmony_ci    chars[(unsigned)pos] = newChar;
897370b324cSopenharmony_ci    pos++;
898370b324cSopenharmony_ci    // number++;
899370b324cSopenharmony_ci  }
900370b324cSopenharmony_ci  return; //  number;
901370b324cSopenharmony_ci}
902370b324cSopenharmony_ci
903370b324cSopenharmony_civoid AString::Replace(const AString &oldString, const AString &newString)
904370b324cSopenharmony_ci{
905370b324cSopenharmony_ci  if (oldString.IsEmpty())
906370b324cSopenharmony_ci    return; // 0;
907370b324cSopenharmony_ci  if (oldString == newString)
908370b324cSopenharmony_ci    return; // 0;
909370b324cSopenharmony_ci  unsigned oldLen = oldString.Len();
910370b324cSopenharmony_ci  unsigned newLen = newString.Len();
911370b324cSopenharmony_ci  // unsigned number = 0;
912370b324cSopenharmony_ci  int pos = 0;
913370b324cSopenharmony_ci  while ((unsigned)pos < _len)
914370b324cSopenharmony_ci  {
915370b324cSopenharmony_ci    pos = Find(oldString, (unsigned)pos);
916370b324cSopenharmony_ci    if (pos < 0)
917370b324cSopenharmony_ci      break;
918370b324cSopenharmony_ci    Delete((unsigned)pos, oldLen);
919370b324cSopenharmony_ci    Insert((unsigned)pos, newString);
920370b324cSopenharmony_ci    pos += newLen;
921370b324cSopenharmony_ci    // number++;
922370b324cSopenharmony_ci  }
923370b324cSopenharmony_ci  // return number;
924370b324cSopenharmony_ci}
925370b324cSopenharmony_ci
926370b324cSopenharmony_civoid AString::Delete(unsigned index) throw()
927370b324cSopenharmony_ci{
928370b324cSopenharmony_ci  MoveItems(index, index + 1);
929370b324cSopenharmony_ci  _len--;
930370b324cSopenharmony_ci}
931370b324cSopenharmony_ci
932370b324cSopenharmony_civoid AString::Delete(unsigned index, unsigned count) throw()
933370b324cSopenharmony_ci{
934370b324cSopenharmony_ci  if (index + count > _len)
935370b324cSopenharmony_ci    count = _len - index;
936370b324cSopenharmony_ci  if (count > 0)
937370b324cSopenharmony_ci  {
938370b324cSopenharmony_ci    MoveItems(index, index + count);
939370b324cSopenharmony_ci    _len -= count;
940370b324cSopenharmony_ci  }
941370b324cSopenharmony_ci}
942370b324cSopenharmony_ci
943370b324cSopenharmony_civoid AString::DeleteFrontal(unsigned num) throw()
944370b324cSopenharmony_ci{
945370b324cSopenharmony_ci  if (num != 0)
946370b324cSopenharmony_ci  {
947370b324cSopenharmony_ci    MoveItems(0, num);
948370b324cSopenharmony_ci    _len -= num;
949370b324cSopenharmony_ci  }
950370b324cSopenharmony_ci}
951370b324cSopenharmony_ci
952370b324cSopenharmony_ci/*
953370b324cSopenharmony_ciAString operator+(const AString &s1, const AString &s2)
954370b324cSopenharmony_ci{
955370b324cSopenharmony_ci  AString result(s1);
956370b324cSopenharmony_ci  result += s2;
957370b324cSopenharmony_ci  return result;
958370b324cSopenharmony_ci}
959370b324cSopenharmony_ci
960370b324cSopenharmony_ciAString operator+(const AString &s, const char *chars)
961370b324cSopenharmony_ci{
962370b324cSopenharmony_ci  AString result(s);
963370b324cSopenharmony_ci  result += chars;
964370b324cSopenharmony_ci  return result;
965370b324cSopenharmony_ci}
966370b324cSopenharmony_ci
967370b324cSopenharmony_ciAString operator+(const char *chars, const AString &s)
968370b324cSopenharmony_ci{
969370b324cSopenharmony_ci  AString result(chars);
970370b324cSopenharmony_ci  result += s;
971370b324cSopenharmony_ci  return result;
972370b324cSopenharmony_ci}
973370b324cSopenharmony_ci
974370b324cSopenharmony_ciAString operator+(const AString &s, char c)
975370b324cSopenharmony_ci{
976370b324cSopenharmony_ci  AString result(s);
977370b324cSopenharmony_ci  result += c;
978370b324cSopenharmony_ci  return result;
979370b324cSopenharmony_ci}
980370b324cSopenharmony_ci*/
981370b324cSopenharmony_ci
982370b324cSopenharmony_ci/*
983370b324cSopenharmony_ciAString operator+(char c, const AString &s)
984370b324cSopenharmony_ci{
985370b324cSopenharmony_ci  AString result(c);
986370b324cSopenharmony_ci  result += s;
987370b324cSopenharmony_ci  return result;
988370b324cSopenharmony_ci}
989370b324cSopenharmony_ci*/
990370b324cSopenharmony_ci
991370b324cSopenharmony_ci
992370b324cSopenharmony_ci
993370b324cSopenharmony_ci
994370b324cSopenharmony_ci// ---------- UString ----------
995370b324cSopenharmony_ci
996370b324cSopenharmony_civoid UString::InsertSpace(unsigned index, unsigned size)
997370b324cSopenharmony_ci{
998370b324cSopenharmony_ci  Grow(size);
999370b324cSopenharmony_ci  MoveItems(index + size, index);
1000370b324cSopenharmony_ci}
1001370b324cSopenharmony_ci
1002370b324cSopenharmony_civoid UString::ReAlloc(unsigned newLimit)
1003370b324cSopenharmony_ci{
1004370b324cSopenharmony_ci  // MY_STRING_REALLOC(_chars, wchar_t, (size_t)newLimit + 1, (size_t)_len + 1);
1005370b324cSopenharmony_ci  wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1);
1006370b324cSopenharmony_ci  wmemcpy(newBuf, _chars, _len + 1);
1007370b324cSopenharmony_ci  MY_STRING_DELETE(_chars)
1008370b324cSopenharmony_ci  _chars = newBuf;
1009370b324cSopenharmony_ci  _limit = newLimit;
1010370b324cSopenharmony_ci}
1011370b324cSopenharmony_ci
1012370b324cSopenharmony_civoid UString::ReAlloc2(unsigned newLimit)
1013370b324cSopenharmony_ci{
1014370b324cSopenharmony_ci  if (newLimit > k_Alloc_Len_Limit) throw 20130221;
1015370b324cSopenharmony_ci  // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0);
1016370b324cSopenharmony_ci  wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)newLimit + 1);
1017370b324cSopenharmony_ci  newBuf[0] = 0;
1018370b324cSopenharmony_ci  MY_STRING_DELETE(_chars)
1019370b324cSopenharmony_ci  _chars = newBuf;
1020370b324cSopenharmony_ci  _limit = newLimit;
1021370b324cSopenharmony_ci  _len = 0;
1022370b324cSopenharmony_ci}
1023370b324cSopenharmony_ci
1024370b324cSopenharmony_civoid UString::SetStartLen(unsigned len)
1025370b324cSopenharmony_ci{
1026370b324cSopenharmony_ci  _chars = NULL;
1027370b324cSopenharmony_ci  _chars = MY_STRING_NEW_wchar_t((size_t)len + 1);
1028370b324cSopenharmony_ci  _len = len;
1029370b324cSopenharmony_ci  _limit = len;
1030370b324cSopenharmony_ci}
1031370b324cSopenharmony_ci
1032370b324cSopenharmony_civoid UString::Grow_1()
1033370b324cSopenharmony_ci{
1034370b324cSopenharmony_ci  unsigned next = _len;
1035370b324cSopenharmony_ci  next += next / 2;
1036370b324cSopenharmony_ci  next += 16;
1037370b324cSopenharmony_ci  next &= ~(unsigned)15;
1038370b324cSopenharmony_ci  next--;
1039370b324cSopenharmony_ci  if (next < _len || next > k_Alloc_Len_Limit)
1040370b324cSopenharmony_ci    next = k_Alloc_Len_Limit;
1041370b324cSopenharmony_ci  if (next <= _len)
1042370b324cSopenharmony_ci    throw 20130220;
1043370b324cSopenharmony_ci  ReAlloc(next);
1044370b324cSopenharmony_ci}
1045370b324cSopenharmony_ci
1046370b324cSopenharmony_civoid UString::Grow(unsigned n)
1047370b324cSopenharmony_ci{
1048370b324cSopenharmony_ci  const unsigned freeSize = _limit - _len;
1049370b324cSopenharmony_ci  if (n <= freeSize)
1050370b324cSopenharmony_ci    return;
1051370b324cSopenharmony_ci  unsigned next = _len + n;
1052370b324cSopenharmony_ci  next += next / 2;
1053370b324cSopenharmony_ci  next += 16;
1054370b324cSopenharmony_ci  next &= ~(unsigned)15;
1055370b324cSopenharmony_ci  next--;
1056370b324cSopenharmony_ci  if (next < _len || next > k_Alloc_Len_Limit)
1057370b324cSopenharmony_ci    next = k_Alloc_Len_Limit;
1058370b324cSopenharmony_ci  if (next <= _len || next - _len < n)
1059370b324cSopenharmony_ci    throw 20130220;
1060370b324cSopenharmony_ci  ReAlloc(next - 1);
1061370b324cSopenharmony_ci}
1062370b324cSopenharmony_ci
1063370b324cSopenharmony_ci
1064370b324cSopenharmony_ciUString::UString(unsigned num, const wchar_t *s)
1065370b324cSopenharmony_ci{
1066370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1067370b324cSopenharmony_ci  if (num > len)
1068370b324cSopenharmony_ci    num = len;
1069370b324cSopenharmony_ci  SetStartLen(num);
1070370b324cSopenharmony_ci  wmemcpy(_chars, s, num);
1071370b324cSopenharmony_ci  _chars[num] = 0;
1072370b324cSopenharmony_ci}
1073370b324cSopenharmony_ci
1074370b324cSopenharmony_ci
1075370b324cSopenharmony_ciUString::UString(unsigned num, const UString &s)
1076370b324cSopenharmony_ci{
1077370b324cSopenharmony_ci  if (num > s._len)
1078370b324cSopenharmony_ci    num = s._len;
1079370b324cSopenharmony_ci  SetStartLen(num);
1080370b324cSopenharmony_ci  wmemcpy(_chars, s._chars, num);
1081370b324cSopenharmony_ci  _chars[num] = 0;
1082370b324cSopenharmony_ci}
1083370b324cSopenharmony_ci
1084370b324cSopenharmony_ciUString::UString(const UString &s, wchar_t c)
1085370b324cSopenharmony_ci{
1086370b324cSopenharmony_ci  SetStartLen(s.Len() + 1);
1087370b324cSopenharmony_ci  wchar_t *chars = _chars;
1088370b324cSopenharmony_ci  unsigned len = s.Len();
1089370b324cSopenharmony_ci  wmemcpy(chars, s, len);
1090370b324cSopenharmony_ci  chars[len] = c;
1091370b324cSopenharmony_ci  chars[(size_t)len + 1] = 0;
1092370b324cSopenharmony_ci}
1093370b324cSopenharmony_ci
1094370b324cSopenharmony_ciUString::UString(const wchar_t *s1, unsigned num1, const wchar_t *s2, unsigned num2)
1095370b324cSopenharmony_ci{
1096370b324cSopenharmony_ci  SetStartLen(num1 + num2);
1097370b324cSopenharmony_ci  wchar_t *chars = _chars;
1098370b324cSopenharmony_ci  wmemcpy(chars, s1, num1);
1099370b324cSopenharmony_ci  wmemcpy(chars + num1, s2, num2 + 1);
1100370b324cSopenharmony_ci}
1101370b324cSopenharmony_ci
1102370b324cSopenharmony_ciUString operator+(const UString &s1, const UString &s2) { return UString(s1, s1.Len(), s2, s2.Len()); }
1103370b324cSopenharmony_ciUString operator+(const UString &s1, const wchar_t *s2) { return UString(s1, s1.Len(), s2, MyStringLen(s2)); }
1104370b324cSopenharmony_ciUString operator+(const wchar_t *s1, const UString &s2) { return UString(s1, MyStringLen(s1), s2, s2.Len()); }
1105370b324cSopenharmony_ci
1106370b324cSopenharmony_ciUString::UString()
1107370b324cSopenharmony_ci{
1108370b324cSopenharmony_ci  _chars = NULL;
1109370b324cSopenharmony_ci  _chars = MY_STRING_NEW_wchar_t(kStartStringCapacity);
1110370b324cSopenharmony_ci  _len = 0;
1111370b324cSopenharmony_ci  _limit = kStartStringCapacity - 1;
1112370b324cSopenharmony_ci  _chars[0] = 0;
1113370b324cSopenharmony_ci}
1114370b324cSopenharmony_ci
1115370b324cSopenharmony_ciUString::UString(wchar_t c)
1116370b324cSopenharmony_ci{
1117370b324cSopenharmony_ci  SetStartLen(1);
1118370b324cSopenharmony_ci  wchar_t *chars = _chars;
1119370b324cSopenharmony_ci  chars[0] = c;
1120370b324cSopenharmony_ci  chars[1] = 0;
1121370b324cSopenharmony_ci}
1122370b324cSopenharmony_ci
1123370b324cSopenharmony_ciUString::UString(char c)
1124370b324cSopenharmony_ci{
1125370b324cSopenharmony_ci  SetStartLen(1);
1126370b324cSopenharmony_ci  wchar_t *chars = _chars;
1127370b324cSopenharmony_ci  chars[0] = (unsigned char)c;
1128370b324cSopenharmony_ci  chars[1] = 0;
1129370b324cSopenharmony_ci}
1130370b324cSopenharmony_ci
1131370b324cSopenharmony_ciUString::UString(const wchar_t *s)
1132370b324cSopenharmony_ci{
1133370b324cSopenharmony_ci  const unsigned len = MyStringLen(s);
1134370b324cSopenharmony_ci  SetStartLen(len);
1135370b324cSopenharmony_ci  wmemcpy(_chars, s, len + 1);
1136370b324cSopenharmony_ci}
1137370b324cSopenharmony_ci
1138370b324cSopenharmony_ciUString::UString(const char *s)
1139370b324cSopenharmony_ci{
1140370b324cSopenharmony_ci  const unsigned len = MyStringLen(s);
1141370b324cSopenharmony_ci  SetStartLen(len);
1142370b324cSopenharmony_ci  wchar_t *chars = _chars;
1143370b324cSopenharmony_ci  for (unsigned i = 0; i < len; i++)
1144370b324cSopenharmony_ci    chars[i] = (unsigned char)s[i];
1145370b324cSopenharmony_ci  chars[len] = 0;
1146370b324cSopenharmony_ci}
1147370b324cSopenharmony_ci
1148370b324cSopenharmony_ciUString::UString(const AString &s)
1149370b324cSopenharmony_ci{
1150370b324cSopenharmony_ci  const unsigned len = s.Len();
1151370b324cSopenharmony_ci  SetStartLen(len);
1152370b324cSopenharmony_ci  wchar_t *chars = _chars;
1153370b324cSopenharmony_ci  const char *s2 = s.Ptr();
1154370b324cSopenharmony_ci  for (unsigned i = 0; i < len; i++)
1155370b324cSopenharmony_ci    chars[i] = (unsigned char)s2[i];
1156370b324cSopenharmony_ci  chars[len] = 0;
1157370b324cSopenharmony_ci}
1158370b324cSopenharmony_ci
1159370b324cSopenharmony_ciUString::UString(const UString &s)
1160370b324cSopenharmony_ci{
1161370b324cSopenharmony_ci  SetStartLen(s._len);
1162370b324cSopenharmony_ci  wmemcpy(_chars, s._chars, s._len + 1);
1163370b324cSopenharmony_ci}
1164370b324cSopenharmony_ci
1165370b324cSopenharmony_ciUString &UString::operator=(wchar_t c)
1166370b324cSopenharmony_ci{
1167370b324cSopenharmony_ci  if (1 > _limit)
1168370b324cSopenharmony_ci  {
1169370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t(1 + 1);
1170370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
1171370b324cSopenharmony_ci    _chars = newBuf;
1172370b324cSopenharmony_ci    _limit = 1;
1173370b324cSopenharmony_ci  }
1174370b324cSopenharmony_ci  _len = 1;
1175370b324cSopenharmony_ci  wchar_t *chars = _chars;
1176370b324cSopenharmony_ci  chars[0] = c;
1177370b324cSopenharmony_ci  chars[1] = 0;
1178370b324cSopenharmony_ci  return *this;
1179370b324cSopenharmony_ci}
1180370b324cSopenharmony_ci
1181370b324cSopenharmony_ciUString &UString::operator=(const wchar_t *s)
1182370b324cSopenharmony_ci{
1183370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1184370b324cSopenharmony_ci  if (len > _limit)
1185370b324cSopenharmony_ci  {
1186370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1187370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
1188370b324cSopenharmony_ci    _chars = newBuf;
1189370b324cSopenharmony_ci    _limit = len;
1190370b324cSopenharmony_ci  }
1191370b324cSopenharmony_ci  _len = len;
1192370b324cSopenharmony_ci  wmemcpy(_chars, s, len + 1);
1193370b324cSopenharmony_ci  return *this;
1194370b324cSopenharmony_ci}
1195370b324cSopenharmony_ci
1196370b324cSopenharmony_ciUString &UString::operator=(const UString &s)
1197370b324cSopenharmony_ci{
1198370b324cSopenharmony_ci  if (&s == this)
1199370b324cSopenharmony_ci    return *this;
1200370b324cSopenharmony_ci  unsigned len = s._len;
1201370b324cSopenharmony_ci  if (len > _limit)
1202370b324cSopenharmony_ci  {
1203370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1204370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
1205370b324cSopenharmony_ci    _chars = newBuf;
1206370b324cSopenharmony_ci    _limit = len;
1207370b324cSopenharmony_ci  }
1208370b324cSopenharmony_ci  _len = len;
1209370b324cSopenharmony_ci  wmemcpy(_chars, s._chars, len + 1);
1210370b324cSopenharmony_ci  return *this;
1211370b324cSopenharmony_ci}
1212370b324cSopenharmony_ci
1213370b324cSopenharmony_civoid UString::SetFrom(const wchar_t *s, unsigned len) // no check
1214370b324cSopenharmony_ci{
1215370b324cSopenharmony_ci  if (len > _limit)
1216370b324cSopenharmony_ci  {
1217370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1218370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
1219370b324cSopenharmony_ci    _chars = newBuf;
1220370b324cSopenharmony_ci    _limit = len;
1221370b324cSopenharmony_ci  }
1222370b324cSopenharmony_ci  if (len != 0)
1223370b324cSopenharmony_ci    wmemcpy(_chars, s, len);
1224370b324cSopenharmony_ci  _chars[len] = 0;
1225370b324cSopenharmony_ci  _len = len;
1226370b324cSopenharmony_ci}
1227370b324cSopenharmony_ci
1228370b324cSopenharmony_civoid UString::SetFromBstr(LPCOLESTR s)
1229370b324cSopenharmony_ci{
1230370b324cSopenharmony_ci  unsigned len = ::SysStringLen((BSTR)(void *)(s));
1231370b324cSopenharmony_ci
1232370b324cSopenharmony_ci  /*
1233370b324cSopenharmony_ci  #if WCHAR_MAX > 0xffff
1234370b324cSopenharmony_ci  size_t num_wchars = 0;
1235370b324cSopenharmony_ci  for (size_t i = 0; i < len;)
1236370b324cSopenharmony_ci  {
1237370b324cSopenharmony_ci    wchar_t c = s[i++];
1238370b324cSopenharmony_ci    if (c >= 0xd800 && c < 0xdc00 && i + 1 != len)
1239370b324cSopenharmony_ci    {
1240370b324cSopenharmony_ci      wchar_t c2 = s[i];
1241370b324cSopenharmony_ci      if (c2 >= 0xdc00 && c2 < 0x10000)
1242370b324cSopenharmony_ci      {
1243370b324cSopenharmony_ci        c = 0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff);
1244370b324cSopenharmony_ci        i++;
1245370b324cSopenharmony_ci      }
1246370b324cSopenharmony_ci    }
1247370b324cSopenharmony_ci    num_wchars++;
1248370b324cSopenharmony_ci  }
1249370b324cSopenharmony_ci  len = num_wchars;
1250370b324cSopenharmony_ci  #endif
1251370b324cSopenharmony_ci  */
1252370b324cSopenharmony_ci
1253370b324cSopenharmony_ci  if (len > _limit)
1254370b324cSopenharmony_ci  {
1255370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1256370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
1257370b324cSopenharmony_ci    _chars = newBuf;
1258370b324cSopenharmony_ci    _limit = len;
1259370b324cSopenharmony_ci  }
1260370b324cSopenharmony_ci  _len = len;
1261370b324cSopenharmony_ci
1262370b324cSopenharmony_ci  /*
1263370b324cSopenharmony_ci  #if WCHAR_MAX > 0xffff
1264370b324cSopenharmony_ci
1265370b324cSopenharmony_ci  wchar_t *chars = _chars;
1266370b324cSopenharmony_ci  for (size_t i = 0; i <= len; i++)
1267370b324cSopenharmony_ci  {
1268370b324cSopenharmony_ci    wchar_t c = *s++;
1269370b324cSopenharmony_ci    if (c >= 0xd800 && c < 0xdc00 && i + 1 != len)
1270370b324cSopenharmony_ci    {
1271370b324cSopenharmony_ci      wchar_t c2 = *s;
1272370b324cSopenharmony_ci      if (c2 >= 0xdc00 && c2 < 0x10000)
1273370b324cSopenharmony_ci      {
1274370b324cSopenharmony_ci        s++;
1275370b324cSopenharmony_ci        c = 0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff);
1276370b324cSopenharmony_ci      }
1277370b324cSopenharmony_ci    }
1278370b324cSopenharmony_ci    chars[i] = c;
1279370b324cSopenharmony_ci  }
1280370b324cSopenharmony_ci
1281370b324cSopenharmony_ci  #else
1282370b324cSopenharmony_ci  */
1283370b324cSopenharmony_ci
1284370b324cSopenharmony_ci  // if (s)
1285370b324cSopenharmony_ci    wmemcpy(_chars, s, len + 1);
1286370b324cSopenharmony_ci
1287370b324cSopenharmony_ci  // #endif
1288370b324cSopenharmony_ci}
1289370b324cSopenharmony_ci
1290370b324cSopenharmony_ciUString &UString::operator=(const char *s)
1291370b324cSopenharmony_ci{
1292370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1293370b324cSopenharmony_ci  if (len > _limit)
1294370b324cSopenharmony_ci  {
1295370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1296370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
1297370b324cSopenharmony_ci    _chars = newBuf;
1298370b324cSopenharmony_ci    _limit = len;
1299370b324cSopenharmony_ci  }
1300370b324cSopenharmony_ci  wchar_t *chars = _chars;
1301370b324cSopenharmony_ci  for (unsigned i = 0; i < len; i++)
1302370b324cSopenharmony_ci    chars[i] = (unsigned char)s[i];
1303370b324cSopenharmony_ci  chars[len] = 0;
1304370b324cSopenharmony_ci  _len = len;
1305370b324cSopenharmony_ci  return *this;
1306370b324cSopenharmony_ci}
1307370b324cSopenharmony_ci
1308370b324cSopenharmony_civoid UString::Add_Dot() { operator+=(L'.'); }
1309370b324cSopenharmony_civoid UString::Add_Space() { operator+=(L' '); }
1310370b324cSopenharmony_civoid UString::Add_Space_if_NotEmpty() { if (!IsEmpty()) Add_Space(); }
1311370b324cSopenharmony_ci
1312370b324cSopenharmony_civoid UString::Add_LF()
1313370b324cSopenharmony_ci{
1314370b324cSopenharmony_ci  if (_limit == _len)
1315370b324cSopenharmony_ci    Grow_1();
1316370b324cSopenharmony_ci  unsigned len = _len;
1317370b324cSopenharmony_ci  wchar_t *chars = _chars;
1318370b324cSopenharmony_ci  chars[len++] = L'\n';
1319370b324cSopenharmony_ci  chars[len] = 0;
1320370b324cSopenharmony_ci  _len = len;
1321370b324cSopenharmony_ci}
1322370b324cSopenharmony_ci
1323370b324cSopenharmony_ciUString &UString::operator+=(const wchar_t *s)
1324370b324cSopenharmony_ci{
1325370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1326370b324cSopenharmony_ci  Grow(len);
1327370b324cSopenharmony_ci  wmemcpy(_chars + _len, s, len + 1);
1328370b324cSopenharmony_ci  _len += len;
1329370b324cSopenharmony_ci  return *this;
1330370b324cSopenharmony_ci}
1331370b324cSopenharmony_ci
1332370b324cSopenharmony_ciUString &UString::operator+=(const UString &s)
1333370b324cSopenharmony_ci{
1334370b324cSopenharmony_ci  Grow(s._len);
1335370b324cSopenharmony_ci  wmemcpy(_chars + _len, s._chars, s._len + 1);
1336370b324cSopenharmony_ci  _len += s._len;
1337370b324cSopenharmony_ci  return *this;
1338370b324cSopenharmony_ci}
1339370b324cSopenharmony_ci
1340370b324cSopenharmony_ciUString &UString::operator+=(const char *s)
1341370b324cSopenharmony_ci{
1342370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1343370b324cSopenharmony_ci  Grow(len);
1344370b324cSopenharmony_ci  wchar_t *chars = _chars + _len;
1345370b324cSopenharmony_ci  for (unsigned i = 0; i < len; i++)
1346370b324cSopenharmony_ci    chars[i] = (unsigned char)s[i];
1347370b324cSopenharmony_ci  chars[len] = 0;
1348370b324cSopenharmony_ci  _len += len;
1349370b324cSopenharmony_ci  return *this;
1350370b324cSopenharmony_ci}
1351370b324cSopenharmony_ci
1352370b324cSopenharmony_ci
1353370b324cSopenharmony_civoid UString::Add_UInt32(UInt32 v)
1354370b324cSopenharmony_ci{
1355370b324cSopenharmony_ci  Grow(10);
1356370b324cSopenharmony_ci  _len = (unsigned)(ConvertUInt32ToString(v, _chars + _len) - _chars);
1357370b324cSopenharmony_ci}
1358370b324cSopenharmony_ci
1359370b324cSopenharmony_civoid AString::Add_UInt64(UInt64 v)
1360370b324cSopenharmony_ci{
1361370b324cSopenharmony_ci  Grow(20);
1362370b324cSopenharmony_ci  _len = (unsigned)(ConvertUInt64ToString(v, _chars + _len) - _chars);
1363370b324cSopenharmony_ci}
1364370b324cSopenharmony_ci
1365370b324cSopenharmony_ci
1366370b324cSopenharmony_ciint UString::Find(const wchar_t *s, unsigned startIndex) const throw()
1367370b324cSopenharmony_ci{
1368370b324cSopenharmony_ci  const wchar_t *fs = wcsstr(_chars + startIndex, s);
1369370b324cSopenharmony_ci  if (!fs)
1370370b324cSopenharmony_ci    return -1;
1371370b324cSopenharmony_ci  return (int)(fs - _chars);
1372370b324cSopenharmony_ci
1373370b324cSopenharmony_ci  /*
1374370b324cSopenharmony_ci  if (s[0] == 0)
1375370b324cSopenharmony_ci    return startIndex;
1376370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1377370b324cSopenharmony_ci  const wchar_t *p = _chars + startIndex;
1378370b324cSopenharmony_ci  for (;; p++)
1379370b324cSopenharmony_ci  {
1380370b324cSopenharmony_ci    const wchar_t c = *p;
1381370b324cSopenharmony_ci    if (c != s[0])
1382370b324cSopenharmony_ci    {
1383370b324cSopenharmony_ci      if (c == 0)
1384370b324cSopenharmony_ci        return -1;
1385370b324cSopenharmony_ci      continue;
1386370b324cSopenharmony_ci    }
1387370b324cSopenharmony_ci    unsigned i;
1388370b324cSopenharmony_ci    for (i = 1; i < len; i++)
1389370b324cSopenharmony_ci      if (p[i] != s[i])
1390370b324cSopenharmony_ci        break;
1391370b324cSopenharmony_ci    if (i == len)
1392370b324cSopenharmony_ci      return (int)(p - _chars);
1393370b324cSopenharmony_ci  }
1394370b324cSopenharmony_ci  */
1395370b324cSopenharmony_ci}
1396370b324cSopenharmony_ci
1397370b324cSopenharmony_ciint UString::ReverseFind(wchar_t c) const throw()
1398370b324cSopenharmony_ci{
1399370b324cSopenharmony_ci  if (_len == 0)
1400370b324cSopenharmony_ci    return -1;
1401370b324cSopenharmony_ci  const wchar_t *p = _chars + _len;
1402370b324cSopenharmony_ci  do
1403370b324cSopenharmony_ci  {
1404370b324cSopenharmony_ci    if (*(--p) == c)
1405370b324cSopenharmony_ci      return (int)(p - _chars);
1406370b324cSopenharmony_ci  }
1407370b324cSopenharmony_ci  while (p != _chars);
1408370b324cSopenharmony_ci  return -1;
1409370b324cSopenharmony_ci}
1410370b324cSopenharmony_ci
1411370b324cSopenharmony_ciint UString::ReverseFind_PathSepar() const throw()
1412370b324cSopenharmony_ci{
1413370b324cSopenharmony_ci  const wchar_t *p = _chars + _len;
1414370b324cSopenharmony_ci  while (p != _chars)
1415370b324cSopenharmony_ci  {
1416370b324cSopenharmony_ci    const wchar_t c = *(--p);
1417370b324cSopenharmony_ci    if (IS_PATH_SEPAR(c))
1418370b324cSopenharmony_ci      return (int)(p - _chars);
1419370b324cSopenharmony_ci  }
1420370b324cSopenharmony_ci  return -1;
1421370b324cSopenharmony_ci}
1422370b324cSopenharmony_ci
1423370b324cSopenharmony_civoid UString::TrimLeft() throw()
1424370b324cSopenharmony_ci{
1425370b324cSopenharmony_ci  const wchar_t *p = _chars;
1426370b324cSopenharmony_ci  for (;; p++)
1427370b324cSopenharmony_ci  {
1428370b324cSopenharmony_ci    wchar_t c = *p;
1429370b324cSopenharmony_ci    if (c != ' ' && c != '\n' && c != '\t')
1430370b324cSopenharmony_ci      break;
1431370b324cSopenharmony_ci  }
1432370b324cSopenharmony_ci  unsigned pos = (unsigned)(p - _chars);
1433370b324cSopenharmony_ci  if (pos != 0)
1434370b324cSopenharmony_ci  {
1435370b324cSopenharmony_ci    MoveItems(0, pos);
1436370b324cSopenharmony_ci    _len -= pos;
1437370b324cSopenharmony_ci  }
1438370b324cSopenharmony_ci}
1439370b324cSopenharmony_ci
1440370b324cSopenharmony_civoid UString::TrimRight() throw()
1441370b324cSopenharmony_ci{
1442370b324cSopenharmony_ci  const wchar_t *p = _chars;
1443370b324cSopenharmony_ci  unsigned i;
1444370b324cSopenharmony_ci  for (i = _len; i != 0; i--)
1445370b324cSopenharmony_ci  {
1446370b324cSopenharmony_ci    wchar_t c = p[(size_t)i - 1];
1447370b324cSopenharmony_ci    if (c != ' ' && c != '\n' && c != '\t')
1448370b324cSopenharmony_ci      break;
1449370b324cSopenharmony_ci  }
1450370b324cSopenharmony_ci  if (i != _len)
1451370b324cSopenharmony_ci  {
1452370b324cSopenharmony_ci    _chars[i] = 0;
1453370b324cSopenharmony_ci    _len = i;
1454370b324cSopenharmony_ci  }
1455370b324cSopenharmony_ci}
1456370b324cSopenharmony_ci
1457370b324cSopenharmony_civoid UString::InsertAtFront(wchar_t c)
1458370b324cSopenharmony_ci{
1459370b324cSopenharmony_ci  if (_limit == _len)
1460370b324cSopenharmony_ci    Grow_1();
1461370b324cSopenharmony_ci  MoveItems(1, 0);
1462370b324cSopenharmony_ci  _chars[0] = c;
1463370b324cSopenharmony_ci  _len++;
1464370b324cSopenharmony_ci}
1465370b324cSopenharmony_ci
1466370b324cSopenharmony_ci/*
1467370b324cSopenharmony_civoid UString::Insert_wchar_t(unsigned index, wchar_t c)
1468370b324cSopenharmony_ci{
1469370b324cSopenharmony_ci  InsertSpace(index, 1);
1470370b324cSopenharmony_ci  _chars[index] = c;
1471370b324cSopenharmony_ci  _len++;
1472370b324cSopenharmony_ci}
1473370b324cSopenharmony_ci*/
1474370b324cSopenharmony_ci
1475370b324cSopenharmony_civoid UString::Insert(unsigned index, const wchar_t *s)
1476370b324cSopenharmony_ci{
1477370b324cSopenharmony_ci  unsigned num = MyStringLen(s);
1478370b324cSopenharmony_ci  if (num != 0)
1479370b324cSopenharmony_ci  {
1480370b324cSopenharmony_ci    InsertSpace(index, num);
1481370b324cSopenharmony_ci    wmemcpy(_chars + index, s, num);
1482370b324cSopenharmony_ci    _len += num;
1483370b324cSopenharmony_ci  }
1484370b324cSopenharmony_ci}
1485370b324cSopenharmony_ci
1486370b324cSopenharmony_civoid UString::Insert(unsigned index, const UString &s)
1487370b324cSopenharmony_ci{
1488370b324cSopenharmony_ci  unsigned num = s.Len();
1489370b324cSopenharmony_ci  if (num != 0)
1490370b324cSopenharmony_ci  {
1491370b324cSopenharmony_ci    InsertSpace(index, num);
1492370b324cSopenharmony_ci    wmemcpy(_chars + index, s, num);
1493370b324cSopenharmony_ci    _len += num;
1494370b324cSopenharmony_ci  }
1495370b324cSopenharmony_ci}
1496370b324cSopenharmony_ci
1497370b324cSopenharmony_civoid UString::RemoveChar(wchar_t ch) throw()
1498370b324cSopenharmony_ci{
1499370b324cSopenharmony_ci  wchar_t *src = _chars;
1500370b324cSopenharmony_ci
1501370b324cSopenharmony_ci  for (;;)
1502370b324cSopenharmony_ci  {
1503370b324cSopenharmony_ci    wchar_t c = *src++;
1504370b324cSopenharmony_ci    if (c == 0)
1505370b324cSopenharmony_ci      return;
1506370b324cSopenharmony_ci    if (c == ch)
1507370b324cSopenharmony_ci      break;
1508370b324cSopenharmony_ci  }
1509370b324cSopenharmony_ci
1510370b324cSopenharmony_ci  wchar_t *dest = src - 1;
1511370b324cSopenharmony_ci
1512370b324cSopenharmony_ci  for (;;)
1513370b324cSopenharmony_ci  {
1514370b324cSopenharmony_ci    wchar_t c = *src++;
1515370b324cSopenharmony_ci    if (c == 0)
1516370b324cSopenharmony_ci      break;
1517370b324cSopenharmony_ci    if (c != ch)
1518370b324cSopenharmony_ci      *dest++ = c;
1519370b324cSopenharmony_ci  }
1520370b324cSopenharmony_ci
1521370b324cSopenharmony_ci  *dest = 0;
1522370b324cSopenharmony_ci  _len = (unsigned)(dest - _chars);
1523370b324cSopenharmony_ci}
1524370b324cSopenharmony_ci
1525370b324cSopenharmony_ci// !!!!!!!!!!!!!!! test it if newChar = '\0'
1526370b324cSopenharmony_civoid UString::Replace(wchar_t oldChar, wchar_t newChar) throw()
1527370b324cSopenharmony_ci{
1528370b324cSopenharmony_ci  if (oldChar == newChar)
1529370b324cSopenharmony_ci    return; // 0;
1530370b324cSopenharmony_ci  // unsigned number = 0;
1531370b324cSopenharmony_ci  int pos = 0;
1532370b324cSopenharmony_ci  wchar_t *chars = _chars;
1533370b324cSopenharmony_ci  while ((unsigned)pos < _len)
1534370b324cSopenharmony_ci  {
1535370b324cSopenharmony_ci    pos = Find(oldChar, (unsigned)pos);
1536370b324cSopenharmony_ci    if (pos < 0)
1537370b324cSopenharmony_ci      break;
1538370b324cSopenharmony_ci    chars[(unsigned)pos] = newChar;
1539370b324cSopenharmony_ci    pos++;
1540370b324cSopenharmony_ci    // number++;
1541370b324cSopenharmony_ci  }
1542370b324cSopenharmony_ci  return; //  number;
1543370b324cSopenharmony_ci}
1544370b324cSopenharmony_ci
1545370b324cSopenharmony_civoid UString::Replace(const UString &oldString, const UString &newString)
1546370b324cSopenharmony_ci{
1547370b324cSopenharmony_ci  if (oldString.IsEmpty())
1548370b324cSopenharmony_ci    return; // 0;
1549370b324cSopenharmony_ci  if (oldString == newString)
1550370b324cSopenharmony_ci    return; // 0;
1551370b324cSopenharmony_ci  unsigned oldLen = oldString.Len();
1552370b324cSopenharmony_ci  unsigned newLen = newString.Len();
1553370b324cSopenharmony_ci  // unsigned number = 0;
1554370b324cSopenharmony_ci  int pos = 0;
1555370b324cSopenharmony_ci  while ((unsigned)pos < _len)
1556370b324cSopenharmony_ci  {
1557370b324cSopenharmony_ci    pos = Find(oldString, (unsigned)pos);
1558370b324cSopenharmony_ci    if (pos < 0)
1559370b324cSopenharmony_ci      break;
1560370b324cSopenharmony_ci    Delete((unsigned)pos, oldLen);
1561370b324cSopenharmony_ci    Insert((unsigned)pos, newString);
1562370b324cSopenharmony_ci    pos += newLen;
1563370b324cSopenharmony_ci    // number++;
1564370b324cSopenharmony_ci  }
1565370b324cSopenharmony_ci  // return number;
1566370b324cSopenharmony_ci}
1567370b324cSopenharmony_ci
1568370b324cSopenharmony_civoid UString::Delete(unsigned index) throw()
1569370b324cSopenharmony_ci{
1570370b324cSopenharmony_ci  MoveItems(index, index + 1);
1571370b324cSopenharmony_ci  _len--;
1572370b324cSopenharmony_ci}
1573370b324cSopenharmony_ci
1574370b324cSopenharmony_civoid UString::Delete(unsigned index, unsigned count) throw()
1575370b324cSopenharmony_ci{
1576370b324cSopenharmony_ci  if (index + count > _len)
1577370b324cSopenharmony_ci    count = _len - index;
1578370b324cSopenharmony_ci  if (count > 0)
1579370b324cSopenharmony_ci  {
1580370b324cSopenharmony_ci    MoveItems(index, index + count);
1581370b324cSopenharmony_ci    _len -= count;
1582370b324cSopenharmony_ci  }
1583370b324cSopenharmony_ci}
1584370b324cSopenharmony_ci
1585370b324cSopenharmony_civoid UString::DeleteFrontal(unsigned num) throw()
1586370b324cSopenharmony_ci{
1587370b324cSopenharmony_ci  if (num != 0)
1588370b324cSopenharmony_ci  {
1589370b324cSopenharmony_ci    MoveItems(0, num);
1590370b324cSopenharmony_ci    _len -= num;
1591370b324cSopenharmony_ci  }
1592370b324cSopenharmony_ci}
1593370b324cSopenharmony_ci
1594370b324cSopenharmony_ci
1595370b324cSopenharmony_ci// ---------- UString2 ----------
1596370b324cSopenharmony_ci
1597370b324cSopenharmony_civoid UString2::ReAlloc2(unsigned newLimit)
1598370b324cSopenharmony_ci{
1599370b324cSopenharmony_ci  // wrong (_len) is allowed after this function
1600370b324cSopenharmony_ci  if (newLimit > k_Alloc_Len_Limit) throw 20130221;
1601370b324cSopenharmony_ci  // MY_STRING_REALLOC(_chars, wchar_t, newLimit + 1, 0);
1602370b324cSopenharmony_ci  if (_chars)
1603370b324cSopenharmony_ci  {
1604370b324cSopenharmony_ci    MY_STRING_DELETE(_chars)
1605370b324cSopenharmony_ci    _chars = NULL;
1606370b324cSopenharmony_ci    // _len = 0;
1607370b324cSopenharmony_ci  }
1608370b324cSopenharmony_ci  _chars = MY_STRING_NEW_wchar_t((size_t)newLimit + 1);
1609370b324cSopenharmony_ci  _chars[0] = 0;
1610370b324cSopenharmony_ci  // _len = newLimit;
1611370b324cSopenharmony_ci}
1612370b324cSopenharmony_ci
1613370b324cSopenharmony_civoid UString2::SetStartLen(unsigned len)
1614370b324cSopenharmony_ci{
1615370b324cSopenharmony_ci  _chars = NULL;
1616370b324cSopenharmony_ci  _chars = MY_STRING_NEW_wchar_t((size_t)len + 1);
1617370b324cSopenharmony_ci  _len = len;
1618370b324cSopenharmony_ci}
1619370b324cSopenharmony_ci
1620370b324cSopenharmony_ci
1621370b324cSopenharmony_ci/*
1622370b324cSopenharmony_ciUString2::UString2(wchar_t c)
1623370b324cSopenharmony_ci{
1624370b324cSopenharmony_ci  SetStartLen(1);
1625370b324cSopenharmony_ci  wchar_t *chars = _chars;
1626370b324cSopenharmony_ci  chars[0] = c;
1627370b324cSopenharmony_ci  chars[1] = 0;
1628370b324cSopenharmony_ci}
1629370b324cSopenharmony_ci*/
1630370b324cSopenharmony_ci
1631370b324cSopenharmony_ciUString2::UString2(const wchar_t *s)
1632370b324cSopenharmony_ci{
1633370b324cSopenharmony_ci  const unsigned len = MyStringLen(s);
1634370b324cSopenharmony_ci  SetStartLen(len);
1635370b324cSopenharmony_ci  wmemcpy(_chars, s, len + 1);
1636370b324cSopenharmony_ci}
1637370b324cSopenharmony_ci
1638370b324cSopenharmony_ciUString2::UString2(const UString2 &s): _chars(NULL), _len(0)
1639370b324cSopenharmony_ci{
1640370b324cSopenharmony_ci  if (s._chars)
1641370b324cSopenharmony_ci  {
1642370b324cSopenharmony_ci    SetStartLen(s._len);
1643370b324cSopenharmony_ci    wmemcpy(_chars, s._chars, s._len + 1);
1644370b324cSopenharmony_ci  }
1645370b324cSopenharmony_ci}
1646370b324cSopenharmony_ci
1647370b324cSopenharmony_ci/*
1648370b324cSopenharmony_ciUString2 &UString2::operator=(wchar_t c)
1649370b324cSopenharmony_ci{
1650370b324cSopenharmony_ci  if (1 > _len)
1651370b324cSopenharmony_ci  {
1652370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t(1 + 1);
1653370b324cSopenharmony_ci    if (_chars)
1654370b324cSopenharmony_ci      MY_STRING_DELETE(_chars)
1655370b324cSopenharmony_ci    _chars = newBuf;
1656370b324cSopenharmony_ci  }
1657370b324cSopenharmony_ci  _len = 1;
1658370b324cSopenharmony_ci  wchar_t *chars = _chars;
1659370b324cSopenharmony_ci  chars[0] = c;
1660370b324cSopenharmony_ci  chars[1] = 0;
1661370b324cSopenharmony_ci  return *this;
1662370b324cSopenharmony_ci}
1663370b324cSopenharmony_ci*/
1664370b324cSopenharmony_ci
1665370b324cSopenharmony_ciUString2 &UString2::operator=(const wchar_t *s)
1666370b324cSopenharmony_ci{
1667370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1668370b324cSopenharmony_ci  if (len > _len)
1669370b324cSopenharmony_ci  {
1670370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1671370b324cSopenharmony_ci    if (_chars)
1672370b324cSopenharmony_ci      MY_STRING_DELETE(_chars)
1673370b324cSopenharmony_ci    _chars = newBuf;
1674370b324cSopenharmony_ci  }
1675370b324cSopenharmony_ci  _len = len;
1676370b324cSopenharmony_ci  MyStringCopy(_chars, s);
1677370b324cSopenharmony_ci  return *this;
1678370b324cSopenharmony_ci}
1679370b324cSopenharmony_ci
1680370b324cSopenharmony_civoid UString2::SetFromAscii(const char *s)
1681370b324cSopenharmony_ci{
1682370b324cSopenharmony_ci  unsigned len = MyStringLen(s);
1683370b324cSopenharmony_ci  if (len > _len)
1684370b324cSopenharmony_ci  {
1685370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1686370b324cSopenharmony_ci    if (_chars)
1687370b324cSopenharmony_ci      MY_STRING_DELETE(_chars)
1688370b324cSopenharmony_ci    _chars = newBuf;
1689370b324cSopenharmony_ci  }
1690370b324cSopenharmony_ci  wchar_t *chars = _chars;
1691370b324cSopenharmony_ci  for (unsigned i = 0; i < len; i++)
1692370b324cSopenharmony_ci    chars[i] = (unsigned char)s[i];
1693370b324cSopenharmony_ci  chars[len] = 0;
1694370b324cSopenharmony_ci  _len = len;
1695370b324cSopenharmony_ci}
1696370b324cSopenharmony_ci
1697370b324cSopenharmony_ciUString2 &UString2::operator=(const UString2 &s)
1698370b324cSopenharmony_ci{
1699370b324cSopenharmony_ci  if (&s == this)
1700370b324cSopenharmony_ci    return *this;
1701370b324cSopenharmony_ci  unsigned len = s._len;
1702370b324cSopenharmony_ci  if (len > _len)
1703370b324cSopenharmony_ci  {
1704370b324cSopenharmony_ci    wchar_t *newBuf = MY_STRING_NEW_wchar_t((size_t)len + 1);
1705370b324cSopenharmony_ci    if (_chars)
1706370b324cSopenharmony_ci      MY_STRING_DELETE(_chars)
1707370b324cSopenharmony_ci    _chars = newBuf;
1708370b324cSopenharmony_ci  }
1709370b324cSopenharmony_ci  _len = len;
1710370b324cSopenharmony_ci  MyStringCopy(_chars, s._chars);
1711370b324cSopenharmony_ci  return *this;
1712370b324cSopenharmony_ci}
1713370b324cSopenharmony_ci
1714370b324cSopenharmony_cibool operator==(const UString2 &s1, const UString2 &s2)
1715370b324cSopenharmony_ci{
1716370b324cSopenharmony_ci  return s1.Len() == s2.Len() && (s1.IsEmpty() || wcscmp(s1.GetRawPtr(), s2.GetRawPtr()) == 0);
1717370b324cSopenharmony_ci}
1718370b324cSopenharmony_ci
1719370b324cSopenharmony_cibool operator==(const UString2 &s1, const wchar_t *s2)
1720370b324cSopenharmony_ci{
1721370b324cSopenharmony_ci  if (s1.IsEmpty())
1722370b324cSopenharmony_ci    return (*s2 == 0);
1723370b324cSopenharmony_ci  return wcscmp(s1.GetRawPtr(), s2) == 0;
1724370b324cSopenharmony_ci}
1725370b324cSopenharmony_ci
1726370b324cSopenharmony_cibool operator==(const wchar_t *s1, const UString2 &s2)
1727370b324cSopenharmony_ci{
1728370b324cSopenharmony_ci  if (s2.IsEmpty())
1729370b324cSopenharmony_ci    return (*s1 == 0);
1730370b324cSopenharmony_ci  return wcscmp(s1, s2.GetRawPtr()) == 0;
1731370b324cSopenharmony_ci}
1732370b324cSopenharmony_ci
1733370b324cSopenharmony_ci
1734370b324cSopenharmony_ci
1735370b324cSopenharmony_ci// ----------------------------------------
1736370b324cSopenharmony_ci
1737370b324cSopenharmony_ci/*
1738370b324cSopenharmony_ciint MyStringCompareNoCase(const char *s1, const char *s2)
1739370b324cSopenharmony_ci{
1740370b324cSopenharmony_ci  return MyStringCompareNoCase(MultiByteToUnicodeString(s1), MultiByteToUnicodeString(s2));
1741370b324cSopenharmony_ci}
1742370b324cSopenharmony_ci*/
1743370b324cSopenharmony_ci
1744370b324cSopenharmony_ci#if !defined(USE_UNICODE_FSTRING) || !defined(_UNICODE)
1745370b324cSopenharmony_ci
1746370b324cSopenharmony_cistatic inline UINT GetCurrentCodePage()
1747370b324cSopenharmony_ci{
1748370b324cSopenharmony_ci  #if defined(UNDER_CE) || !defined(_WIN32)
1749370b324cSopenharmony_ci  return CP_ACP;
1750370b324cSopenharmony_ci  #else
1751370b324cSopenharmony_ci  return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP;
1752370b324cSopenharmony_ci  #endif
1753370b324cSopenharmony_ci}
1754370b324cSopenharmony_ci
1755370b324cSopenharmony_ci#endif
1756370b324cSopenharmony_ci
1757370b324cSopenharmony_ci#ifdef USE_UNICODE_FSTRING
1758370b324cSopenharmony_ci
1759370b324cSopenharmony_ci#ifndef _UNICODE
1760370b324cSopenharmony_ci
1761370b324cSopenharmony_ciAString fs2fas(CFSTR s)
1762370b324cSopenharmony_ci{
1763370b324cSopenharmony_ci  return UnicodeStringToMultiByte(s, GetCurrentCodePage());
1764370b324cSopenharmony_ci}
1765370b324cSopenharmony_ci
1766370b324cSopenharmony_ciFString fas2fs(const char *s)
1767370b324cSopenharmony_ci{
1768370b324cSopenharmony_ci  return MultiByteToUnicodeString(s, GetCurrentCodePage());
1769370b324cSopenharmony_ci}
1770370b324cSopenharmony_ci
1771370b324cSopenharmony_ciFString fas2fs(const AString &s)
1772370b324cSopenharmony_ci{
1773370b324cSopenharmony_ci  return MultiByteToUnicodeString(s, GetCurrentCodePage());
1774370b324cSopenharmony_ci}
1775370b324cSopenharmony_ci
1776370b324cSopenharmony_ci#endif //  _UNICODE
1777370b324cSopenharmony_ci
1778370b324cSopenharmony_ci#else // USE_UNICODE_FSTRING
1779370b324cSopenharmony_ci
1780370b324cSopenharmony_ciUString fs2us(const FChar *s)
1781370b324cSopenharmony_ci{
1782370b324cSopenharmony_ci  return MultiByteToUnicodeString(s, GetCurrentCodePage());
1783370b324cSopenharmony_ci}
1784370b324cSopenharmony_ci
1785370b324cSopenharmony_ciUString fs2us(const FString &s)
1786370b324cSopenharmony_ci{
1787370b324cSopenharmony_ci  return MultiByteToUnicodeString(s, GetCurrentCodePage());
1788370b324cSopenharmony_ci}
1789370b324cSopenharmony_ci
1790370b324cSopenharmony_ciFString us2fs(const wchar_t *s)
1791370b324cSopenharmony_ci{
1792370b324cSopenharmony_ci  return UnicodeStringToMultiByte(s, GetCurrentCodePage());
1793370b324cSopenharmony_ci}
1794370b324cSopenharmony_ci
1795370b324cSopenharmony_ci#endif // USE_UNICODE_FSTRING
1796370b324cSopenharmony_ci
1797370b324cSopenharmony_ci
1798370b324cSopenharmony_cibool CStringFinder::FindWord_In_LowCaseAsciiList_NoCase(const char *p, const wchar_t *str)
1799370b324cSopenharmony_ci{
1800370b324cSopenharmony_ci  _temp.Empty();
1801370b324cSopenharmony_ci  for (;;)
1802370b324cSopenharmony_ci  {
1803370b324cSopenharmony_ci    const wchar_t c = *str++;
1804370b324cSopenharmony_ci    if (c == 0)
1805370b324cSopenharmony_ci      break;
1806370b324cSopenharmony_ci    if (c <= 0x20 || c > 0x7f)
1807370b324cSopenharmony_ci      return false;
1808370b324cSopenharmony_ci    _temp += (char)MyCharLower_Ascii((char)c);
1809370b324cSopenharmony_ci  }
1810370b324cSopenharmony_ci
1811370b324cSopenharmony_ci  while (*p != 0)
1812370b324cSopenharmony_ci  {
1813370b324cSopenharmony_ci    const char *s2 = _temp.Ptr();
1814370b324cSopenharmony_ci    char c, c2;
1815370b324cSopenharmony_ci    do
1816370b324cSopenharmony_ci    {
1817370b324cSopenharmony_ci      c = *p++;
1818370b324cSopenharmony_ci      c2 = *s2++;
1819370b324cSopenharmony_ci    }
1820370b324cSopenharmony_ci    while (c == c2);
1821370b324cSopenharmony_ci
1822370b324cSopenharmony_ci    if (c == ' ')
1823370b324cSopenharmony_ci    {
1824370b324cSopenharmony_ci      if (c2 == 0)
1825370b324cSopenharmony_ci        return true;
1826370b324cSopenharmony_ci      continue;
1827370b324cSopenharmony_ci    }
1828370b324cSopenharmony_ci
1829370b324cSopenharmony_ci    while (*p++ != ' ');
1830370b324cSopenharmony_ci  }
1831370b324cSopenharmony_ci
1832370b324cSopenharmony_ci  return false;
1833370b324cSopenharmony_ci}
1834370b324cSopenharmony_ci
1835370b324cSopenharmony_ci
1836370b324cSopenharmony_civoid SplitString(const UString &srcString, UStringVector &destStrings)
1837370b324cSopenharmony_ci{
1838370b324cSopenharmony_ci  destStrings.Clear();
1839370b324cSopenharmony_ci  unsigned len = srcString.Len();
1840370b324cSopenharmony_ci  if (len == 0)
1841370b324cSopenharmony_ci    return;
1842370b324cSopenharmony_ci  UString s;
1843370b324cSopenharmony_ci  for (unsigned i = 0; i < len; i++)
1844370b324cSopenharmony_ci  {
1845370b324cSopenharmony_ci    const wchar_t c = srcString[i];
1846370b324cSopenharmony_ci    if (c == ' ')
1847370b324cSopenharmony_ci    {
1848370b324cSopenharmony_ci      if (!s.IsEmpty())
1849370b324cSopenharmony_ci      {
1850370b324cSopenharmony_ci        destStrings.Add(s);
1851370b324cSopenharmony_ci        s.Empty();
1852370b324cSopenharmony_ci      }
1853370b324cSopenharmony_ci    }
1854370b324cSopenharmony_ci    else
1855370b324cSopenharmony_ci      s += c;
1856370b324cSopenharmony_ci  }
1857370b324cSopenharmony_ci  if (!s.IsEmpty())
1858370b324cSopenharmony_ci    destStrings.Add(s);
1859370b324cSopenharmony_ci}
1860