CollatorICU.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2008 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #include <wtf/unicode/Collator.h>
  30. #if USE(ICU_UNICODE) && !UCONFIG_NO_COLLATION
  31. #include <wtf/Assertions.h>
  32. #include <wtf/StringExtras.h>
  33. #include <wtf/Threading.h>
  34. #include <unicode/ucol.h>
  35. #include <string.h>
  36. #if OS(DARWIN)
  37. #include <wtf/RetainPtr.h>
  38. #include <CoreFoundation/CoreFoundation.h>
  39. #endif
  40. namespace WTF {
  41. static UCollator* cachedCollator;
  42. static Mutex& cachedCollatorMutex()
  43. {
  44. AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
  45. return mutex;
  46. }
  47. Collator::Collator(const char* locale)
  48. : m_collator(0)
  49. , m_locale(locale ? strdup(locale) : 0)
  50. , m_lowerFirst(false)
  51. {
  52. }
  53. PassOwnPtr<Collator> Collator::userDefault()
  54. {
  55. #if OS(DARWIN) && USE(CF)
  56. // Mac OS X doesn't set UNIX locale to match user-selected one, so ICU default doesn't work.
  57. #if !OS(IOS)
  58. RetainPtr<CFLocaleRef> currentLocale = adoptCF(CFLocaleCopyCurrent());
  59. CFStringRef collationOrder = (CFStringRef)CFLocaleGetValue(currentLocale.get(), kCFLocaleCollatorIdentifier);
  60. #else
  61. RetainPtr<CFStringRef> collationOrderRetainer = adoptCF((CFStringRef)CFPreferencesCopyValue(CFSTR("AppleCollationOrder"), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
  62. CFStringRef collationOrder = collationOrderRetainer.get();
  63. #endif
  64. char buf[256];
  65. if (!collationOrder)
  66. return adoptPtr(new Collator(""));
  67. CFStringGetCString(collationOrder, buf, sizeof(buf), kCFStringEncodingASCII);
  68. return adoptPtr(new Collator(buf));
  69. #else
  70. return adoptPtr(new Collator(0));
  71. #endif
  72. }
  73. Collator::~Collator()
  74. {
  75. releaseCollator();
  76. free(m_locale);
  77. }
  78. void Collator::setOrderLowerFirst(bool lowerFirst)
  79. {
  80. m_lowerFirst = lowerFirst;
  81. }
  82. Collator::Result Collator::collate(const UChar* lhs, size_t lhsLength, const UChar* rhs, size_t rhsLength) const
  83. {
  84. if (!m_collator)
  85. createCollator();
  86. return static_cast<Result>(ucol_strcoll(m_collator, lhs, lhsLength, rhs, rhsLength));
  87. }
  88. void Collator::createCollator() const
  89. {
  90. ASSERT(!m_collator);
  91. UErrorCode status = U_ZERO_ERROR;
  92. {
  93. Locker<Mutex> lock(cachedCollatorMutex());
  94. if (cachedCollator) {
  95. const char* cachedCollatorLocale = ucol_getLocaleByType(cachedCollator, ULOC_REQUESTED_LOCALE, &status);
  96. ASSERT(U_SUCCESS(status));
  97. ASSERT(cachedCollatorLocale);
  98. UColAttributeValue cachedCollatorLowerFirst = ucol_getAttribute(cachedCollator, UCOL_CASE_FIRST, &status);
  99. ASSERT(U_SUCCESS(status));
  100. // FIXME: default locale is never matched, because ucol_getLocaleByType returns the actual one used, not 0.
  101. if (m_locale && 0 == strcmp(cachedCollatorLocale, m_locale)
  102. && ((UCOL_LOWER_FIRST == cachedCollatorLowerFirst && m_lowerFirst) || (UCOL_UPPER_FIRST == cachedCollatorLowerFirst && !m_lowerFirst))) {
  103. m_collator = cachedCollator;
  104. cachedCollator = 0;
  105. return;
  106. }
  107. }
  108. }
  109. m_collator = ucol_open(m_locale, &status);
  110. if (U_FAILURE(status)) {
  111. status = U_ZERO_ERROR;
  112. m_collator = ucol_open("", &status); // Fallback to Unicode Collation Algorithm.
  113. }
  114. ASSERT(U_SUCCESS(status));
  115. ucol_setAttribute(m_collator, UCOL_CASE_FIRST, m_lowerFirst ? UCOL_LOWER_FIRST : UCOL_UPPER_FIRST, &status);
  116. ASSERT(U_SUCCESS(status));
  117. ucol_setAttribute(m_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
  118. ASSERT(U_SUCCESS(status));
  119. }
  120. void Collator::releaseCollator()
  121. {
  122. {
  123. Locker<Mutex> lock(cachedCollatorMutex());
  124. if (cachedCollator)
  125. ucol_close(cachedCollator);
  126. cachedCollator = m_collator;
  127. m_collator = 0;
  128. }
  129. }
  130. } // namespace WTF
  131. #endif // USE(ICU_UNICODE) && !UCONFIG_NO_COLLATION