Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
6.9 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. STRUTILS.H
  5. Abstract:
  6. String utilities
  7. History:
  8. --*/
  9. #ifndef __WBEM_STRING_UTILS__H_
  10. #define __WBEM_STRING_UTILS__H_
  11. #pragma optimize("gt", on)
  12. #ifdef _DBG
  13. #define _DBG_BREAK DebugBreak();
  14. #else
  15. #define _DBG_BREAK
  16. #endif
  17. inline wchar_t ToLower(wchar_t c)
  18. {
  19. wchar_t wideChar ;
  20. if (LCMapStringW(LOCALE_INVARIANT, LCMAP_LOWERCASE, &c, 1, &wideChar, 1) ==0)
  21. {
  22. _DBG_BREAK;
  23. return c;
  24. }
  25. return wideChar;
  26. }
  27. inline wchar_t ToUpper(wchar_t c)
  28. {
  29. wchar_t wideChar ;
  30. if (LCMapStringW(LOCALE_INVARIANT, LCMAP_UPPERCASE, &c, 1, &wideChar, 1) ==0)
  31. {
  32. _DBG_BREAK;
  33. return c;
  34. }
  35. return wideChar;
  36. }
  37. inline wchar_t wbem_towlower(wchar_t c)
  38. {
  39. if(c >= 0 && c <= 127)
  40. {
  41. if(c >= 'A' && c <= 'Z')
  42. return c + ('a' - 'A');
  43. else
  44. return c;
  45. }
  46. else return ToLower(c);
  47. }
  48. inline wchar_t wbem_towupper(wchar_t c)
  49. {
  50. if(c >= 0 && c <= 127)
  51. {
  52. if(c >= 'a' && c <= 'z')
  53. return c + ('A' - 'a');
  54. else
  55. return c;
  56. }
  57. else return ToUpper(c);
  58. }
  59. inline int wbem_wcsicmp( const wchar_t* wsz1, const wchar_t* wsz2)
  60. {
  61. while(*wsz1 || *wsz2)
  62. {
  63. int diff = wbem_towlower(*wsz1) - wbem_towlower(*wsz2);
  64. if(diff) return diff;
  65. wsz1++; wsz2++;
  66. }
  67. return 0;
  68. }
  69. inline int wbem_unaligned_wcsicmp( UNALIGNED const wchar_t* wsz1, UNALIGNED const wchar_t* wsz2)
  70. {
  71. while(*wsz1 || *wsz2)
  72. {
  73. int diff = wbem_towlower(*wsz1) - wbem_towlower(*wsz2);
  74. if(diff) return diff;
  75. wsz1++; wsz2++;
  76. }
  77. return 0;
  78. }
  79. // just like wcsicmp, but first 0 of unicode chracters have been omitted
  80. inline int wbem_ncsicmp(const char* wsz1, const char* wsz2)
  81. {
  82. while(*wsz1 || *wsz2)
  83. {
  84. int diff = wbem_towlower((unsigned char)*wsz1) -
  85. wbem_towlower((unsigned char)*wsz2);
  86. if(diff) return diff;
  87. wsz1++; wsz2++;
  88. }
  89. return 0;
  90. }
  91. inline int wbem_wcsnicmp( const wchar_t* wsz1, const wchar_t* wsz2, size_t n )
  92. {
  93. while(n-- && (*wsz1 || *wsz2))
  94. {
  95. int diff = wbem_towlower(*wsz1) - wbem_towlower(*wsz2);
  96. if(diff) return diff;
  97. wsz1++; wsz2++;
  98. }
  99. return 0;
  100. }
  101. inline int wbem_unaligned_wcsnicmp( UNALIGNED const wchar_t* wsz1, UNALIGNED const wchar_t* wsz2, size_t n )
  102. {
  103. while(n-- && (*wsz1 || *wsz2))
  104. {
  105. int diff = wbem_towlower(*wsz1) - wbem_towlower(*wsz2);
  106. if(diff) return diff;
  107. wsz1++; wsz2++;
  108. }
  109. return 0;
  110. }
  111. inline int wbem_stricmp(const char* sz1, const char* sz2)
  112. {
  113. while(*sz1 || *sz2)
  114. {
  115. int diff = wbem_towlower(*sz1) - wbem_towlower(*sz2);
  116. if(diff) return diff;
  117. sz1++; sz2++;
  118. }
  119. return 0;
  120. }
  121. inline int wbem_strnicmp(const char* sz1, const char* sz2, size_t n)
  122. {
  123. while(n-- && (*sz1 || *sz2))
  124. {
  125. int diff = wbem_towlower(*sz1) - wbem_towlower(*sz2);
  126. if(diff) return diff;
  127. sz1++; sz2++;
  128. }
  129. return 0;
  130. }
  131. inline bool wbem_iswdigit(wchar_t c)
  132. {
  133. WORD result;
  134. if (GetStringTypeExW(LOCALE_INVARIANT, CT_CTYPE1, &c, 1, &result))
  135. {
  136. return (result & C1_DIGIT) != 0;
  137. };
  138. return false;
  139. };
  140. inline bool wbem_iswalnum (wchar_t c)
  141. {
  142. WORD result;
  143. if (GetStringTypeExW(LOCALE_INVARIANT, CT_CTYPE1, &c, 1, &result))
  144. {
  145. return (result & (C1_DIGIT | C1_ALPHA)) != 0;
  146. };
  147. return false;
  148. };
  149. inline bool wbem_isdigit(char c)
  150. {
  151. WORD result;
  152. if (GetStringTypeExA(LOCALE_INVARIANT, CT_CTYPE1, &c, 1, &result))
  153. {
  154. return (result & C1_DIGIT) != 0;
  155. };
  156. return false;
  157. };
  158. //
  159. // returns the real length or Max + 1 if it exceeds
  160. // useful for not probing the entire string to see that it's too big
  161. //
  162. /////////////////////////////////////////
  163. inline size_t wcslen_max(WCHAR * p, size_t Max)
  164. {
  165. WCHAR * pBegin = p;
  166. WCHAR * pTail = p + Max + 1;
  167. while (*p && (p < pTail)) p++;
  168. return p-pBegin;
  169. };
  170. /*
  171. size_t wbem_mbstowcs(
  172. wchar_t *pwcs,
  173. const char *s,
  174. size_t n)
  175. {
  176. size_t count = 0;
  177. if (pwcs && n == 0)
  178. // dest string exists, but 0 bytes converted
  179. return (size_t) 0;
  180. #ifdef _WIN64
  181. // n must fit into an int for MultiByteToWideCha
  182. if ( n > INT_MAX )
  183. return (size_t)-1;
  184. #endif
  185. // if destination string exists, fill it in
  186. if (pwcs)
  187. {
  188. int bytecnt, charcnt;
  189. unsigned char *p;
  190. // Assume that the buffer is large enough
  191. if ( (count = MultiByteToWideChar( CP_ACP,
  192. MB_PRECOMPOSED |
  193. MB_ERR_INVALID_CHARS,
  194. s,
  195. -1,
  196. pwcs,
  197. (int)n )) != 0 )
  198. return count - 1; // don't count NUL
  199. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  200. {
  201. errno = EILSEQ;
  202. return (size_t)-1;
  203. }
  204. // User-supplied buffer not large enough.
  205. // How many bytes are in n characters of the string?
  206. charcnt = (int)n;
  207. for (p = (unsigned char *)s; (charcnt-- && *p); p++)
  208. {
  209. if (__isleadbyte_mt(ptloci, *p))
  210. p++;
  211. }
  212. bytecnt = ((int) ((char *)p - (char *)s));
  213. if ( (count = MultiByteToWideChar( ptloci->lc_codepage,
  214. MB_PRECOMPOSED,
  215. s,
  216. bytecnt,
  217. pwcs,
  218. (int)n )) == 0 )
  219. {
  220. return (size_t)-1;
  221. }
  222. return count; // no NUL in string
  223. }
  224. else // pwcs == NULL, get size only, s must be NUL-terminated
  225. {
  226. if ( (count = MultiByteToWideChar( CP_ACP,
  227. MB_PRECOMPOSED |
  228. MB_ERR_INVALID_CHARS,
  229. s,
  230. -1,
  231. NULL,
  232. 0 )) == 0 )
  233. {
  234. return (size_t)-1;
  235. }
  236. return count - 1;
  237. }
  238. }
  239. */
  240. #pragma optimize("", on)
  241. #endif