MyString.cpp 4.5 KB

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