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