view src/win32/7zip/7z/CPP/Common/MyString.cpp @ 6:458a4f18f3cd

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