Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
5.2 kB · 193 lines
C++
at android
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194/*This file is part of Darling.
Copyright (C) 2013 Lubos Dolezel
Darling is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.
Darling is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.
You should have received a copy of the GNU General Public Licensealong with Darling. If not, see <http://www.gnu.org/licenses/>.*/
#include "UnicodeUtilities.h"#include <unicode/coll.h>#include <unicode/sortkey.h>#include <string>#include <cstring>#include "UniChar.h"#include "MacErrors.h"
namespace Darling{ int getLocaleUID(const std::string& str); const char* getLocaleString(int uid);}
static UColAttributeValue optsToColAttr(uint32_t options){ UColAttributeValue strength = UCOL_DEFAULT_STRENGTH; if (options & kUCCollateComposeInsensitiveMask) strength = UCOL_QUATERNARY; //if (options & kUCCollateWidthInsensitiveMask) if (options & kUCCollateCaseInsensitiveMask) strength = UCOL_SECONDARY; if (options & kUCCollateDiacritInsensitiveMask) strength = UCOL_PRIMARY; return strength;}
OSStatus UCCreateCollator(LocaleRef locale, LocaleOperationVariant opVariant, uint32_t options, CollatorRef* collator){ UErrorCode code = U_ZERO_ERROR; const char* localeStr = Darling::getLocaleString(locale); Collator* c = Collator::createInstance(localeStr, code); *collator = c; if (!c) return paramErr; code = U_ZERO_ERROR; c->setAttribute(UCOL_STRENGTH, optsToColAttr(options), code); return noErr;}
OSStatus UCGetCollationKey(CollatorRef collator, const UniChar* text, unsigned long textlen, unsigned long maxKeySize, unsigned long* actualKeySize, uint32_t* collationKey){ if (!text || !actualKeySize || !collationKey || !collator) return paramErr; UnicodeString str1((const char*) text, textlen*2, UNICHAR_ENCODING); Collator* c = static_cast<Collator*>(collator); CollationKey key; UErrorCode code = U_ZERO_ERROR; int32_t count; const uint8_t* data; *actualKeySize = 0; c->getCollationKey(str1, key, code); if (code != U_ZERO_ERROR) return -1; data = key.getByteArray(count); if (count > sizeof(uint32_t)*maxKeySize) return kUCOutputBufferTooSmall; *actualKeySize = (count + sizeof(uint32_t) - 1) / sizeof(uint32_t); memset(collationKey, 0, *actualKeySize); memcpy(collationKey, data, count); return noErr;}
OSStatus UCCompareCollationKeys(const uint32_t* key1, unsigned long key1len, const uint32_t* key2, unsigned long key2len, Boolean* equiv, int32_t* order){ if (!equiv && !order) return paramErr; CollationKey ckey1((const uint8_t*)key1, key1len); CollationKey ckey2((const uint8_t*)key2, key2len); UCollationResult result; UErrorCode code = U_ZERO_ERROR; result = ckey1.compareTo(ckey2, code); if (code != U_ZERO_ERROR) return -1; if (equiv != nullptr) *equiv = result == UCOL_EQUAL; if (order != nullptr) { if (result == UCOL_GREATER) *order = 1; else if (result == UCOL_LESS) *order = -1; else *order = 0; } return noErr;}
OSStatus UCCompareText(CollatorRef collator, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order){ if (!equiv && !order) return paramErr; if (!text1 || !text2 || !collator) return paramErr; UnicodeString str1((const char*) text1, text1len*2, UNICHAR_ENCODING); UnicodeString str2((const char*) text2, text2len*2, UNICHAR_ENCODING); Collator* c = static_cast<Collator*>(collator); UCollationResult result; UErrorCode code = U_ZERO_ERROR; result = c->compare(str1, str2, code); if (code != U_ZERO_ERROR) return -1; if (equiv != nullptr) *equiv = (result == UCOL_EQUAL); if (order != nullptr) { if (result == UCOL_GREATER) *order = 1; else if (result == UCOL_LESS) *order = -1; else *order = 0; } return noErr;}
OSStatus UCDisposeCollator(CollatorRef* collator){ delete static_cast<Collator*>(*collator); *collator = nullptr; return noErr;}
OSStatus UCCompareTextDefault(uint32_t options, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order){ UErrorCode code = U_ZERO_ERROR; Collator* col = Collator::createInstance(code); OSStatus rv; code = U_ZERO_ERROR; col->setAttribute(UCOL_STRENGTH, optsToColAttr(options), code); rv = UCCompareText(col, text1, text1len, text2, text2len, equiv, order); delete col; return rv;}
OSStatus UCCompareTextNoLocale(uint32_t options, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order){ UErrorCode code = U_ZERO_ERROR; Collator* col = Collator::createInstance(Locale::getRoot(), code); // is getRoot correct? OSStatus rv; code = U_ZERO_ERROR; col->setAttribute(UCOL_STRENGTH, optsToColAttr(options), code); rv = UCCompareText(col, text1, text1len, text2, text2len, equiv, order); delete col; return rv;}