gstr_convert.go 757 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gstr
  7. import (
  8. "regexp"
  9. "strconv"
  10. )
  11. var (
  12. // octReg is the regular expression object for checks octal string.
  13. octReg = regexp.MustCompile(`\\[0-7]{3}`)
  14. )
  15. // OctStr converts string container octal string to its original string,
  16. // for example, to Chinese string.
  17. // Eg: `\346\200\241` -> 怡
  18. func OctStr(str string) string {
  19. return octReg.ReplaceAllStringFunc(
  20. str,
  21. func(s string) string {
  22. i, _ := strconv.ParseInt(s[1:], 8, 0)
  23. return string([]byte{byte(i)})
  24. },
  25. )
  26. }