annotate src/win32/7zip/7z/CPP/Common/MyString.cpp @ 1:f9f4f1b99eed

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