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