From 621e49bb465f500cc46d47e39e828cf76d6381d7 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 24 Jul 2018 14:35:44 +0200 Subject: update vendor --- .../x/text/collate/tools/colcmp/darwin.go | 111 +++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 vendor/golang.org/x/text/collate/tools/colcmp/darwin.go (limited to 'vendor/golang.org/x/text/collate/tools/colcmp/darwin.go') diff --git a/vendor/golang.org/x/text/collate/tools/colcmp/darwin.go b/vendor/golang.org/x/text/collate/tools/colcmp/darwin.go new file mode 100644 index 0000000..d2300e3 --- /dev/null +++ b/vendor/golang.org/x/text/collate/tools/colcmp/darwin.go @@ -0,0 +1,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 +#include +*/ +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)) +} -- cgit v1.2.3