icu.go 1.4 KB

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