Path: blob/main/vendor/golang.org/x/text/cases/icu.go
2880 views
// Copyright 2016 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34//go:build icu56package cases78// Ideally these functions would be defined in a test file, but go test doesn't9// allow CGO in tests. The build tag should ensure either way that these10// functions will not end up in the package.1112// TODO: Ensure that the correct ICU version is set.1314/*15#cgo LDFLAGS: -licui18n.57 -licuuc.5716#include <stdlib.h>17#include <unicode/ustring.h>18#include <unicode/utypes.h>19#include <unicode/localpointer.h>20#include <unicode/ucasemap.h>21*/22import "C"2324import "unsafe"2526func doICU(tag, caser, input string) string {27err := C.UErrorCode(0)28loc := C.CString(tag)29cm := C.ucasemap_open(loc, C.uint32_t(0), &err)3031buf := make([]byte, len(input)*4)32dst := (*C.char)(unsafe.Pointer(&buf[0]))33src := C.CString(input)3435cn := C.int32_t(0)3637switch caser {38case "fold":39cn = C.ucasemap_utf8FoldCase(cm,40dst, C.int32_t(len(buf)),41src, C.int32_t(len(input)),42&err)43case "lower":44cn = C.ucasemap_utf8ToLower(cm,45dst, C.int32_t(len(buf)),46src, C.int32_t(len(input)),47&err)48case "upper":49cn = C.ucasemap_utf8ToUpper(cm,50dst, C.int32_t(len(buf)),51src, C.int32_t(len(input)),52&err)53case "title":54cn = C.ucasemap_utf8ToTitle(cm,55dst, C.int32_t(len(buf)),56src, C.int32_t(len(input)),57&err)58}59return string(buf[:cn])60}616263