this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

UC* unicode utilities support

+250 -2
+1
src/CoreServices/MacErrors.h
··· 8 8 #define unimpErr -4 9 9 #define fnfErr -43 // file not found 10 10 #define paramErr -50 11 + #define kUCOutputBufferTooSmall -25340 11 12 12 13 #endif
+9 -2
src/CoreServices/MacLocales.cpp
··· 49 49 iconv_close(g_icUtf32ToUtf16); 50 50 } 51 51 52 - static int getLocaleUID(const std::string& str) 52 + namespace Darling 53 + { 54 + 55 + int getLocaleUID(const std::string& str) 53 56 { 54 57 auto it = g_mapLocaleString.find(str); 55 58 if (it != g_mapLocaleString.end()) ··· 67 70 } 68 71 } 69 72 70 - static const char* getLocaleString(int uid) 73 + const char* getLocaleString(int uid) 71 74 { 72 75 auto it = g_mapLocaleStringRev.find(uid); 73 76 if (it != g_mapLocaleStringRev.end()) ··· 75 78 else 76 79 return "INVALID"; 77 80 } 81 + 82 + } 83 + 84 + using namespace Darling; 78 85 79 86 OSStatus LocaleRefFromLangOrRegionCode(LangCode langCode, RegionCode regionCode, LocaleRef* refOut) 80 87 {
+188
src/CoreServices/UnicodeUtilities.cpp
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2013 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + 21 + #include "UnicodeUtilities.h" 22 + #include <unicode/coll.h> 23 + #include <unicode/sortkey.h> 24 + #include <string> 25 + #include <cstring> 26 + #include "MacErrors.h" 27 + 28 + namespace Darling 29 + { 30 + int getLocaleUID(const std::string& str); 31 + const char* getLocaleString(int uid); 32 + } 33 + 34 + static UColAttributeValue optsToColAttr(uint32_t options) 35 + { 36 + UColAttributeValue strength = UCOL_DEFAULT_STRENGTH; 37 + 38 + if (options & kUCCollateComposeInsensitiveMask) 39 + strength = UCOL_QUATERNARY; 40 + //if (options & kUCCollateWidthInsensitiveMask) 41 + if (options & kUCCollateCaseInsensitiveMask) 42 + strength = UCOL_SECONDARY; 43 + if (options & kUCCollateDiacritInsensitiveMask) 44 + strength = UCOL_PRIMARY; 45 + 46 + return strength; 47 + } 48 + 49 + OSStatus UCCreateCollator(LocaleRef locale, LocaleOperationVariant opVariant, uint32_t options, CollatorRef* collator) 50 + { 51 + UErrorCode code; 52 + Collator* c = Collator::createInstance(Darling::getLocaleString(locale), code); 53 + 54 + *collator = c; 55 + 56 + if (!c) 57 + return paramErr; 58 + 59 + c->setAttribute(UCOL_STRENGTH, optsToColAttr(options), code); 60 + return noErr; 61 + } 62 + 63 + OSStatus UCGetCollationKey(CollatorRef collator, const UniChar* text, unsigned long textlen, unsigned long maxKeySize, unsigned long* actualKeySize, uint32_t* collationKey) 64 + { 65 + if (!text || !actualKeySize || !collationKey || !collator) 66 + return paramErr; 67 + 68 + UnicodeString str1((const char*) text, textlen*2, "UTF-16"); 69 + Collator* c = static_cast<Collator*>(collator); 70 + CollationKey key; 71 + UErrorCode code; 72 + int32_t count; 73 + const uint8_t* data; 74 + 75 + *actualKeySize = 0; 76 + c->getCollationKey(str1, key, code); 77 + 78 + if (code != U_ZERO_ERROR) 79 + return -1; 80 + 81 + data = key.getByteArray(count); 82 + if (count > sizeof(uint32_t)*maxKeySize) 83 + return kUCOutputBufferTooSmall; 84 + 85 + *actualKeySize = (count + sizeof(uint32_t) - 1) / sizeof(uint32_t); 86 + memset(collationKey, 0, *actualKeySize); 87 + memcpy(collationKey, data, count); 88 + 89 + return noErr; 90 + } 91 + 92 + OSStatus UCCompareCollationKeys(const uint32_t* key1, unsigned long key1len, const uint32_t* key2, unsigned long key2len, Boolean* equiv, int32_t* order) 93 + { 94 + if (!equiv && !order) 95 + return paramErr; 96 + 97 + CollationKey ckey1((const uint8_t*)key1, key1len); 98 + CollationKey ckey2((const uint8_t*)key2, key2len); 99 + UCollationResult result; 100 + UErrorCode code; 101 + 102 + result = ckey1.compareTo(ckey2, code); 103 + 104 + if (code != U_ZERO_ERROR) 105 + return -1; 106 + 107 + if (equiv != nullptr) 108 + *equiv = result == UCOL_EQUAL; 109 + 110 + if (order != nullptr) 111 + { 112 + if (result == UCOL_GREATER) 113 + *order = 1; 114 + else if (result == UCOL_LESS) 115 + *order = -1; 116 + else 117 + *order = 0; 118 + } 119 + 120 + return noErr; 121 + } 122 + 123 + OSStatus UCCompareText(CollatorRef collator, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order) 124 + { 125 + if (!equiv && !order) 126 + return paramErr; 127 + if (!text1 || !text2 || !collator) 128 + return paramErr; 129 + 130 + UnicodeString str1((const char*) text1, text1len*2, "UTF-16"); 131 + UnicodeString str2((const char*) text2, text2len*2, "UTF-16"); 132 + Collator* c = static_cast<Collator*>(collator); 133 + UCollationResult result; 134 + UErrorCode code; 135 + 136 + result = c->compare(str1, str2, code); 137 + 138 + if (code != U_ZERO_ERROR) 139 + return -1; 140 + 141 + if (equiv != nullptr) 142 + *equiv = result == UCOL_EQUAL; 143 + 144 + if (order != nullptr) 145 + { 146 + if (result == UCOL_GREATER) 147 + *order = 1; 148 + else if (result == UCOL_LESS) 149 + *order = -1; 150 + else 151 + *order = 0; 152 + } 153 + 154 + return noErr; 155 + } 156 + 157 + OSStatus UCDisposeCollator(CollatorRef* collator) 158 + { 159 + delete static_cast<Collator*>(*collator); 160 + *collator = nullptr; 161 + return noErr; 162 + } 163 + 164 + OSStatus UCCompareTextDefault(uint32_t options, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order) 165 + { 166 + UErrorCode code; 167 + Collator* col = Collator::createInstance(code); 168 + OSStatus rv; 169 + 170 + col->setAttribute(UCOL_STRENGTH, optsToColAttr(options), code); 171 + rv = UCCompareText(col, text1, text1len, text2, text2len, equiv, order); 172 + 173 + delete col; 174 + return rv; 175 + } 176 + 177 + OSStatus UCCompareTextNoLocale(uint32_t options, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order) 178 + { 179 + UErrorCode code; 180 + Collator* col = Collator::createInstance(Locale::getRoot(), code); // is getRoot correct? 181 + OSStatus rv; 182 + 183 + col->setAttribute(UCOL_STRENGTH, optsToColAttr(options), code); 184 + rv = UCCompareText(col, text1, text1len, text2, text2len, equiv, order); 185 + 186 + delete col; 187 + return rv; 188 + }
+52
src/CoreServices/UnicodeUtilities.h
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2013 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 1 20 #ifndef UNICODEUTILITIES_H 2 21 #define UNICODEUTILITIES_H 22 + #include "MacTypes.h" 23 + #include "MacLocales.h" 24 + #include <stdint.h> 3 25 26 + extern "C" 27 + { 4 28 29 + typedef void* CollatorRef; 30 + enum : uint32_t 31 + { 32 + kUCCollateComposeInsensitiveMask = 0x2, 33 + kUCCollateWidthInsensitiveMask = 0x4, 34 + kUCCollateCaseInsensitiveMask = 0x8, 35 + kUCCollateDiacritInsensitiveMask = 0x10, 36 + kUCCollatePunctuationSignificantMask = 0x8000, 37 + kUCCollateDigitsOverrideMask = 0x10000, 38 + kUCCollateDigitsAsNumberMask = 0x20000 39 + }; 40 + 41 + 42 + OSStatus UCCreateCollator(LocaleRef locale, LocaleOperationVariant opVariant, uint32_t options, CollatorRef* collator); 43 + 44 + OSStatus UCGetCollationKey(CollatorRef collator, const UniChar * text, unsigned long textlen, unsigned long maxKeySize, unsigned long* actualKeySize, uint32_t* collationKey); 45 + 46 + OSStatus UCCompareCollationKeys(const uint32_t* key1, unsigned long key1len, const uint32_t* key2, unsigned long key2len, Boolean* equiv, int32_t* order); 47 + 48 + OSStatus UCCompareText(CollatorRef collator, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order); 49 + 50 + OSStatus UCDisposeCollator(CollatorRef* collator); 51 + 52 + OSStatus UCCompareTextDefault(uint32_t options, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order); 53 + 54 + OSStatus UCCompareTextNoLocale(uint32_t options, const UniChar* text1, unsigned long text1len, const UniChar* text2, unsigned long text2len, Boolean* equiv, int32_t* order); 55 + 56 + } 5 57 6 58 #endif