1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 #cgo LDFLAGS: -framework CoreFoundation
11 #include <CoreFoundation/CFBase.h>
12 #include <CoreFoundation/CoreFoundation.h>
20 AddFactory(CollatorFactory{"osx", newOSX16Collator,
21 "OS X/Darwin collator, using native strings."})
22 AddFactory(CollatorFactory{"osx8", newOSX8Collator,
23 "OS X/Darwin collator for UTF-8."})
26 func osxUInt8P(s []byte) *C.UInt8 {
27 return (*C.UInt8)(unsafe.Pointer(&s[0]))
30 func osxCharP(s []uint16) *C.UniChar {
31 return (*C.UniChar)(unsafe.Pointer(&s[0]))
34 // osxCollator implements an Collator based on OS X's CoreFoundation.
35 type osxCollator struct {
37 opt C.CFStringCompareFlags
40 func (c *osxCollator) init(locale string) {
41 l := C.CFStringCreateWithBytes(
43 osxUInt8P([]byte(locale)),
44 C.CFIndex(len(locale)),
45 C.kCFStringEncodingUTF8,
48 c.loc = C.CFLocaleCreate(nil, l)
51 func newOSX8Collator(locale string) (Collator, error) {
57 func newOSX16Collator(locale string) (Collator, error) {
63 func (c osxCollator) Key(s Input) []byte {
64 return nil // sort keys not supported by OS X CoreFoundation
67 type osx8Collator struct {
71 type osx16Collator struct {
75 func (c osx16Collator) Compare(a, b Input) int {
76 sa := C.CFStringCreateWithCharactersNoCopy(
79 C.CFIndex(len(a.UTF16)),
82 sb := C.CFStringCreateWithCharactersNoCopy(
85 C.CFIndex(len(b.UTF16)),
88 _range := C.CFRangeMake(0, C.CFStringGetLength(sa))
89 return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))
92 func (c osx8Collator) Compare(a, b Input) int {
93 sa := C.CFStringCreateWithBytesNoCopy(
96 C.CFIndex(len(a.UTF8)),
97 C.kCFStringEncodingUTF8,
101 sb := C.CFStringCreateWithBytesNoCopy(
104 C.CFIndex(len(b.UTF8)),
105 C.kCFStringEncodingUTF8,
109 _range := C.CFRangeMake(0, C.CFStringGetLength(sa))
110 return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))