github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/golang.org/x/text/cases/icu.go (about)

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