| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include "wtf/Threading.h" | 33 #include "wtf/Threading.h" |
| 34 #include "wtf/ThreadingPrimitives.h" | 34 #include "wtf/ThreadingPrimitives.h" |
| 35 #include <stdlib.h> | 35 #include <stdlib.h> |
| 36 #include <string.h> | 36 #include <string.h> |
| 37 #include <unicode/ucol.h> | 37 #include <unicode/ucol.h> |
| 38 | 38 |
| 39 namespace WTF { | 39 namespace WTF { |
| 40 | 40 |
| 41 static UCollator* cachedCollator; | 41 static UCollator* cachedCollator; |
| 42 static char cachedEquivalentLocale[Collator::ulocFullnameCapacity]; | 42 static char cachedEquivalentLocale[Collator::ulocFullnameCapacity]; |
| 43 static Mutex& cachedCollatorMutex() | 43 static Mutex& cachedCollatorMutex() { |
| 44 { | 44 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex); |
| 45 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex); | 45 return mutex; |
| 46 return mutex; | |
| 47 } | 46 } |
| 48 | 47 |
| 49 Collator::Collator(const char* locale) | 48 Collator::Collator(const char* locale) |
| 50 : m_collator(0) | 49 : m_collator(0), |
| 51 , m_locale(locale ? strdup(locale) : 0) | 50 m_locale(locale ? strdup(locale) : 0), |
| 52 , m_lowerFirst(false) | 51 m_lowerFirst(false) { |
| 53 { | 52 setEquivalentLocale(m_locale, m_equivalentLocale); |
| 54 setEquivalentLocale(m_locale, m_equivalentLocale); | |
| 55 } | 53 } |
| 56 | 54 |
| 57 PassOwnPtr<Collator> Collator::userDefault() | 55 PassOwnPtr<Collator> Collator::userDefault() { |
| 58 { | 56 return adoptPtr(new Collator(0)); |
| 59 return adoptPtr(new Collator(0)); | |
| 60 } | 57 } |
| 61 | 58 |
| 62 Collator::~Collator() | 59 Collator::~Collator() { |
| 63 { | 60 releaseCollator(); |
| 64 releaseCollator(); | 61 free(m_locale); |
| 65 free(m_locale); | |
| 66 } | 62 } |
| 67 | 63 |
| 68 void Collator::setOrderLowerFirst(bool lowerFirst) | 64 void Collator::setOrderLowerFirst(bool lowerFirst) { |
| 69 { | 65 m_lowerFirst = lowerFirst; |
| 70 m_lowerFirst = lowerFirst; | |
| 71 } | 66 } |
| 72 | 67 |
| 73 Collator::Result Collator::collate(const UChar* lhs, size_t lhsLength, const UCh
ar* rhs, size_t rhsLength) const | 68 Collator::Result Collator::collate(const UChar* lhs, |
| 74 { | 69 size_t lhsLength, |
| 75 if (!m_collator) | 70 const UChar* rhs, |
| 76 createCollator(); | 71 size_t rhsLength) const { |
| 72 if (!m_collator) |
| 73 createCollator(); |
| 77 | 74 |
| 78 return static_cast<Result>(ucol_strcoll(m_collator, lhs, lhsLength, rhs, rhs
Length)); | 75 return static_cast<Result>( |
| 76 ucol_strcoll(m_collator, lhs, lhsLength, rhs, rhsLength)); |
| 79 } | 77 } |
| 80 | 78 |
| 81 void Collator::createCollator() const | 79 void Collator::createCollator() const { |
| 82 { | 80 ASSERT(!m_collator); |
| 83 ASSERT(!m_collator); | 81 UErrorCode status = U_ZERO_ERROR; |
| 84 UErrorCode status = U_ZERO_ERROR; | |
| 85 | 82 |
| 86 { | 83 { |
| 87 Locker<Mutex> lock(cachedCollatorMutex()); | 84 Locker<Mutex> lock(cachedCollatorMutex()); |
| 88 if (cachedCollator) { | 85 if (cachedCollator) { |
| 89 UColAttributeValue cachedCollatorLowerFirst = ucol_getAttribute(cach
edCollator, UCOL_CASE_FIRST, &status); | 86 UColAttributeValue cachedCollatorLowerFirst = |
| 90 ASSERT(U_SUCCESS(status)); | 87 ucol_getAttribute(cachedCollator, UCOL_CASE_FIRST, &status); |
| 88 ASSERT(U_SUCCESS(status)); |
| 91 | 89 |
| 92 if (0 == strcmp(cachedEquivalentLocale, m_equivalentLocale) | 90 if (0 == strcmp(cachedEquivalentLocale, m_equivalentLocale) && |
| 93 && ((UCOL_LOWER_FIRST == cachedCollatorLowerFirst && m_lowerFirs
t) || (UCOL_UPPER_FIRST == cachedCollatorLowerFirst && !m_lowerFirst))) { | 91 ((UCOL_LOWER_FIRST == cachedCollatorLowerFirst && m_lowerFirst) || |
| 94 m_collator = cachedCollator; | 92 (UCOL_UPPER_FIRST == cachedCollatorLowerFirst && !m_lowerFirst))) { |
| 95 cachedCollator = 0; | 93 m_collator = cachedCollator; |
| 96 cachedEquivalentLocale[0] = 0; | 94 cachedCollator = 0; |
| 97 return; | 95 cachedEquivalentLocale[0] = 0; |
| 98 } | 96 return; |
| 99 } | 97 } |
| 100 } | 98 } |
| 99 } |
| 101 | 100 |
| 102 m_collator = ucol_open(m_locale, &status); | 101 m_collator = ucol_open(m_locale, &status); |
| 103 if (U_FAILURE(status)) { | 102 if (U_FAILURE(status)) { |
| 104 status = U_ZERO_ERROR; | 103 status = U_ZERO_ERROR; |
| 105 m_collator = ucol_open("", &status); // Fallback to Unicode Collation Al
gorithm. | 104 m_collator = |
| 106 } | 105 ucol_open("", &status); // Fallback to Unicode Collation Algorithm. |
| 107 ASSERT(U_SUCCESS(status)); | 106 } |
| 107 ASSERT(U_SUCCESS(status)); |
| 108 | 108 |
| 109 ucol_setAttribute(m_collator, UCOL_CASE_FIRST, m_lowerFirst ? UCOL_LOWER_FIR
ST : UCOL_UPPER_FIRST, &status); | 109 ucol_setAttribute(m_collator, UCOL_CASE_FIRST, |
| 110 ASSERT(U_SUCCESS(status)); | 110 m_lowerFirst ? UCOL_LOWER_FIRST : UCOL_UPPER_FIRST, |
| 111 &status); |
| 112 ASSERT(U_SUCCESS(status)); |
| 111 | 113 |
| 112 ucol_setAttribute(m_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); | 114 ucol_setAttribute(m_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); |
| 113 ASSERT(U_SUCCESS(status)); | 115 ASSERT(U_SUCCESS(status)); |
| 114 } | 116 } |
| 115 | 117 |
| 116 void Collator::releaseCollator() | 118 void Collator::releaseCollator() { |
| 117 { | 119 { |
| 118 { | 120 Locker<Mutex> lock(cachedCollatorMutex()); |
| 119 Locker<Mutex> lock(cachedCollatorMutex()); | 121 if (cachedCollator) |
| 120 if (cachedCollator) | 122 ucol_close(cachedCollator); |
| 121 ucol_close(cachedCollator); | 123 cachedCollator = m_collator; |
| 122 cachedCollator = m_collator; | 124 strncpy(cachedEquivalentLocale, m_equivalentLocale, ulocFullnameCapacity); |
| 123 strncpy(cachedEquivalentLocale, m_equivalentLocale, ulocFullnameCapacity
); | |
| 124 m_collator = 0; | |
| 125 } | |
| 126 m_collator = 0; | 125 m_collator = 0; |
| 126 } |
| 127 m_collator = 0; |
| 127 } | 128 } |
| 128 | 129 |
| 129 void Collator::setEquivalentLocale(const char* locale, char* equivalentLocale) | 130 void Collator::setEquivalentLocale(const char* locale, char* equivalentLocale) { |
| 130 { | 131 UErrorCode status = U_ZERO_ERROR; |
| 131 UErrorCode status = U_ZERO_ERROR; | 132 UBool isAvailable; |
| 132 UBool isAvailable; | 133 ucol_getFunctionalEquivalent(equivalentLocale, ulocFullnameCapacity, |
| 133 ucol_getFunctionalEquivalent(equivalentLocale, ulocFullnameCapacity, "collat
ion", locale, &isAvailable, &status); | 134 "collation", locale, &isAvailable, &status); |
| 134 if (U_FAILURE(status)) | 135 if (U_FAILURE(status)) |
| 135 strcpy(equivalentLocale, "root"); | 136 strcpy(equivalentLocale, "root"); |
| 136 } | 137 } |
| 137 | 138 |
| 138 } // namespace WTF | 139 } // namespace WTF |
| OLD | NEW |