gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

icu.go (1458B)


      1 // Copyright 2016 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 //go:build icu
      6 // +build icu
      7 
      8 package cases
      9 
     10 // Ideally these functions would be defined in a test file, but go test doesn't
     11 // allow CGO in tests. The build tag should ensure either way that these
     12 // functions will not end up in the package.
     13 
     14 // TODO: Ensure that the correct ICU version is set.
     15 
     16 /*
     17 #cgo LDFLAGS: -licui18n.57 -licuuc.57
     18 #include <stdlib.h>
     19 #include <unicode/ustring.h>
     20 #include <unicode/utypes.h>
     21 #include <unicode/localpointer.h>
     22 #include <unicode/ucasemap.h>
     23 */
     24 import "C"
     25 
     26 import "unsafe"
     27 
     28 func doICU(tag, caser, input string) string {
     29 	err := C.UErrorCode(0)
     30 	loc := C.CString(tag)
     31 	cm := C.ucasemap_open(loc, C.uint32_t(0), &err)
     32 
     33 	buf := make([]byte, len(input)*4)
     34 	dst := (*C.char)(unsafe.Pointer(&buf[0]))
     35 	src := C.CString(input)
     36 
     37 	cn := C.int32_t(0)
     38 
     39 	switch caser {
     40 	case "fold":
     41 		cn = C.ucasemap_utf8FoldCase(cm,
     42 			dst, C.int32_t(len(buf)),
     43 			src, C.int32_t(len(input)),
     44 			&err)
     45 	case "lower":
     46 		cn = C.ucasemap_utf8ToLower(cm,
     47 			dst, C.int32_t(len(buf)),
     48 			src, C.int32_t(len(input)),
     49 			&err)
     50 	case "upper":
     51 		cn = C.ucasemap_utf8ToUpper(cm,
     52 			dst, C.int32_t(len(buf)),
     53 			src, C.int32_t(len(input)),
     54 			&err)
     55 	case "title":
     56 		cn = C.ucasemap_utf8ToTitle(cm,
     57 			dst, C.int32_t(len(buf)),
     58 			src, C.int32_t(len(input)),
     59 			&err)
     60 	}
     61 	return string(buf[:cn])
     62 }