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.

76 lines
1.2 KiB

10 years ago
  1. package mahonia
  2. import (
  3. "unicode/utf8"
  4. )
  5. // Converters for Microsoft's version of the EUC-JP encoding
  6. func init() {
  7. RegisterCharset(&Charset{
  8. Name: "cp51932",
  9. Aliases: []string{"windows-51932"},
  10. NewDecoder: func() Decoder {
  11. return decodeCP51932
  12. },
  13. NewEncoder: func() Encoder {
  14. msJISTable.Reverse()
  15. return encodeCP51932
  16. },
  17. })
  18. }
  19. func decodeCP51932(p []byte) (c rune, size int, status Status) {
  20. if len(p) == 0 {
  21. return 0, 0, NO_ROOM
  22. }
  23. b := p[0]
  24. switch {
  25. case b < 0x80:
  26. return rune(b), 1, SUCCESS
  27. case b == 0x8e:
  28. if len(p) < 2 {
  29. return 0, 0, NO_ROOM
  30. }
  31. b2 := p[1]
  32. if b2 < 0xa1 || b2 > 0xdf {
  33. return utf8.RuneError, 1, INVALID_CHAR
  34. }
  35. return rune(b2) + (0xff61 - 0xa1), 2, SUCCESS
  36. case 0xa1 <= b && b <= 0xfe:
  37. return msJISTable.DecodeHigh(p)
  38. }
  39. return utf8.RuneError, 1, INVALID_CHAR
  40. }
  41. func encodeCP51932(p []byte, c rune) (size int, status Status) {
  42. if len(p) == 0 {
  43. return 0, NO_ROOM
  44. }
  45. if c < 0x80 {
  46. p[0] = byte(c)
  47. return 1, SUCCESS
  48. }
  49. if len(p) < 2 {
  50. return 0, NO_ROOM
  51. }
  52. if c > 0xffff {
  53. p[0] = '?'
  54. return 1, INVALID_CHAR
  55. }
  56. if 0xff61 <= c && c <= 0xff9f {
  57. p[0] = 0x8e
  58. p[1] = byte(c - (0xff61 - 0xa1))
  59. return 2, SUCCESS
  60. }
  61. return msJISTable.EncodeHigh(p, c)
  62. }