2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "wtf/text/Collator.h"
32 #include "wtf/Assertions.h"
33 #include "wtf/StringExtras.h"
34 #include "wtf/Threading.h"
35 #include "wtf/ThreadingPrimitives.h"
38 #include <unicode/ucol.h>
42 static UCollator
* cachedCollator
;
43 static char cachedEquivalentLocale
[Collator::ulocFullnameCapacity
];
44 static Mutex
& cachedCollatorMutex()
46 AtomicallyInitializedStaticReference(Mutex
, mutex
, new Mutex
);
50 Collator::Collator(const char* locale
)
52 , m_locale(locale
? strdup(locale
) : 0)
55 setEquivalentLocale(m_locale
, m_equivalentLocale
);
58 PassOwnPtr
<Collator
> Collator::userDefault()
60 return adoptPtr(new Collator(0));
69 void Collator::setOrderLowerFirst(bool lowerFirst
)
71 m_lowerFirst
= lowerFirst
;
74 Collator::Result
Collator::collate(const UChar
* lhs
, size_t lhsLength
, const UChar
* rhs
, size_t rhsLength
) const
79 return static_cast<Result
>(ucol_strcoll(m_collator
, lhs
, lhsLength
, rhs
, rhsLength
));
82 void Collator::createCollator() const
85 UErrorCode status
= U_ZERO_ERROR
;
88 Locker
<Mutex
> lock(cachedCollatorMutex());
90 UColAttributeValue cachedCollatorLowerFirst
= ucol_getAttribute(cachedCollator
, UCOL_CASE_FIRST
, &status
);
91 ASSERT(U_SUCCESS(status
));
93 if (0 == strcmp(cachedEquivalentLocale
, m_equivalentLocale
)
94 && ((UCOL_LOWER_FIRST
== cachedCollatorLowerFirst
&& m_lowerFirst
) || (UCOL_UPPER_FIRST
== cachedCollatorLowerFirst
&& !m_lowerFirst
))) {
95 m_collator
= cachedCollator
;
97 cachedEquivalentLocale
[0] = 0;
103 m_collator
= ucol_open(m_locale
, &status
);
104 if (U_FAILURE(status
)) {
105 status
= U_ZERO_ERROR
;
106 m_collator
= ucol_open("", &status
); // Fallback to Unicode Collation Algorithm.
108 ASSERT(U_SUCCESS(status
));
110 ucol_setAttribute(m_collator
, UCOL_CASE_FIRST
, m_lowerFirst
? UCOL_LOWER_FIRST
: UCOL_UPPER_FIRST
, &status
);
111 ASSERT(U_SUCCESS(status
));
113 ucol_setAttribute(m_collator
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
114 ASSERT(U_SUCCESS(status
));
117 void Collator::releaseCollator()
120 Locker
<Mutex
> lock(cachedCollatorMutex());
122 ucol_close(cachedCollator
);
123 cachedCollator
= m_collator
;
124 strncpy(cachedEquivalentLocale
, m_equivalentLocale
, ulocFullnameCapacity
);
130 void Collator::setEquivalentLocale(const char* locale
, char* equivalentLocale
)
132 UErrorCode status
= U_ZERO_ERROR
;
134 ucol_getFunctionalEquivalent(equivalentLocale
, ulocFullnameCapacity
, "collation", locale
, &isAvailable
, &status
);
135 if (U_FAILURE(status
))
136 strcpy(equivalentLocale
, "root");