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.5 KiB

10 years ago
  1. package mahonia
  2. // Converters for ASCII and ISO-8859-1
  3. func init() {
  4. for i := 0; i < len(asciiCharsets); i++ {
  5. RegisterCharset(&asciiCharsets[i])
  6. }
  7. }
  8. var asciiCharsets = []Charset{
  9. {
  10. Name: "US-ASCII",
  11. NewDecoder: func() Decoder { return decodeASCIIRune },
  12. NewEncoder: func() Encoder { return encodeASCIIRune },
  13. Aliases: []string{"ASCII", "US", "ISO646-US", "IBM367", "cp367", "ANSI_X3.4-1968", "iso-ir-6", "ANSI_X3.4-1986", "ISO_646.irv:1991", "csASCII"},
  14. },
  15. {
  16. Name: "ISO-8859-1",
  17. NewDecoder: func() Decoder { return decodeLatin1Rune },
  18. NewEncoder: func() Encoder { return encodeLatin1Rune },
  19. Aliases: []string{"latin1", "ISO Latin 1", "IBM819", "cp819", "ISO_8859-1:1987", "iso-ir-100", "l1", "csISOLatin1"},
  20. },
  21. }
  22. func decodeASCIIRune(p []byte) (c rune, size int, status Status) {
  23. if len(p) == 0 {
  24. status = NO_ROOM
  25. return
  26. }
  27. b := p[0]
  28. if b > 127 {
  29. return 0xfffd, 1, INVALID_CHAR
  30. }
  31. return rune(b), 1, SUCCESS
  32. }
  33. func encodeASCIIRune(p []byte, c rune) (size int, status Status) {
  34. if len(p) == 0 {
  35. status = NO_ROOM
  36. return
  37. }
  38. if c < 128 {
  39. p[0] = byte(c)
  40. return 1, SUCCESS
  41. }
  42. p[0] = '?'
  43. return 1, INVALID_CHAR
  44. }
  45. func decodeLatin1Rune(p []byte) (c rune, size int, status Status) {
  46. if len(p) == 0 {
  47. status = NO_ROOM
  48. return
  49. }
  50. return rune(p[0]), 1, SUCCESS
  51. }
  52. func encodeLatin1Rune(p []byte, c rune) (size int, status Status) {
  53. if len(p) == 0 {
  54. status = NO_ROOM
  55. return
  56. }
  57. if c < 256 {
  58. p[0] = byte(c)
  59. return 1, SUCCESS
  60. }
  61. p[0] = '?'
  62. return 1, INVALID_CHAR
  63. }