1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Common/MyString.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _WIN32
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <ctype.h>
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _UNICODE
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StringConvert.h"
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "MyString.h"
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef _WIN32
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _UNICODE
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
20baa3858d3f5d128a5c8466b700098109edcad5f2repo syncwchar_t MyCharUpper(wchar_t c)
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (c == 0)
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  wchar_t *res = CharUpperW((LPWSTR)(UINT_PTR)(unsigned int)c);
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return (wchar_t)(unsigned int)(UINT_PTR)res;
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const int kBufferSize = 4;
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  char s[kBufferSize + 1];
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numChars == 0 || numChars > kBufferSize)
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return c;
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  s[numChars] = 0;
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ::CharUpperA(s);
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return c;
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
38baa3858d3f5d128a5c8466b700098109edcad5f2repo syncwchar_t MyCharLower(wchar_t c)
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (c == 0)
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  wchar_t *res = CharLowerW((LPWSTR)(UINT_PTR)(unsigned int)c);
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return (wchar_t)(unsigned int)(UINT_PTR)res;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const int kBufferSize = 4;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  char s[kBufferSize + 1];
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numChars == 0 || numChars > kBufferSize)
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return c;
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  s[numChars] = 0;
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ::CharLowerA(s);
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return c;
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
56baa3858d3f5d128a5c8466b700098109edcad5f2repo syncwchar_t * MyStringUpper(wchar_t *s)
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (s == 0)
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  wchar_t *res = CharUpperW(s);
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return res;
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  AString a = UnicodeStringToMultiByte(s);
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  a.MakeUpper();
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
68baa3858d3f5d128a5c8466b700098109edcad5f2repo syncwchar_t * MyStringLower(wchar_t *s)
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (s == 0)
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0;
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  wchar_t *res = CharLowerW(s);
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return res;
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  AString a = UnicodeStringToMultiByte(s);
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  a.MakeLower();
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/*
83baa3858d3f5d128a5c8466b700098109edcad5f2repo syncinline int ConvertCompareResult(int r) { return r - 2; }
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
85baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCollate(const wchar_t *s1, const wchar_t *s2)
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int res = CompareStringW(
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1);
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef _UNICODE
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return ConvertCompareResult(res);
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return ConvertCompareResult(res);
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return MyStringCollate(UnicodeStringToMultiByte(s1),
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        UnicodeStringToMultiByte(s2));
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef UNDER_CE
100baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCollate(const char *s1, const char *s2)
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return ConvertCompareResult(CompareStringA(
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1));
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
106baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCollateNoCase(const char *s1, const char *s2)
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return ConvertCompareResult(CompareStringA(
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1));
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
113baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2)
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int res = CompareStringW(
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1);
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef _UNICODE
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return ConvertCompareResult(res);
119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return ConvertCompareResult(res);
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return MyStringCollateNoCase(UnicodeStringToMultiByte(s1),
123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UnicodeStringToMultiByte(s2));
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync*/
127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#else
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
130baa3858d3f5d128a5c8466b700098109edcad5f2repo syncwchar_t MyCharUpper(wchar_t c)
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return toupper(c);
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/*
136baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2)
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t c1 = *s1++;
141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t c2 = *s2++;
142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t u1 = MyCharUpper(c1);
143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t u2 = MyCharUpper(c2);
144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (u1 < u2) return -1;
146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (u1 > u2) return 1;
147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (u1 == 0) return 0;
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync*/
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
154baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCompare(const char *s1, const char *s2)
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    unsigned char c1 = (unsigned char)*s1++;
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    unsigned char c2 = (unsigned char)*s2++;
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 < c2) return -1;
161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 > c2) return 1;
162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 == 0) return 0;
163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
166baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCompare(const wchar_t *s1, const wchar_t *s2)
167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t c1 = *s1++;
171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t c2 = *s2++;
172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 < c2) return -1;
173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 > c2) return 1;
174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 == 0) return 0;
175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
178baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2)
179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t c1 = *s1++;
183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t c2 = *s2++;
184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 != c2)
185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      wchar_t u1 = MyCharUpper(c1);
187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      wchar_t u2 = MyCharUpper(c2);
188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (u1 < u2) return -1;
189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (u1 > u2) return 1;
190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (c1 == 0) return 0;
192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/*
196baa3858d3f5d128a5c8466b700098109edcad5f2repo syncint MyStringCompareNoCase(const char *s1, const char *s2)
197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return MyStringCompareNoCase(MultiByteToUnicodeString(s1), MultiByteToUnicodeString(s2));
199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync*/
201