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.

45 lines
736 B

10 years ago
  1. package mahonia
  2. import "unicode/utf8"
  3. func init() {
  4. RegisterCharset(&Charset{
  5. Name: "UTF-8",
  6. NewDecoder: func() Decoder { return decodeUTF8Rune },
  7. NewEncoder: func() Encoder { return encodeUTF8Rune },
  8. })
  9. }
  10. func decodeUTF8Rune(p []byte) (c rune, size int, status Status) {
  11. if len(p) == 0 {
  12. status = NO_ROOM
  13. return
  14. }
  15. if p[0] < 128 {
  16. return rune(p[0]), 1, SUCCESS
  17. }
  18. c, size = utf8.DecodeRune(p)
  19. if c == 0xfffd {
  20. if utf8.FullRune(p) {
  21. status = INVALID_CHAR
  22. return
  23. }
  24. return 0, 0, NO_ROOM
  25. }
  26. status = SUCCESS
  27. return
  28. }
  29. func encodeUTF8Rune(p []byte, c rune) (size int, status Status) {
  30. size = utf8.RuneLen(c)
  31. if size > len(p) {
  32. return 0, NO_ROOM
  33. }
  34. return utf8.EncodeRune(p, c), SUCCESS
  35. }