summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dfordsoft/golib/ic/ic.go
blob: 9e414e363ee671ace3ef0375714d617dd2a3a33b (plain) (blame)
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
package ic

import "log"

// Convert convert bytes from CJK or UTF-8 to UTF-8 or CJK
func Convert(from string, to string, src []byte) []byte {
	if to == "utf-8" {
		out, e := ToUTF8(from, src)
		if e == nil {
			return out
		}
		log.Printf("converting from %s to UTF-8 failed: %v", from, e)
		return src
	}

	if from == "utf-8" {
		out, e := FromUTF8(to, src)
		if e == nil {
			return out
		}
		log.Printf("converting from UTF-8 to %s failed: %v", to, e)
		return src
	}
	log.Println("only converting between CJK encodings and UTF-8 is supported")
	return src
}

// ConvertString convert string from CJK or UTF-8 to UTF-8 or CJK
func ConvertString(from string, to string, src string) string {
	return string(Convert(from, to, []byte(src)))
}