00001
00002
00003
00004
00005 #include "StdAfx.h"
00006
00007 #ifdef _WIN32
00008 #include "StringConvert.h"
00009 #else
00010 #include <ctype.h>
00011 #endif
00012
00013 #include "String.h"
00014
00015 #ifdef _WIN32
00016
00017 #ifndef _UNICODE
00018
00019 wchar_t MyCharUpper(wchar_t c)
00020 {
00021 if (c == 0)
00022 return 0;
00023 wchar_t *res = CharUpperW((LPWSTR)(unsigned int)c);
00024 if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
00025 return (wchar_t)(unsigned int)res;
00026 const int kBufferSize = 4;
00027 char s[kBufferSize + 1];
00028 int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
00029 if (numChars == 0 || numChars > kBufferSize)
00030 return c;
00031 s[numChars] = 0;
00032 ::CharUpperA(s);
00033 ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
00034 return c;
00035 }
00036
00037 wchar_t MyCharLower(wchar_t c)
00038 {
00039 if (c == 0)
00040 return 0;
00041 wchar_t *res = CharLowerW((LPWSTR)(unsigned int)c);
00042 if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
00043 return (wchar_t)(unsigned int)res;
00044 const int kBufferSize = 4;
00045 char s[kBufferSize + 1];
00046 int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
00047 if (numChars == 0 || numChars > kBufferSize)
00048 return c;
00049 s[numChars] = 0;
00050 ::CharLowerA(s);
00051 ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
00052 return c;
00053 }
00054
00055 wchar_t * MyStringUpper(wchar_t *s)
00056 {
00057 if (s == 0)
00058 return 0;
00059 wchar_t *res = CharUpperW(s);
00060 if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
00061 return res;
00062 AString a = UnicodeStringToMultiByte(s);
00063 a.MakeUpper();
00064 return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
00065 }
00066
00067 wchar_t * MyStringLower(wchar_t *s)
00068 {
00069 if (s == 0)
00070 return 0;
00071 wchar_t *res = CharLowerW(s);
00072 if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
00073 return res;
00074 AString a = UnicodeStringToMultiByte(s);
00075 a.MakeLower();
00076 return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
00077 }
00078
00079 #endif
00080
00081 #else
00082
00083 wchar_t MyCharUpper(wchar_t c)
00084 {
00085 return toupper(c);
00086 }
00087
00088 #endif
00089
00090 int MyStringCompare(const char *s1, const char *s2)
00091 {
00092 while (true)
00093 {
00094 unsigned char c1 = (unsigned char)*s1++;
00095 unsigned char c2 = (unsigned char)*s2++;
00096 if (c1 < c2) return -1;
00097 if (c1 > c2) return 1;
00098 if (c1 == 0) return 0;
00099 }
00100 }
00101
00102 int MyStringCompare(const wchar_t *s1, const wchar_t *s2)
00103 {
00104 while (true)
00105 {
00106 wchar_t c1 = *s1++;
00107 wchar_t c2 = *s2++;
00108 if (c1 < c2) return -1;
00109 if (c1 > c2) return 1;
00110 if (c1 == 0) return 0;
00111 }
00112 }
00113
00114 int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2)
00115 {
00116 while (true)
00117 {
00118 wchar_t c1 = *s1++;
00119 wchar_t c2 = *s2++;
00120 if (c1 != c2)
00121 {
00122 wchar_t u1 = MyCharUpper(c1);
00123 wchar_t u2 = MyCharUpper(c2);
00124 if (u1 < u2) return -1;
00125 if (u1 > u2) return 1;
00126 }
00127 if (c1 == 0) return 0;
00128 }
00129 }
00130
00131 #ifdef _WIN32
00132 int MyStringCompareNoCase(const char *s1, const char *s2)
00133 {
00134 return MyStringCompareNoCase(MultiByteToUnicodeString(s1), MultiByteToUnicodeString(s2));
00135 }
00136 #endif