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.

128 lines
3.6 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. *
  17. *******************************************************************************/
  18. #include <cruntime.h>
  19. #include <windows.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <locale.h>
  23. #include <setlocal.h>
  24. #include <mtdll.h>
  25. #include <errno.h>
  26. #include <awint.h>
  27. #include <xlocinfo.h> /* for _Collvec, _Wcscoll */
  28. /***
  29. *static int _Wmemcmp(s1, s2, n) - compare wchar_t s1[n], s2[n]
  30. *
  31. *Purpose:
  32. *
  33. *Entry:
  34. *
  35. *Exit:
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40. static int _Wmemcmp(
  41. const wchar_t *s1,
  42. const wchar_t *s2,
  43. int n
  44. )
  45. {
  46. for (; 0 < n; ++s1, ++s2, --n)
  47. if (*s1 != *s2)
  48. return (*s1 < *s2 ? -1 : +1);
  49. return (0);
  50. }
  51. /***
  52. *int _Wcscoll() - Collate wide-character locale strings
  53. *
  54. *Purpose:
  55. * Compare two wchar_t strings using the locale LC_COLLATE information.
  56. * In the C locale, wcscmp() is used to make the comparison.
  57. *
  58. *Entry:
  59. * const wchar_t *_string1 = pointer to beginning of the first string
  60. * const wchar_t *_end1 = pointer past end of the first string
  61. * const wchar_t *_string2 = pointer to beginning of the second string
  62. * const wchar_t *_end2 = pointer past end of the second string
  63. * const _Collvec *ploc = pointer to locale info
  64. *
  65. *Exit:
  66. * -1 = first string less than second string
  67. * 0 = strings are equal
  68. * 1 = first string greater than second string
  69. * This range of return values may differ from other *cmp/*coll functions.
  70. *
  71. *Exceptions:
  72. * _NLSCMPERROR = error
  73. * errno = EINVAL
  74. *
  75. *******************************************************************************/
  76. int __cdecl _Wcscoll (
  77. const wchar_t *_string1,
  78. const wchar_t *_end1,
  79. const wchar_t *_string2,
  80. const wchar_t *_end2,
  81. const _Collvec *ploc
  82. )
  83. {
  84. int n1 = _end1 - _string1;
  85. int n2 = _end2 - _string2;
  86. int ret;
  87. LCID handle;
  88. #ifdef _MT
  89. int local_lock_flag;
  90. _lock_locale( local_lock_flag )
  91. #endif
  92. if (ploc == 0)
  93. handle = __lc_handle[LC_COLLATE];
  94. else
  95. handle = ploc->_Hand;
  96. if (handle == _CLOCALEHANDLE) {
  97. int ans;
  98. _unlock_locale( local_lock_flag )
  99. ans = _Wmemcmp(_string1, _string2, n1 < n2 ? n1 : n2);
  100. return ans != 0 || n1 == n2 ? ans : n1 < n2 ? -1 : +1;
  101. }
  102. if (0 == (ret = __crtCompareStringW(handle,
  103. 0,
  104. _string1,
  105. n1,
  106. _string2,
  107. n2,
  108. __lc_collate_cp)))
  109. {
  110. _unlock_locale( local_lock_flag )
  111. errno = EINVAL;
  112. return _NLSCMPERROR;
  113. }
  114. _unlock_locale( local_lock_flag )
  115. return (ret - 2);
  116. }