other-licenses/7zstub/src/Common/String.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial