github.com/Ptt-official-app/go-bbs@v0.12.0/big5.go (about) 1 // Copyright 2020 Pichu Chen, The PTT APP Authors 2 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 7 // http://www.apache.org/licenses/LICENSE-2.0 8 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Encoding convertion for Big5 to UTF-8, 16 // Actual BBS using "BIG5-UAO", so BBS in Taiwan supports Japanese 17 // charset, and golang traditionalchinese supports BIG5-UAO already. 18 // Please see "big5_test.go" for more infomation. 19 20 package bbs 21 22 import ( 23 "golang.org/x/text/encoding/traditionalchinese" 24 "golang.org/x/text/transform" 25 ) 26 27 func Utf8ToBig5(input string) []byte { 28 utf8ToBig5 := traditionalchinese.Big5.NewEncoder() 29 big5, _, _ := transform.Bytes(utf8ToBig5, []byte(input)) 30 return big5 31 } 32 33 func Big5ToUtf8(input []byte) string { 34 big5ToUTF8 := traditionalchinese.Big5.NewDecoder() 35 utf8, _, _ := transform.String(big5ToUTF8, string(input)) 36 return utf8 37 }