OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / text / collate / tools / colcmp / darwin.go
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.
4
5 // +build darwin
6
7 package main
8
9 /*
10 #cgo LDFLAGS: -framework CoreFoundation
11 #include <CoreFoundation/CFBase.h>
12 #include <CoreFoundation/CoreFoundation.h>
13 */
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 // osxCollator implements an Collator based on OS X's CoreFoundation.
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                 nil,
43                 osxUInt8P([]byte(locale)),
44                 C.CFIndex(len(locale)),
45                 C.kCFStringEncodingUTF8,
46                 C.Boolean(0),
47         )
48         c.loc = C.CFLocaleCreate(nil, 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 // sort keys not supported by OS X CoreFoundation
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                 nil,
78                 osxCharP(a.UTF16),
79                 C.CFIndex(len(a.UTF16)),
80                 nil,
81         )
82         sb := C.CFStringCreateWithCharactersNoCopy(
83                 nil,
84                 osxCharP(b.UTF16),
85                 C.CFIndex(len(b.UTF16)),
86                 nil,
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                 nil,
95                 osxUInt8P(a.UTF8),
96                 C.CFIndex(len(a.UTF8)),
97                 C.kCFStringEncodingUTF8,
98                 C.Boolean(0),
99                 nil,
100         )
101         sb := C.CFStringCreateWithBytesNoCopy(
102                 nil,
103                 osxUInt8P(b.UTF8),
104                 C.CFIndex(len(b.UTF8)),
105                 C.kCFStringEncodingUTF8,
106                 C.Boolean(0),
107                 nil,
108         )
109         _range := C.CFRangeMake(0, C.CFStringGetLength(sa))
110         return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))
111 }