forked from jlaffaye/ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
69 lines (66 loc) · 1.61 KB
/
encoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ftp
import "unicode/utf8"
// ISO8859_15ToUTF8 converts an ISO-8859-15 string to UTF-8 encoding
func ISO8859_15ToUTF8(s string) string {
var rn rune
u := make([]rune, len(s))
for i := 0; i < len(u); i++ {
r := int(s[i])
switch r {
case 0xA4:
rn = 0x20AC // EURO SIGN
case 0xA6:
rn = 0x0160 // LATIN CAPITAL LETTER S WITH CARON
case 0xA8:
rn = 0x0161 // LATIN SMALL LETTER S WITH CARON
case 0xB4:
rn = 0x017D // LATIN CAPITAL LETTER Z WITH CARON
case 0xB8:
rn = 0x017E // LATIN SMALL LETTER Z WITH CARON
case 0xBC:
rn = 0x0152 // LATIN CAPITAL LIGATURE OE
case 0xBD:
rn = 0x0153 // LATIN SMALL LIGATURE OE
case 0xBE:
rn = 0x0178 // LATIN CAPITAL LETTER Y WITH DIAERESIS
default:
rn = rune(r)
}
u[i] = rn
}
return string(u)
}
// UTF8ToISO8859_15 converts a UTF-8 string to ISO-8859-15 encoding
func UTF8ToISO8859_15(c string) string {
var b byte
s := make([]byte, utf8.RuneCountInString(c))
si := 0
for i, w := 0, 0; i < len(c); i += w {
r, width := utf8.DecodeRuneInString(c[i:])
w = width
switch r {
case 0x20AC:
b = 0xA4 // EURO SIGN
case 0x0160:
b = 0xA6 // LATIN CAPITAL LETTER S WITH CARON
case 0x0161:
b = 0xA8 // LATIN SMALL LETTER S WITH CARON
case 0x017D:
b = 0xB4 // LATIN CAPITAL LETTER Z WITH CARON
case 0x017E:
b = 0xB8 // LATIN SMALL LETTER Z WITH CARON
case 0x0152:
b = 0xBC // LATIN CAPITAL LIGATURE OE
case 0x0153:
b = 0xBD // LATIN SMALL LIGATURE OE
case 0x0178:
b = 0xBE // LATIN CAPITAL LETTER Y WITH DIAERESIS
default:
b = byte(r)
}
s[si] = b
si++
}
//fmt.Printf("%x\n", s)
return string(s)
}