Source code of Windows XP (NT5)
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.

131 lines
3.8 KiB

  1. /***
  2. *xwcscoll.c - Collate wide-character locale strings
  3. *
  4. * Copyright (c) 1996-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Compare two wchar_t strings using the locale LC_COLLATE information.
  8. *
  9. *Revision History:
  10. * 01-XX-96 GJF Created from wcscoll.c January 1996 by P.J. Plauger
  11. * 04-18-96 GJF Updated for current locale locking. Also, reformatted
  12. * and made several cosmetic changes.
  13. * 12-02-97 GJF Removed bogus codepage determination.
  14. * 01-12-98 GJF Use _lc_collate_cp codepage.
  15. * 01-05-99 GJF Changes for 64-bit size_t.
  16. * 05-11-99 PML Win64 fix: cast ptr diff to int
  17. * 01-29-01 GB Added _func function version of data variable used in msvcprt.lib
  18. * to work with STATIC_CPPLIB
  19. *
  20. *******************************************************************************/
  21. #include <cruntime.h>
  22. #include <windows.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <locale.h>
  26. #include <setlocal.h>
  27. #include <mtdll.h>
  28. #include <errno.h>
  29. #include <awint.h>
  30. #include <xlocinfo.h> /* for _Collvec, _Wcscoll */
  31. /***
  32. *static int _Wmemcmp(s1, s2, n) - compare wchar_t s1[n], s2[n]
  33. *
  34. *Purpose:
  35. *
  36. *Entry:
  37. *
  38. *Exit:
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43. static int _Wmemcmp(
  44. const wchar_t *s1,
  45. const wchar_t *s2,
  46. int n
  47. )
  48. {
  49. for (; 0 < n; ++s1, ++s2, --n)
  50. if (*s1 != *s2)
  51. return (*s1 < *s2 ? -1 : +1);
  52. return (0);
  53. }
  54. /***
  55. *int _Wcscoll() - Collate wide-character locale strings
  56. *
  57. *Purpose:
  58. * Compare two wchar_t strings using the locale LC_COLLATE information.
  59. * In the C locale, wcscmp() is used to make the comparison.
  60. *
  61. *Entry:
  62. * const wchar_t *_string1 = pointer to beginning of the first string
  63. * const wchar_t *_end1 = pointer past end of the first string
  64. * const wchar_t *_string2 = pointer to beginning of the second string
  65. * const wchar_t *_end2 = pointer past end of the second string
  66. * const _Collvec *ploc = pointer to locale info
  67. *
  68. *Exit:
  69. * -1 = first string less than second string
  70. * 0 = strings are equal
  71. * 1 = first string greater than second string
  72. * This range of return values may differ from other *cmp/*coll functions.
  73. *
  74. *Exceptions:
  75. * _NLSCMPERROR = error
  76. * errno = EINVAL
  77. *
  78. *******************************************************************************/
  79. int __cdecl _Wcscoll (
  80. const wchar_t *_string1,
  81. const wchar_t *_end1,
  82. const wchar_t *_string2,
  83. const wchar_t *_end2,
  84. const _Collvec *ploc
  85. )
  86. {
  87. int n1 = (int)(_end1 - _string1);
  88. int n2 = (int)(_end2 - _string2);
  89. int ret;
  90. LCID handle;
  91. #ifdef _MT
  92. int local_lock_flag;
  93. _lock_locale( local_lock_flag )
  94. #endif
  95. if (ploc == 0)
  96. handle = ___lc_handle_func()[LC_COLLATE];
  97. else
  98. handle = ploc->_Hand;
  99. if (handle == _CLOCALEHANDLE) {
  100. int ans;
  101. _unlock_locale( local_lock_flag )
  102. ans = _Wmemcmp(_string1, _string2, n1 < n2 ? n1 : n2);
  103. return ans != 0 || n1 == n2 ? ans : n1 < n2 ? -1 : +1;
  104. }
  105. if (0 == (ret = __crtCompareStringW(handle,
  106. 0,
  107. _string1,
  108. n1,
  109. _string2,
  110. n2,
  111. ___lc_collate_cp_func())))
  112. {
  113. _unlock_locale( local_lock_flag )
  114. errno = EINVAL;
  115. return _NLSCMPERROR;
  116. }
  117. _unlock_locale( local_lock_flag )
  118. return (ret - 2);
  119. }