summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/collate/tools/colcmp/darwin.go
blob: d2300e3e2131eb6c9c465e5daa163fbdfccc2f85 (plain)
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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin

package main

/*
#cgo LDFLAGS: -framework CoreFoundation
#include <CoreFoundation/CFBase.h>
#include <CoreFoundation/CoreFoundation.h>
*/
import "C"
import (
	"unsafe"
)

func init() {
	AddFactory(CollatorFactory{"osx", newOSX16Collator,
		"OS X/Darwin collator, using native strings."})
	AddFactory(CollatorFactory{"osx8", newOSX8Collator,
		"OS X/Darwin collator for UTF-8."})
}

func osxUInt8P(s []byte) *C.UInt8 {
	return (*C.UInt8)(unsafe.Pointer(&s[0]))
}

func osxCharP(s []uint16) *C.UniChar {
	return (*C.UniChar)(unsafe.Pointer(&s[0]))
}

// osxCollator implements an Collator based on OS X's CoreFoundation.
type osxCollator struct {
	loc C.CFLocaleRef
	opt C.CFStringCompareFlags
}

func (c *osxCollator) init(locale string) {
	l := C.CFStringCreateWithBytes(
		C.kCFAllocatorDefault,
		osxUInt8P([]byte(locale)),
		C.CFIndex(len(locale)),
		C.kCFStringEncodingUTF8,
		C.Boolean(0),
	)
	c.loc = C.CFLocaleCreate(C.kCFAllocatorDefault, l)
}

func newOSX8Collator(locale string) (Collator, error) {
	c := &osx8Collator{}
	c.init(locale)
	return c, nil
}

func newOSX16Collator(locale string) (Collator, error) {
	c := &osx16Collator{}
	c.init(locale)
	return c, nil
}

func (c osxCollator) Key(s Input) []byte {
	return nil // sort keys not supported by OS X CoreFoundation
}

type osx8Collator struct {
	osxCollator
}

type osx16Collator struct {
	osxCollator
}

func (c osx16Collator) Compare(a, b Input) int {
	sa := C.CFStringCreateWithCharactersNoCopy(
		C.kCFAllocatorDefault,
		osxCharP(a.UTF16),
		C.CFIndex(len(a.UTF16)),
		C.kCFAllocatorDefault,
	)
	sb := C.CFStringCreateWithCharactersNoCopy(
		C.kCFAllocatorDefault,
		osxCharP(b.UTF16),
		C.CFIndex(len(b.UTF16)),
		C.kCFAllocatorDefault,
	)
	_range := C.CFRangeMake(0, C.CFStringGetLength(sa))
	return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))
}

func (c osx8Collator) Compare(a, b Input) int {
	sa := C.CFStringCreateWithBytesNoCopy(
		C.kCFAllocatorDefault,
		osxUInt8P(a.UTF8),
		C.CFIndex(len(a.UTF8)),
		C.kCFStringEncodingUTF8,
		C.Boolean(0),
		C.kCFAllocatorDefault,
	)
	sb := C.CFStringCreateWithBytesNoCopy(
		C.kCFAllocatorDefault,
		osxUInt8P(b.UTF8),
		C.CFIndex(len(b.UTF8)),
		C.kCFStringEncodingUTF8,
		C.Boolean(0),
		C.kCFAllocatorDefault,
	)
	_range := C.CFRangeMake(0, C.CFStringGetLength(sa))
	return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))
}