2016-12-03 19:01:45 -06:00
|
|
|
// 3 december 2016
|
|
|
|
#import "uipriv_darwin.h"
|
2018-03-04 14:48:45 -06:00
|
|
|
#import "attrstr.h"
|
2016-12-03 19:01:45 -06:00
|
|
|
|
|
|
|
// CFStringGetRangeOfComposedCharactersAtIndex() is the function for grapheme clusters
|
|
|
|
// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/stringsClusters.html says that this does work on all multi-codepoint graphemes (despite the name), and that this is the preferred function for this particular job anyway
|
|
|
|
|
2018-03-04 14:48:45 -06:00
|
|
|
int uiprivGraphemesTakesUTF16(void)
|
2016-12-03 19:01:45 -06:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-15 21:43:14 -05:00
|
|
|
uiprivGraphemes *uiprivNewGraphemes(void *s, size_t len)
|
2016-12-03 19:01:45 -06:00
|
|
|
{
|
2018-04-15 21:43:14 -05:00
|
|
|
uiprivGraphemes *g;
|
2016-12-03 19:01:45 -06:00
|
|
|
UniChar *str = (UniChar *) s;
|
2017-01-17 12:05:40 -06:00
|
|
|
CFStringRef cfstr;
|
2016-12-03 19:01:45 -06:00
|
|
|
size_t ppos, gpos;
|
|
|
|
CFRange range;
|
|
|
|
size_t i;
|
|
|
|
|
2018-04-15 21:43:14 -05:00
|
|
|
g = uiprivNew(uiprivGraphemes);
|
2016-12-03 19:01:45 -06:00
|
|
|
|
|
|
|
cfstr = CFStringCreateWithCharactersNoCopy(NULL, str, len, kCFAllocatorNull);
|
|
|
|
if (cfstr == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// first figure out how many graphemes there are
|
|
|
|
g->len = 0;
|
|
|
|
ppos = 0;
|
|
|
|
while (ppos < len) {
|
|
|
|
range = CFStringGetRangeOfComposedCharactersAtIndex(cfstr, ppos);
|
|
|
|
g->len++;
|
|
|
|
ppos = range.location + range.length;
|
|
|
|
}
|
|
|
|
|
2018-03-04 14:48:45 -06:00
|
|
|
g->pointsToGraphemes = (size_t *) uiprivAlloc((len + 1) * sizeof (size_t), "size_t[] (graphemes)");
|
|
|
|
g->graphemesToPoints = (size_t *) uiprivAlloc((g->len + 1) * sizeof (size_t), "size_t[] (graphemes)");
|
2016-12-03 19:01:45 -06:00
|
|
|
|
|
|
|
// now calculate everything
|
|
|
|
// fortunately due to the use of CFRange we can do this in one loop trivially!
|
|
|
|
ppos = 0;
|
|
|
|
gpos = 0;
|
|
|
|
while (ppos < len) {
|
|
|
|
range = CFStringGetRangeOfComposedCharactersAtIndex(cfstr, ppos);
|
|
|
|
for (i = 0; i < range.length; i++)
|
|
|
|
g->pointsToGraphemes[range.location + i] = gpos;
|
|
|
|
g->graphemesToPoints[gpos] = range.location;
|
|
|
|
gpos++;
|
|
|
|
ppos = range.location + range.length;
|
|
|
|
}
|
|
|
|
// and set the last one
|
|
|
|
g->pointsToGraphemes[ppos] = gpos;
|
|
|
|
g->graphemesToPoints[gpos] = ppos;
|
|
|
|
|
|
|
|
CFRelease(cfstr);
|
|
|
|
return g;
|
|
|
|
}
|