-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiconv.go
48 lines (39 loc) · 1.2 KB
/
iconv.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
package iconv
// #cgo darwin LDFLAGS: -liconv
// #cgo freebsd LDFLAGS: -liconv
// #cgo windows LDFLAGS: -liconv
// #include <stdlib.h>
// char *do_iconv(char *to, char *from, char *text, int transliterate);
import "C"
import "errors"
import "unsafe"
// to is the target charset, from is the source charset text is the text to convert
// to. If transliterate is true, the function try to convert text even if the target
// charset has no corresponding char. Exemple, utf-8 "é" to ascii give "'e". If
// transliterate is false the conversion fails.
func Iconv(to string, from string, text string, transliterate bool)(string, error) {
var c_from *C.char
var c_to *C.char
var c_text *C.char
var c_out *C.char
var c_transliterate C.int
var out string
c_from = C.CString(from)
defer C.free(unsafe.Pointer(c_from))
c_to = C.CString(to)
defer C.free(unsafe.Pointer(c_to))
c_text = C.CString(text)
defer C.free(unsafe.Pointer(c_text))
if transliterate {
c_transliterate = 1
} else {
c_transliterate = 0
}
c_out = C.do_iconv(c_to, c_from, c_text, c_transliterate)
if c_out == nil {
return "", errors.New("Can't convert text")
}
out = C.GoString(c_out)
C.free(unsafe.Pointer(c_out))
return out, nil
}