icu.go 1.4 KB

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