Add example case for registering encodings.

fixes #583
This commit is contained in:
Garrett D'Amore 2022-12-30 12:54:50 -08:00
parent e6d83cf4c3
commit 916a717ae8
2 changed files with 38 additions and 7 deletions

View File

@ -1,4 +1,4 @@
// Copyright 2015 The TCell Authors
// Copyright 2022 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
@ -46,7 +46,7 @@ var encodingFallback EncodingFallback = EncodingFallbackFail
// Aliases can be registered as well, for example "8859-15" could be an alias
// for "ISO8859-15".
//
// For POSIX systems, the tcell package will check the environment variables
// For POSIX systems, this package will check the environment variables
// LC_ALL, LC_CTYPE, and LANG (in that order) to determine the character set.
// These are expected to have the following pattern:
//
@ -64,9 +64,12 @@ var encodingFallback EncodingFallback = EncodingFallbackFail
// quite a lot processing overhead.
//
// Note that some encodings are quite large (for example GB18030 which is a
// superset of Unicode) and so the application size can be expected ot
// increase quite a bit as each encoding is added. The East Asian encodings
// have been seen to add 100-200K per encoding to the application size.
// superset of Unicode) and so the application size can be expected to
// increase quite a bit as each encoding is added.
// The East Asian encodings have been seen to add 100-200K per encoding to the
// size of the resulting binary.
//
func RegisterEncoding(charset string, enc encoding.Encoding) {
encodingLk.Lock()
charset = strings.ToLower(charset)
@ -74,7 +77,7 @@ func RegisterEncoding(charset string, enc encoding.Encoding) {
encodingLk.Unlock()
}
// EncodingFallback describes how the system behavees when the locale
// EncodingFallback describes how the system behaves when the locale
// requires a character set that we do not support. The system always
// supports UTF-8 and US-ASCII. On Windows consoles, UTF-16LE is also
// supported automatically. Other character sets must be added using the
@ -87,7 +90,7 @@ const (
// when it cannot find an encoding.
EncodingFallbackFail = iota
// EncodingFallbackASCII behaviore causes GetEncoding to fall back
// EncodingFallbackASCII behavior causes GetEncoding to fall back
// to a 7-bit ASCII encoding, if no other encoding can be found.
EncodingFallbackASCII

28
encoding_test.go Normal file
View File

@ -0,0 +1,28 @@
// Copyright 2022 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tcell
import (
"fmt"
"golang.org/x/text/encoding/simplifiedchinese"
)
func ExampleRegisterEncoding() {
RegisterEncoding("GBK", simplifiedchinese.GBK);
enc := GetEncoding("GBK");
glyph, _ := enc.NewDecoder().Bytes([]byte{0x82, 0x74})
fmt.Println(string(glyph))
// Output: 倀
}