| OLD | NEW |
| (Empty) |
| 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 | |
| 29 #include "config.h" | |
| 30 #include <wtf/unicode/Collator.h> | |
| 31 | |
| 32 #if USE(ICU_UNICODE) && !UCONFIG_NO_COLLATION | |
| 33 | |
| 34 #include <wtf/Assertions.h> | |
| 35 #include <wtf/StringExtras.h> | |
| 36 #include <wtf/Threading.h> | |
| 37 #include <unicode/ucol.h> | |
| 38 #include <string.h> | |
| 39 | |
| 40 #if OS(DARWIN) | |
| 41 #include <wtf/RetainPtr.h> | |
| 42 #include <CoreFoundation/CoreFoundation.h> | |
| 43 #endif | |
| 44 | |
| 45 namespace WTF { | |
| 46 | |
| 47 static UCollator* cachedCollator; | |
| 48 static Mutex& cachedCollatorMutex() | |
| 49 { | |
| 50 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); | |
| 51 return mutex; | |
| 52 } | |
| 53 | |
| 54 Collator::Collator(const char* locale) | |
| 55 : m_collator(0) | |
| 56 , m_locale(locale ? strdup(locale) : 0) | |
| 57 , m_lowerFirst(false) | |
| 58 { | |
| 59 } | |
| 60 | |
| 61 PassOwnPtr<Collator> Collator::userDefault() | |
| 62 { | |
| 63 #if OS(DARWIN) && USE(CF) | |
| 64 // Mac OS X doesn't set UNIX locale to match user-selected one, so ICU defau
lt doesn't work. | |
| 65 #if !OS(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 | |
| 66 RetainPtr<CFLocaleRef> currentLocale(AdoptCF, CFLocaleCopyCurrent()); | |
| 67 CFStringRef collationOrder = (CFStringRef)CFLocaleGetValue(currentLocale.get
(), kCFLocaleCollatorIdentifier); | |
| 68 #else | |
| 69 RetainPtr<CFStringRef> collationOrderRetainer(AdoptCF, (CFStringRef)CFPrefer
encesCopyValue(CFSTR("AppleCollationOrder"), kCFPreferencesAnyApplication, kCFPr
eferencesCurrentUser, kCFPreferencesAnyHost)); | |
| 70 CFStringRef collationOrder = collationOrderRetainer.get(); | |
| 71 #endif | |
| 72 char buf[256]; | |
| 73 if (!collationOrder) | |
| 74 return adoptPtr(new Collator("")); | |
| 75 CFStringGetCString(collationOrder, buf, sizeof(buf), kCFStringEncodingASCII)
; | |
| 76 return adoptPtr(new Collator(buf)); | |
| 77 #else | |
| 78 return adoptPtr(new Collator(0)); | |
| 79 #endif | |
| 80 } | |
| 81 | |
| 82 Collator::~Collator() | |
| 83 { | |
| 84 releaseCollator(); | |
| 85 free(m_locale); | |
| 86 } | |
| 87 | |
| 88 void Collator::setOrderLowerFirst(bool lowerFirst) | |
| 89 { | |
| 90 m_lowerFirst = lowerFirst; | |
| 91 } | |
| 92 | |
| 93 Collator::Result Collator::collate(const UChar* lhs, size_t lhsLength, const UCh
ar* rhs, size_t rhsLength) const | |
| 94 { | |
| 95 if (!m_collator) | |
| 96 createCollator(); | |
| 97 | |
| 98 return static_cast<Result>(ucol_strcoll(m_collator, lhs, lhsLength, rhs, rhs
Length)); | |
| 99 } | |
| 100 | |
| 101 void Collator::createCollator() const | |
| 102 { | |
| 103 ASSERT(!m_collator); | |
| 104 UErrorCode status = U_ZERO_ERROR; | |
| 105 | |
| 106 { | |
| 107 Locker<Mutex> lock(cachedCollatorMutex()); | |
| 108 if (cachedCollator) { | |
| 109 const char* cachedCollatorLocale = ucol_getLocaleByType(cachedCollat
or, ULOC_REQUESTED_LOCALE, &status); | |
| 110 ASSERT(U_SUCCESS(status)); | |
| 111 ASSERT(cachedCollatorLocale); | |
| 112 | |
| 113 UColAttributeValue cachedCollatorLowerFirst = ucol_getAttribute(cach
edCollator, UCOL_CASE_FIRST, &status); | |
| 114 ASSERT(U_SUCCESS(status)); | |
| 115 | |
| 116 // FIXME: default locale is never matched, because ucol_getLocaleByT
ype returns the actual one used, not 0. | |
| 117 if (m_locale && 0 == strcmp(cachedCollatorLocale, m_locale) | |
| 118 && ((UCOL_LOWER_FIRST == cachedCollatorLowerFirst && m_lowerFirs
t) || (UCOL_UPPER_FIRST == cachedCollatorLowerFirst && !m_lowerFirst))) { | |
| 119 m_collator = cachedCollator; | |
| 120 cachedCollator = 0; | |
| 121 return; | |
| 122 } | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 m_collator = ucol_open(m_locale, &status); | |
| 127 if (U_FAILURE(status)) { | |
| 128 status = U_ZERO_ERROR; | |
| 129 m_collator = ucol_open("", &status); // Fallback to Unicode Collation Al
gorithm. | |
| 130 } | |
| 131 ASSERT(U_SUCCESS(status)); | |
| 132 | |
| 133 ucol_setAttribute(m_collator, UCOL_CASE_FIRST, m_lowerFirst ? UCOL_LOWER_FIR
ST : UCOL_UPPER_FIRST, &status); | |
| 134 ASSERT(U_SUCCESS(status)); | |
| 135 | |
| 136 ucol_setAttribute(m_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); | |
| 137 ASSERT(U_SUCCESS(status)); | |
| 138 } | |
| 139 | |
| 140 void Collator::releaseCollator() | |
| 141 { | |
| 142 { | |
| 143 Locker<Mutex> lock(cachedCollatorMutex()); | |
| 144 if (cachedCollator) | |
| 145 ucol_close(cachedCollator); | |
| 146 cachedCollator = m_collator; | |
| 147 m_collator = 0; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 } // namespace WTF | |
| 152 | |
| 153 #endif // USE(ICU_UNICODE) && !UCONFIG_NO_COLLATION | |
| OLD | NEW |