1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* CFStringUtilities.c
Copyright (C) 2011 Free Software Foundation, Inc.
Written by: Stefan Bidigaray
Date: May, 2011
This file is part of GNUstep CoreBase Library.
This library is free software; you can redisibute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is disibuted in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "CoreFoundation/CFBase.h"
#include "CoreFoundation/CFArray.h"
#include "CoreFoundation/CFCharacterSet.h"
#include "CoreFoundation/CFLocale.h"
#include "CoreFoundation/CFString.h"
#include "GSPrivate.h"
#if defined(HAVE_UNICODE_UCOL_H)
#include <unicode/ucol.h>
#endif
#if defined(HAVE_UNICODE_ULOC_H)
#include <unicode/uloc.h>
#endif
#if defined(HAVE_UNICODE_USEARCH_H)
#include <unicode/usearch.h>
#endif
#if defined(HAVE_ICU_H)
#include <icu.h>
#endif
CF_INLINE UCollator *
CFStringICUCollatorOpen (CFStringCompareFlags options, CFLocaleRef loc)
{
const char *cLocale;
char buffer[ULOC_FULLNAME_CAPACITY];
UCollator *ret;
UErrorCode err = U_ZERO_ERROR;
if (loc != NULL && (options & kCFCompareLocalized))
cLocale = CFLocaleGetCStringIdentifier (loc, buffer, ULOC_FULLNAME_CAPACITY);
else
cLocale = NULL;
ret = ucol_open (cLocale, &err);
if (options)
{
if (options & kCFCompareCaseInsensitive)
ucol_setAttribute (ret, UCOL_CASE_LEVEL, UCOL_OFF, &err);
if (options & kCFCompareNonliteral)
ucol_setAttribute (ret, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &err);
if (options & kCFCompareNumerically)
ucol_setAttribute (ret, UCOL_NUMERIC_COLLATION, UCOL_ON, &err);
if (options & kCFCompareDiacriticInsensitive)
ucol_setAttribute (ret, UCOL_NORMALIZATION_MODE, UCOL_ON, &err);
/* FIXME if (compareOptions & kCFCompareWidthInsensitive)
*/
if (options & kCFCompareForcedOrdering)
ucol_setAttribute (ret, UCOL_STRENGTH, UCOL_IDENTICAL, &err);
}
return ret;
}
CF_INLINE void
CFStringICUCollatorClose (UCollator *collator)
{
ucol_close (collator);
}
CFRange
CFStringFind (CFStringRef str, CFStringRef stringToFind,
CFStringCompareFlags compareOptions)
{
CFRange ret;
CFIndex len = CFStringGetLength (str);
if (!CFStringFindWithOptionsAndLocale (str, stringToFind,
CFRangeMake(0, len), compareOptions, NULL, &ret))
ret = CFRangeMake (kCFNotFound, 0);
return ret;
}
Boolean
CFStringFindWithOptions (CFStringRef str, CFStringRef stringToFind,
CFRange rangeToSearch,
CFStringCompareFlags searchOptions, CFRange *result)
{
return CFStringFindWithOptionsAndLocale (str, stringToFind,
rangeToSearch, searchOptions, NULL, result);
}
Boolean
CFStringFindWithOptionsAndLocale (CFStringRef str,
CFStringRef stringToFind,
CFRange rangeToSearch,
CFStringCompareFlags searchOptions,
CFLocaleRef locale, CFRange *result)
{
UniChar *pattern;
UniChar *text;
CFIndex patternLength;
CFIndex textLength;
CFIndex start;
CFIndex end;
CFAllocatorRef alloc;
UCollator *ucol;
UStringSearch *usrch;
UErrorCode err = U_ZERO_ERROR;
if (rangeToSearch.length == 0)
return false;
alloc = CFAllocatorGetDefault ();
textLength = CFStringGetLength (stringToFind);
if (textLength == 0)
return false;
patternLength = rangeToSearch.length;
pattern = CFAllocatorAllocate (alloc, patternLength * sizeof(UniChar), 0);
CFStringGetCharacters (str, rangeToSearch, pattern);
text = CFAllocatorAllocate (alloc, textLength * sizeof(UniChar), 0);
CFStringGetCharacters (stringToFind, CFRangeMake(0, textLength), text);
ucol = CFStringICUCollatorOpen (searchOptions, locale);
usrch = usearch_openFromCollator (text, textLength, pattern, patternLength,
ucol, NULL, &err);
if (U_FAILURE(err))
return false;
/* FIXME: need to handle kCFCompareAnchored */
if (searchOptions & kCFCompareBackwards)
{
start = usearch_last (usrch, &err);
}
else
{
start = usearch_first (usrch, &err);
}
if (start == USEARCH_DONE)
{
CFAllocatorDeallocate (alloc, pattern);
CFAllocatorDeallocate (alloc, text);
return false;
}
end = usearch_getMatchedLength (usrch);
usearch_close (usrch);
CFStringICUCollatorClose (ucol);
if (result)
*result = CFRangeMake (start + rangeToSearch.location, end);
CFAllocatorDeallocate (alloc, pattern);
CFAllocatorDeallocate (alloc, text);
return true;
}
Boolean
CFStringHasPrefix (CFStringRef str, CFStringRef prefix)
{
CFIndex len = CFStringGetLength (str);
return CFStringFindWithOptionsAndLocale (str, prefix, CFRangeMake(0, len),
kCFCompareAnchored, NULL, NULL);
}
Boolean
CFStringHasSuffix (CFStringRef str, CFStringRef suffix)
{
CFIndex len = CFStringGetLength (str);
return CFStringFindWithOptionsAndLocale (str, suffix, CFRangeMake(0, len),
kCFCompareBackwards | kCFCompareAnchored, NULL, NULL);
}
CFComparisonResult
CFStringCompare (CFStringRef str1, CFStringRef str2,
CFStringCompareFlags compareOptions)
{
CFIndex len = CFStringGetLength (str1);
return CFStringCompareWithOptionsAndLocale (str1, str2, CFRangeMake(0, len),
compareOptions, NULL);
}
CFComparisonResult
CFStringCompareWithOptions (CFStringRef str1, CFStringRef str2,
CFRange rangeToCompare, CFStringCompareFlags compareOptions)
{
return CFStringCompareWithOptionsAndLocale (str1, str2, rangeToCompare,
compareOptions, NULL);
}
CFComparisonResult
CFStringCompareWithOptionsAndLocale (CFStringRef str1,
CFStringRef str2, CFRange rangeToCompare,
CFStringCompareFlags compareOptions, CFLocaleRef locale)
{
CFComparisonResult ret;
UniChar *string1;
UniChar *string2;
CFIndex length1;
CFIndex length2;
CFAllocatorRef alloc;
UCollator *ucol;
alloc = CFAllocatorGetDefault ();
length1 = rangeToCompare.length;
string1 = CFAllocatorAllocate (alloc, (length1) * sizeof(UniChar), 0);
CFStringGetCharacters (str1, rangeToCompare, string1);
length2 = CFStringGetLength (str2);
string2 = CFAllocatorAllocate (alloc, (length2) * sizeof(UniChar), 0);
CFStringGetCharacters (str2, CFRangeMake(0, length2), string2);
ucol = CFStringICUCollatorOpen (compareOptions, locale);
ret = (CFComparisonResult)ucol_strcoll (ucol, string2, length2, string1,
length1);
CFStringICUCollatorClose (ucol);
CFAllocatorDeallocate (alloc, string1);
CFAllocatorDeallocate (alloc, string2);
return ret;
}
Boolean
CFStringFindCharacterFromSet (CFStringRef str, CFCharacterSetRef theSet,
CFRange rangeToSearch, CFStringCompareFlags searchOptions, CFRange *result)
{
/* FIXME: Not really sure how to get this done. Input is welcome. */
return false;
}
CFStringRef
CFStringCreateByCombiningStrings (CFAllocatorRef alloc, CFArrayRef theArray,
CFStringRef separatorString)
{
CFIndex idx;
CFIndex count;
CFMutableStringRef string;
CFStringRef currentString;
CFStringRef ret;
count = CFArrayGetCount (theArray) - 1;
if (count == 0)
return NULL;
string = CFStringCreateMutable (NULL, 0);
idx = 0;
while (idx < count)
{
currentString = (CFStringRef)CFArrayGetValueAtIndex (theArray, idx++);
CFStringAppend (string, currentString);
CFStringAppend (string, separatorString);
}
currentString = CFArrayGetValueAtIndex (theArray, idx);
CFStringAppend (string, currentString);
ret = CFStringCreateCopy (alloc, string);
CFRelease (string);
return ret;
}
CFArrayRef
CFStringCreateArrayBySeparatingStrings (CFAllocatorRef alloc,
CFStringRef str, CFStringRef separator)
{
/* This is basically a port of -componentsSeparatedByString: */
CFIndex end;
CFRange search;
CFRange found;
CFStringRef tmp;
CFArrayRef ret;
CFMutableArrayRef array;
array = CFArrayCreateMutable (alloc, 0, &kCFTypeArrayCallBacks);
search = CFRangeMake (0, CFStringGetLength(str));
end = search.length;
while (CFStringFindWithOptions(str, separator, search, 0, &found))
{
CFRange current;
current = CFRangeMake (search.location, found.location - search.location);
tmp = CFStringCreateWithSubstring(alloc, str, current);
CFArrayAppendValue (array, tmp);
CFRelease (tmp);
search = CFRangeMake (found.location + found.length,
end - found.location - found.length);
}
/* Add the last search string range */
tmp = CFStringCreateWithSubstring(alloc, str, search);
CFArrayAppendValue (array, tmp);
CFRelease (tmp);
ret = CFArrayCreateCopy (alloc, array);
CFRelease(array);
return ret;
}
CFArrayRef
CFStringCreateArrayWithFindResults (CFAllocatorRef alloc,
CFStringRef str, CFStringRef stringToFind, CFRange rangeToSearch,
CFStringCompareFlags compOpt)
{
return NULL; /* FIXME */
}
/* These next two functions should be very similar. According to Apple's
documentation the only different between the two is that ...ParagraphBounds
does not stop at Unicode NextLine or LineSeparactor characters.
They can probably be implemented using ICU's break iterator.
*/
void
CFStringGetLineBounds (CFStringRef str, CFRange range,
CFIndex *lineBeginIndex, CFIndex *lineEndIndex, CFIndex *contentsEndIndex)
{
return;
}
void
CFStringGetParagraphBounds (CFStringRef string, CFRange range,
CFIndex *parBeginIndex, CFIndex *parEndIndex, CFIndex *contentsEndIndex)
{
return;
}
|