You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.9 KiB

2 years ago
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "github.com/mozillazg/go-pinyin"
  6. "golang.org/x/text/encoding/simplifiedchinese"
  7. "golang.org/x/text/transform"
  8. "io/ioutil"
  9. "strconv"
  10. "strings"
  11. "unsafe"
  12. )
  13. // 获取中文字符串第一个首字母
  14. func GetChineseFirstLetter(chinese string) string {
  15. // 获取中文字符串第一个字符
  16. firstChar := string([]rune(chinese)[:1])
  17. // Utf8 转 GBK2312
  18. firstCharGbk, err := Utf8ToGbk([]byte(firstChar))
  19. if err != nil {
  20. return ""
  21. }
  22. // 获取第一个字符的16进制
  23. firstCharHex := hex.EncodeToString(firstCharGbk)
  24. // 16进制转十进制
  25. firstCharDec, err := strconv.ParseInt(firstCharHex, 16, 0)
  26. if err != nil {
  27. return ""
  28. }
  29. // 十进制落在GB 2312的某个拼音区间即为某个字母
  30. firstCharDecimalRelative := firstCharDec - 65536
  31. if firstCharDecimalRelative >= -20319 && firstCharDecimalRelative <= -20284 {
  32. return "A"
  33. }
  34. if firstCharDecimalRelative >= -20283 && firstCharDecimalRelative <= -19776 {
  35. return "B"
  36. }
  37. if firstCharDecimalRelative >= -19775 && firstCharDecimalRelative <= -19219 {
  38. return "C"
  39. }
  40. if firstCharDecimalRelative >= -19218 && firstCharDecimalRelative <= -18711 {
  41. return "D"
  42. }
  43. if firstCharDecimalRelative >= -18710 && firstCharDecimalRelative <= -18527 {
  44. return "E"
  45. }
  46. if firstCharDecimalRelative >= -18526 && firstCharDecimalRelative <= -18240 {
  47. return "F"
  48. }
  49. if firstCharDecimalRelative >= -18239 && firstCharDecimalRelative <= -17923 {
  50. return "G"
  51. }
  52. if firstCharDecimalRelative >= -17922 && firstCharDecimalRelative <= -17418 {
  53. return "H"
  54. }
  55. if firstCharDecimalRelative >= -17417 && firstCharDecimalRelative <= -16475 {
  56. return "J"
  57. }
  58. if firstCharDecimalRelative >= -16474 && firstCharDecimalRelative <= -16213 {
  59. return "K"
  60. }
  61. if firstCharDecimalRelative >= -16212 && firstCharDecimalRelative <= -15641 {
  62. return "L"
  63. }
  64. if firstCharDecimalRelative >= -15640 && firstCharDecimalRelative <= -15166 {
  65. return "M"
  66. }
  67. if firstCharDecimalRelative >= -15165 && firstCharDecimalRelative <= -14923 {
  68. return "N"
  69. }
  70. if firstCharDecimalRelative >= -14922 && firstCharDecimalRelative <= -14915 {
  71. return "O"
  72. }
  73. if firstCharDecimalRelative >= -14914 && firstCharDecimalRelative <= -14631 {
  74. return "P"
  75. }
  76. if firstCharDecimalRelative >= -14630 && firstCharDecimalRelative <= -14150 {
  77. return "Q"
  78. }
  79. if firstCharDecimalRelative >= -14149 && firstCharDecimalRelative <= -14091 {
  80. return "R"
  81. }
  82. if firstCharDecimalRelative >= -14090 && firstCharDecimalRelative <= -13319 {
  83. return "S"
  84. }
  85. if firstCharDecimalRelative >= -13318 && firstCharDecimalRelative <= -12839 {
  86. return "T"
  87. }
  88. if firstCharDecimalRelative >= -12838 && firstCharDecimalRelative <= -12557 {
  89. return "W"
  90. }
  91. if firstCharDecimalRelative >= -12556 && firstCharDecimalRelative <= -11848 {
  92. return "X"
  93. }
  94. if firstCharDecimalRelative >= -11847 && firstCharDecimalRelative <= -11056 {
  95. return "Y"
  96. }
  97. if firstCharDecimalRelative >= -11055 && firstCharDecimalRelative <= -10247 {
  98. return "Z"
  99. }
  100. return ""
  101. }
  102. // Utf8ToGbk
  103. func Utf8ToGbk(s []byte) ([]byte, error) {
  104. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
  105. d, e := ioutil.ReadAll(reader)
  106. if e != nil {
  107. return nil, e
  108. }
  109. return d, nil
  110. }
  111. func CharacterUpper(character string) (ch string) {
  112. anyCharacter := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  113. if len(character) > 0 {
  114. if strings.ContainsAny(character, anyCharacter) {
  115. ch = character[0:1]
  116. ch = strings.ToUpper(ch)
  117. return
  118. }
  119. }
  120. return
  121. }
  122. func HanZi2UpperCapital(name string) string {
  123. a := pinyin.NewArgs()
  124. list := pinyin.Pinyin(name, a)
  125. upStrs := ""
  126. for i := 0; i < len(list); i++ {
  127. character := str2bytes(list[i][0])[0]
  128. str := string(character)
  129. upStrs += str
  130. }
  131. return strings.ToUpper(upStrs)
  132. }
  133. func str2bytes(s string) []byte {
  134. x := (*[2]uintptr)(unsafe.Pointer(&s))
  135. h := [3]uintptr{x[0], x[1], x[1]}
  136. return *(*[]byte)(unsafe.Pointer(&h))
  137. }
  138. func bytes2str(b []byte) string {
  139. return *(*string)(unsafe.Pointer(&b))
  140. }