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.

78 lines
1.1 KiB

10 years ago
  1. package mahonia
  2. // Converters for GBK encoding.
  3. func init() {
  4. RegisterCharset(&Charset{
  5. Name: "GBK",
  6. Aliases: []string{"GB2312"}, // GBK is a superset of GB2312.
  7. NewDecoder: func() Decoder {
  8. return decodeGBKRune
  9. },
  10. NewEncoder: func() Encoder {
  11. return encodeGBKRune
  12. },
  13. })
  14. }
  15. func decodeGBKRune(p []byte) (r rune, size int, status Status) {
  16. if len(p) == 0 {
  17. status = NO_ROOM
  18. return
  19. }
  20. b := p[0]
  21. if b < 128 {
  22. return rune(b), 1, SUCCESS
  23. }
  24. if len(p) < 2 {
  25. status = NO_ROOM
  26. return
  27. }
  28. c := uint16(p[0])<<8 + uint16(p[1])
  29. r = rune(gbkToUnicode[c])
  30. if r == 0 {
  31. r = gbkToUnicodeExtra[c]
  32. }
  33. if r != 0 {
  34. return r, 2, SUCCESS
  35. }
  36. return 0xfffd, 1, INVALID_CHAR
  37. }
  38. func encodeGBKRune(p []byte, r rune) (size int, status Status) {
  39. if len(p) == 0 {
  40. status = NO_ROOM
  41. return
  42. }
  43. if r < 128 {
  44. p[0] = byte(r)
  45. return 1, SUCCESS
  46. }
  47. if len(p) < 2 {
  48. status = NO_ROOM
  49. return
  50. }
  51. var c uint16
  52. if r < 0x10000 {
  53. c = unicodeToGBK[r]
  54. } else {
  55. c = unicodeToGBKExtra[r]
  56. }
  57. if c != 0 {
  58. p[0] = byte(c >> 8)
  59. p[1] = byte(c)
  60. return 2, SUCCESS
  61. }
  62. p[0] = 0x1a
  63. return 1, INVALID_CHAR
  64. }