1
2
3
4
5
6
7 package main
8
9
14 import "C"
15 import (
16 "unsafe"
17 )
18
19 func init() {
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."})
24 }
25
26 func osxUInt8P(s []byte) *C.UInt8 {
27 return (*C.UInt8)(unsafe.Pointer(&s[0]))
28 }
29
30 func osxCharP(s []uint16) *C.UniChar {
31 return (*C.UniChar)(unsafe.Pointer(&s[0]))
32 }
33
34
35 type osxCollator struct {
36 loc C.CFLocaleRef
37 opt C.CFStringCompareFlags
38 }
39
40 func (c *osxCollator) init(locale string) {
41 l := C.CFStringCreateWithBytes(
42 C.kCFAllocatorDefault,
43 osxUInt8P([]byte(locale)),
44 C.CFIndex(len(locale)),
45 C.kCFStringEncodingUTF8,
46 C.Boolean(0),
47 )
48 c.loc = C.CFLocaleCreate(C.kCFAllocatorDefault, l)
49 }
50
51 func newOSX8Collator(locale string) (Collator, error) {
52 c := &osx8Collator{}
53 c.init(locale)
54 return c, nil
55 }
56
57 func newOSX16Collator(locale string) (Collator, error) {
58 c := &osx16Collator{}
59 c.init(locale)
60 return c, nil
61 }
62
63 func (c osxCollator) Key(s Input) []byte {
64 return nil
65 }
66
67 type osx8Collator struct {
68 osxCollator
69 }
70
71 type osx16Collator struct {
72 osxCollator
73 }
74
75 func (c osx16Collator) Compare(a, b Input) int {
76 sa := C.CFStringCreateWithCharactersNoCopy(
77 C.kCFAllocatorDefault,
78 osxCharP(a.UTF16),
79 C.CFIndex(len(a.UTF16)),
80 C.kCFAllocatorDefault,
81 )
82 sb := C.CFStringCreateWithCharactersNoCopy(
83 C.kCFAllocatorDefault,
84 osxCharP(b.UTF16),
85 C.CFIndex(len(b.UTF16)),
86 C.kCFAllocatorDefault,
87 )
88 _range := C.CFRangeMake(0, C.CFStringGetLength(sa))
89 return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))
90 }
91
92 func (c osx8Collator) Compare(a, b Input) int {
93 sa := C.CFStringCreateWithBytesNoCopy(
94 C.kCFAllocatorDefault,
95 osxUInt8P(a.UTF8),
96 C.CFIndex(len(a.UTF8)),
97 C.kCFStringEncodingUTF8,
98 C.Boolean(0),
99 C.kCFAllocatorDefault,
100 )
101 sb := C.CFStringCreateWithBytesNoCopy(
102 C.kCFAllocatorDefault,
103 osxUInt8P(b.UTF8),
104 C.CFIndex(len(b.UTF8)),
105 C.kCFStringEncodingUTF8,
106 C.Boolean(0),
107 C.kCFAllocatorDefault,
108 )
109 _range := C.CFRangeMake(0, C.CFStringGetLength(sa))
110 return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))
111 }
112
View as plain text