i18n.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package stringutils2
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "golang.org/x/text/encoding/simplifiedchinese"
  19. "golang.org/x/text/transform"
  20. )
  21. func IsUtf8(str string) bool {
  22. for _, runeVal := range str {
  23. if runeVal > 0x7f {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. func RemoveUtf8Strings(idOrNames []string) []string {
  30. ids := make([]string, 0)
  31. for _, idOrName := range idOrNames {
  32. if !IsUtf8(idOrName) {
  33. ids = append(ids, idOrName)
  34. }
  35. }
  36. return ids
  37. }
  38. func IsPrintableAscii(b byte) bool {
  39. if b >= 32 && b <= 126 {
  40. return true
  41. }
  42. return false
  43. }
  44. func IsPrintableAsciiString(str string) bool {
  45. for _, b := range []byte(str) {
  46. if !IsPrintableAscii(b) {
  47. return false
  48. }
  49. }
  50. return true
  51. }
  52. func UTF82GB18030(s []byte) ([]byte, error) {
  53. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GB18030.NewEncoder())
  54. d, e := ioutil.ReadAll(reader)
  55. if e != nil {
  56. return nil, e
  57. }
  58. return d, nil
  59. }