util.go 602 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package terminalparser
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "unicode"
  7. "unicode/utf8"
  8. )
  9. func DebugString(p string) string {
  10. var s strings.Builder
  11. for _, v := range []rune(p) {
  12. if unicode.IsPrint(v) {
  13. s.WriteRune(v)
  14. } else {
  15. s.WriteString(fmt.Sprintf("%q", v))
  16. }
  17. }
  18. return s.String()
  19. }
  20. func IsAlphabetic(r rune) bool {
  21. index := bytes.IndexRune([]byte(string(Alphabetic)), r)
  22. if index < 0 {
  23. return false
  24. }
  25. return true
  26. }
  27. func ReadRunePacket(p []byte) (code rune, rest []byte) {
  28. r, l := utf8.DecodeRune(p)
  29. if r == utf8.RuneError {
  30. return utf8.RuneError, p
  31. }
  32. return r, p[l:]
  33. }