pax_global_header00006660000000000000000000000064134063365400014516gustar00rootroot0000000000000052 comment=6289cdc94c00ac4aa177771c5fce7af2f96b626d encoding-1.0.0/000077500000000000000000000000001340633654000133025ustar00rootroot00000000000000encoding-1.0.0/.appveyor.yml000066400000000000000000000004261340633654000157520ustar00rootroot00000000000000version: 1.0.{build} clone_folder: c:\gopath\src\github.com\gdamore\encoding environment: GOPATH: c:\gopath build_script: - go version - go env - SET PATH=%LOCALAPPDATA%\atom\bin;%GOPATH%\bin;%PATH% - go get -t ./... - go build - go install ./... test_script: - go test ./... encoding-1.0.0/.travis.yml000066400000000000000000000000721340633654000154120ustar00rootroot00000000000000language: go go: - 1.9.x - 1.10.x - 1.11.x - tip encoding-1.0.0/LICENSE000066400000000000000000000261361340633654000143170ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this 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. encoding-1.0.0/README.md000066400000000000000000000022741340633654000145660ustar00rootroot00000000000000## encoding [![Linux Status](https://img.shields.io/travis/gdamore/encoding.svg?label=linux)](https://travis-ci.org/gdamore/encoding) [![Windows Status](https://img.shields.io/appveyor/ci/gdamore/encoding.svg?label=windows)](https://ci.appveyor.com/project/gdamore/encoding) [![Apache License](https://img.shields.io/badge/license-APACHE2-blue.svg)](https://github.com/gdamore/encoding/blob/master/LICENSE) [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/gdamore/encoding) [![Go Report Card](http://goreportcard.com/badge/gdamore/encoding)](http://goreportcard.com/report/gdamore/encoding) Package encoding provides a number of encodings that are missing from the standard Go [encoding]("https://godoc.org/golang.org/x/text/encoding") package. We hope that we can contribute these to the standard Go library someday. It turns out that some of these are useful for dealing with I/O streams coming from non-UTF friendly sources. The UTF8 Encoder is also useful for situations where valid UTF-8 might be carried in streams that contain non-valid UTF; in particular I use it for helping me cope with terminals that embed escape sequences in otherwise valid UTF-8. encoding-1.0.0/ascii.go000066400000000000000000000021301340633654000147150ustar00rootroot00000000000000// Copyright 2015 Garrett D'Amore // // 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 encoding import ( "golang.org/x/text/encoding" ) // ASCII represents the 7-bit US-ASCII scheme. It decodes directly to // UTF-8 without change, as all ASCII values are legal UTF-8. // Unicode values less than 128 (i.e. 7 bits) map 1:1 with ASCII. // It encodes runes outside of that to 0x1A, the ASCII substitution character. var ASCII encoding.Encoding func init() { amap := make(map[byte]rune) for i := 128; i <= 255; i++ { amap[byte(i)] = RuneError } cm := &Charmap{Map: amap} cm.Init() ASCII = cm } encoding-1.0.0/ascii_test.go000066400000000000000000000021071340633654000157600ustar00rootroot00000000000000// Copyright 2018 Garrett D'Amore // // 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 encoding import ( "testing" ) func TestASCII(t *testing.T) { t.Logf("ASCII identity transforms") for i := 0; i < 128; i++ { verifyMap(t, ASCII, byte(i), rune(i)) } t.Logf("High order bytes map to RuneError") for i := 128; i < 256; i++ { verifyToUTF(t, ASCII, byte(i), RuneError) } t.Logf("High order UTF maps to ASCIISub") for i := 128; i < 256; i++ { verifyFromUTF(t, ASCII, ASCIISub, rune(i)) } t.Logf("Large UTF maps to ASCIISub") verifyFromUTF(t, ASCII, ASCIISub, '㿿') } encoding-1.0.0/charmap.go000066400000000000000000000130241340633654000152440ustar00rootroot00000000000000// Copyright 2015 Garrett D'Amore // // 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 encoding import ( "sync" "unicode/utf8" "golang.org/x/text/encoding" "golang.org/x/text/transform" ) const ( // RuneError is an alias for the UTF-8 replacement rune, '\uFFFD'. RuneError = '\uFFFD' // RuneSelf is the rune below which UTF-8 and the Unicode values are // identical. Its also the limit for ASCII. RuneSelf = 0x80 // ASCIISub is the ASCII substitution character. ASCIISub = '\x1a' ) // Charmap is a structure for setting up encodings for 8-bit character sets, // for transforming between UTF8 and that other character set. It has some // ideas borrowed from golang.org/x/text/encoding/charmap, but it uses a // different implementation. This implementation uses maps, and supports // user-defined maps. // // We do assume that a character map has a reasonable substitution character, // and that valid encodings are stable (exactly a 1:1 map) and stateless // (that is there is no shift character or anything like that.) Hence this // approach will not work for many East Asian character sets. // // Measurement shows little or no measurable difference in the performance of // the two approaches. The difference was down to a couple of nsec/op, and // no consistent pattern as to which ran faster. With the conversion to // UTF-8 the code takes about 25 nsec/op. The conversion in the reverse // direction takes about 100 nsec/op. (The larger cost for conversion // from UTF-8 is most likely due to the need to convert the UTF-8 byte stream // to a rune before conversion. // type Charmap struct { transform.NopResetter bytes map[rune]byte runes [256][]byte once sync.Once // The map between bytes and runes. To indicate that a specific // byte value is invalid for a charcter set, use the rune // utf8.RuneError. Values that are absent from this map will // be assumed to have the identity mapping -- that is the default // is to assume ISO8859-1, where all 8-bit characters have the same // numeric value as their Unicode runes. (Not to be confused with // the UTF-8 values, which *will* be different for non-ASCII runes.) // // If no values less than RuneSelf are changed (or have non-identity // mappings), then the character set is assumed to be an ASCII // superset, and certain assumptions and optimizations become // available for ASCII bytes. Map map[byte]rune // The ReplacementChar is the byte value to use for substitution. // It should normally be ASCIISub for ASCII encodings. This may be // unset (left to zero) for mappings that are strictly ASCII supersets. // In that case ASCIISub will be assumed instead. ReplacementChar byte } type cmapDecoder struct { transform.NopResetter runes [256][]byte } type cmapEncoder struct { transform.NopResetter bytes map[rune]byte replace byte } // Init initializes internal values of a character map. This should // be done early, to minimize the cost of allocation of transforms // later. It is not strictly necessary however, as the allocation // functions will arrange to call it if it has not already been done. func (c *Charmap) Init() { c.once.Do(c.initialize) } func (c *Charmap) initialize() { c.bytes = make(map[rune]byte) ascii := true for i := 0; i < 256; i++ { r, ok := c.Map[byte(i)] if !ok { r = rune(i) } if r < 128 && r != rune(i) { ascii = false } if r != RuneError { c.bytes[r] = byte(i) } utf := make([]byte, utf8.RuneLen(r)) utf8.EncodeRune(utf, r) c.runes[i] = utf } if ascii && c.ReplacementChar == '\x00' { c.ReplacementChar = ASCIISub } } // NewDecoder returns a Decoder the converts from the 8-bit // character set to UTF-8. Unknown mappings, if any, are mapped // to '\uFFFD'. func (c *Charmap) NewDecoder() *encoding.Decoder { c.Init() return &encoding.Decoder{Transformer: &cmapDecoder{runes: c.runes}} } // NewEncoder returns a Transformer that converts from UTF8 to the // 8-bit character set. Unknown mappings are mapped to 0x1A. func (c *Charmap) NewEncoder() *encoding.Encoder { c.Init() return &encoding.Encoder{ Transformer: &cmapEncoder{ bytes: c.bytes, replace: c.ReplacementChar, }, } } func (d *cmapDecoder) Transform(dst, src []byte, atEOF bool) (int, int, error) { var e error var ndst, nsrc int for _, c := range src { b := d.runes[c] l := len(b) if ndst+l > len(dst) { e = transform.ErrShortDst break } for i := 0; i < l; i++ { dst[ndst] = b[i] ndst++ } nsrc++ } return ndst, nsrc, e } func (d *cmapEncoder) Transform(dst, src []byte, atEOF bool) (int, int, error) { var e error var ndst, nsrc int for nsrc < len(src) { if ndst >= len(dst) { e = transform.ErrShortDst break } r, sz := utf8.DecodeRune(src[nsrc:]) if r == utf8.RuneError && sz == 1 { // If its inconclusive due to insufficient data in // in the source, report it if !atEOF && !utf8.FullRune(src[nsrc:]) { e = transform.ErrShortSrc break } } if c, ok := d.bytes[r]; ok { dst[ndst] = c } else { dst[ndst] = d.replace } nsrc += sz ndst++ } return ndst, nsrc, e } encoding-1.0.0/common_test.go000066400000000000000000000034621340633654000161650ustar00rootroot00000000000000// Copyright 2018 Garrett D'Amore // // 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 encoding import ( "bytes" "testing" "unicode/utf8" "golang.org/x/text/encoding" ) func verifyMap(t *testing.T, enc encoding.Encoding, b byte, r rune) { verifyFromUTF(t, enc, b, r) verifyToUTF(t, enc, b, r) } func verifyFromUTF(t *testing.T, enc encoding.Encoding, b byte, r rune) { encoder := enc.NewEncoder() out := make([]byte, 6) utf := make([]byte, utf8.RuneLen(r)) utf8.EncodeRune(utf, r) ndst, nsrc, err := encoder.Transform(out, utf, true) if err != nil { t.Errorf("Transform failed: %v", err) } if nsrc != len(utf) { t.Errorf("Length of source incorrect: %d != %d", nsrc, len(utf)) } if ndst != 1 { t.Errorf("Dest length (%d) != 1", ndst) } if b != out[0] { t.Errorf("From UTF incorrect map %v != %v", b, out[0]) } } func verifyToUTF(t *testing.T, enc encoding.Encoding, b byte, r rune) { decoder := enc.NewDecoder() out := make([]byte, 6) nat := []byte{b} utf := make([]byte, utf8.RuneLen(r)) utf8.EncodeRune(utf, r) ndst, nsrc, err := decoder.Transform(out, nat, true) if err != nil { t.Errorf("Transform failed: %v", err) } if nsrc != 1 { t.Errorf("Src length (%d) != 1", nsrc) } if !bytes.Equal(utf, out[:ndst]) { t.Errorf("UTF expected %v, but got %v for %x\n", utf, out, b) } } encoding-1.0.0/doc.go000066400000000000000000000013201340633654000143720ustar00rootroot00000000000000// Copyright 2015 Garrett D'Amore // // 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 encoding provides a few of the encoding structures that are // missing from the Go x/text/encoding tree. package encoding encoding-1.0.0/ebcdic.go000066400000000000000000000121331340633654000150420ustar00rootroot00000000000000// Copyright 2015 Garrett D'Amore // // 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 encoding import ( "golang.org/x/text/encoding" ) // EBCDIC represents the 8-bit EBCDIC scheme, found in some mainframe // environments. If you don't know what this is, consider yourself lucky. var EBCDIC encoding.Encoding func init() { cm := &Charmap{ ReplacementChar: '\x3f', Map: map[byte]rune{ // 0x00-0x03 match 0x04: RuneError, 0x05: '\t', 0x06: RuneError, 0x07: '\x7f', 0x08: RuneError, 0x09: RuneError, 0x0a: RuneError, // 0x0b-0x13 match 0x14: RuneError, 0x15: '\x85', // Not in any ISO code 0x16: '\x08', 0x17: RuneError, // 0x18-0x19 match 0x1a: RuneError, 0x1b: RuneError, // 0x1c-0x1f match 0x20: RuneError, 0x21: RuneError, 0x22: RuneError, 0x23: RuneError, 0x24: RuneError, 0x25: '\n', 0x26: '\x17', 0x27: '\x1b', 0x28: RuneError, 0x29: RuneError, 0x2a: RuneError, 0x2b: RuneError, 0x2c: RuneError, 0x2d: '\x05', 0x2e: '\x06', 0x2f: '\x07', 0x30: RuneError, 0x31: RuneError, 0x32: '\x16', 0x33: RuneError, 0x34: RuneError, 0x35: RuneError, 0x36: RuneError, 0x37: '\x04', 0x38: RuneError, 0x39: RuneError, 0x3a: RuneError, 0x3b: RuneError, 0x3c: '\x14', 0x3d: '\x15', 0x3e: RuneError, 0x3f: '\x1a', // also replacement char 0x40: ' ', 0x41: '\xa0', 0x42: RuneError, 0x43: RuneError, 0x44: RuneError, 0x45: RuneError, 0x46: RuneError, 0x47: RuneError, 0x48: RuneError, 0x49: RuneError, 0x4a: RuneError, 0x4b: '.', 0x4c: '<', 0x4d: '(', 0x4e: '+', 0x4f: '|', 0x50: '&', 0x51: RuneError, 0x52: RuneError, 0x53: RuneError, 0x54: RuneError, 0x55: RuneError, 0x56: RuneError, 0x57: RuneError, 0x58: RuneError, 0x59: RuneError, 0x5a: '!', 0x5b: '$', 0x5c: '*', 0x5d: ')', 0x5e: ';', 0x5f: '¬', 0x60: '-', 0x61: '/', 0x62: RuneError, 0x63: RuneError, 0x64: RuneError, 0x65: RuneError, 0x66: RuneError, 0x67: RuneError, 0x68: RuneError, 0x69: RuneError, 0x6a: '¦', 0x6b: ',', 0x6c: '%', 0x6d: '_', 0x6e: '>', 0x6f: '?', 0x70: RuneError, 0x71: RuneError, 0x72: RuneError, 0x73: RuneError, 0x74: RuneError, 0x75: RuneError, 0x76: RuneError, 0x77: RuneError, 0x78: RuneError, 0x79: '`', 0x7a: ':', 0x7b: '#', 0x7c: '@', 0x7d: '\'', 0x7e: '=', 0x7f: '"', 0x80: RuneError, 0x81: 'a', 0x82: 'b', 0x83: 'c', 0x84: 'd', 0x85: 'e', 0x86: 'f', 0x87: 'g', 0x88: 'h', 0x89: 'i', 0x8a: RuneError, 0x8b: RuneError, 0x8c: RuneError, 0x8d: RuneError, 0x8e: RuneError, 0x8f: '±', 0x90: RuneError, 0x91: 'j', 0x92: 'k', 0x93: 'l', 0x94: 'm', 0x95: 'n', 0x96: 'o', 0x97: 'p', 0x98: 'q', 0x99: 'r', 0x9a: RuneError, 0x9b: RuneError, 0x9c: RuneError, 0x9d: RuneError, 0x9e: RuneError, 0x9f: RuneError, 0xa0: RuneError, 0xa1: '~', 0xa2: 's', 0xa3: 't', 0xa4: 'u', 0xa5: 'v', 0xa6: 'w', 0xa7: 'x', 0xa8: 'y', 0xa9: 'z', 0xaa: RuneError, 0xab: RuneError, 0xac: RuneError, 0xad: RuneError, 0xae: RuneError, 0xaf: RuneError, 0xb0: '^', 0xb1: RuneError, 0xb2: RuneError, 0xb3: RuneError, 0xb4: RuneError, 0xb5: RuneError, 0xb6: RuneError, 0xb7: RuneError, 0xb8: RuneError, 0xb9: RuneError, 0xba: '[', 0xbb: ']', 0xbc: RuneError, 0xbd: RuneError, 0xbe: RuneError, 0xbf: RuneError, 0xc0: '{', 0xc1: 'A', 0xc2: 'B', 0xc3: 'C', 0xc4: 'D', 0xc5: 'E', 0xc6: 'F', 0xc7: 'G', 0xc8: 'H', 0xc9: 'I', 0xca: '\xad', // NB: soft hyphen 0xcb: RuneError, 0xcc: RuneError, 0xcd: RuneError, 0xce: RuneError, 0xcf: RuneError, 0xd0: '}', 0xd1: 'J', 0xd2: 'K', 0xd3: 'L', 0xd4: 'M', 0xd5: 'N', 0xd6: 'O', 0xd7: 'P', 0xd8: 'Q', 0xd9: 'R', 0xda: RuneError, 0xdb: RuneError, 0xdc: RuneError, 0xdd: RuneError, 0xde: RuneError, 0xdf: RuneError, 0xe0: '\\', 0xe1: '\u2007', // Non-breaking space 0xe2: 'S', 0xe3: 'T', 0xe4: 'U', 0xe5: 'V', 0xe6: 'W', 0xe7: 'X', 0xe8: 'Y', 0xe9: 'Z', 0xea: RuneError, 0xeb: RuneError, 0xec: RuneError, 0xed: RuneError, 0xee: RuneError, 0xef: RuneError, 0xf0: '0', 0xf1: '1', 0xf2: '2', 0xf3: '3', 0xf4: '4', 0xf5: '5', 0xf6: '6', 0xf7: '7', 0xf8: '8', 0xf9: '9', 0xfa: RuneError, 0xfb: RuneError, 0xfc: RuneError, 0xfd: RuneError, 0xfe: RuneError, 0xff: RuneError, }} cm.Init() EBCDIC = cm } encoding-1.0.0/ebcdic_test.go000066400000000000000000000061161340633654000161050ustar00rootroot00000000000000// Copyright 2018 Garrett D'Amore // // 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 encoding import ( "testing" ) type mapRange struct { first byte last byte r rune } var mapRanges = []mapRange{ {0, 3, '\x00'}, {4, 4, RuneError}, {5, 5, '\x09'}, {6, 6, RuneError}, {7, 7, '\x7f'}, {8, 10, RuneError}, {11, 19, '\x0b'}, {20, 20, RuneError}, {21, 21, '\u0085'}, {22, 22, '\x08'}, {23, 23, RuneError}, {24, 25, '\x18'}, {26, 27, RuneError}, {28, 31, '\x1c'}, {32, 36, RuneError}, {37, 37, '\x0a'}, {38, 38, '\x17'}, {39, 39, '\x1b'}, {40, 44, RuneError}, {45, 47, '\x05'}, {48, 49, RuneError}, {50, 50, '\x16'}, {51, 54, RuneError}, {55, 55, '\x04'}, {56, 59, RuneError}, {60, 61, '\x14'}, {62, 62, RuneError}, {63, 63, '\x1a'}, {64, 64, '\x20'}, {65, 65, '\xa0'}, {66, 74, RuneError}, {75, 75, '.'}, {76, 76, '<'}, {77, 77, '('}, {78, 78, '+'}, {79, 79, '|'}, {80, 80, '&'}, {81, 89, RuneError}, {90, 90, '!'}, {91, 91, '$'}, {92, 92, '*'}, {93, 93, ')'}, {94, 94, ';'}, {95, 95, '\u00ac'}, {96, 96, '\x2d'}, {97, 97, '\x2f'}, {98, 105, RuneError}, {106, 106, '\u00a6'}, {107, 107, '\x2c'}, {108, 108, '%'}, {109, 109, '\x5f'}, {110, 111, '\x3e'}, {112, 120, RuneError}, {121, 121, '\x60'}, {122, 122, ':'}, {123, 123, '#'}, {124, 124, '@'}, {125, 125, '\x27'}, {126, 126, '='}, {127, 127, '\x22'}, {128, 128, RuneError}, {129, 137, 'a'}, {138, 142, RuneError}, {143, 143, '\u00b1'}, {144, 144, RuneError}, {145, 153, 'j'}, {154, 160, RuneError}, {161, 161, '~'}, {162, 169, 's'}, {170, 175, RuneError}, {176, 176, '\x5e'}, {177, 185, RuneError}, {186, 186, '['}, {187, 187, ']'}, {188, 191, RuneError}, {192, 192, '{'}, {193, 201, 'A'}, {202, 202, '\u00ad'}, {203, 207, RuneError}, {208, 208, '}'}, {209, 217, 'J'}, {218, 223, RuneError}, {224, 224, '\x5c'}, {226, 233, 'S'}, {234, 239, RuneError}, {240, 249, '0'}, {250, 255, RuneError}, } func TestEBCDICvsASCII(t *testing.T) { t.Logf("EBCDIC/ASCII identity values") for _, rng := range mapRanges { if rng.r == RuneError { continue } for j := 0; j <= int(rng.last-rng.first); j++ { b := rng.first + byte(j) r := rng.r + rune(j) verifyMap(t, EBCDIC, b, r) } } } func TestEBDICvsInvalid(t *testing.T) { t.Logf("EBCDIC invalid transforms") for _, rng := range mapRanges { if rng.r != RuneError { continue } for j := 0; j <= int(rng.last-rng.first); j++ { b := rng.first + byte(j) verifyToUTF(t, EBCDIC, b, RuneError) } } } func TestEBDICvsLargeUTF(t *testing.T) { t.Logf("Large UTF maps to subst char") verifyFromUTF(t, EBCDIC, '\x3F', '㿿') } encoding-1.0.0/go.mod000066400000000000000000000001151340633654000144050ustar00rootroot00000000000000module github.com/gdamore/encoding go 1.9 require golang.org/x/text v0.3.0 encoding-1.0.0/go.sum000066400000000000000000000002311340633654000144310ustar00rootroot00000000000000golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= encoding-1.0.0/latin1.go000066400000000000000000000020701340633654000150200ustar00rootroot00000000000000// Copyright 2015 Garrett D'Amore // // 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 encoding import ( "golang.org/x/text/encoding" ) // ISO8859_1 represents the 8-bit ISO8859-1 scheme. It decodes directly to // UTF-8 without change, as all ISO8859-1 values are legal UTF-8. // Unicode values less than 256 (i.e. 8 bits) map 1:1 with 8859-1. // It encodes runes outside of that to 0x1A, the ASCII substitution character. var ISO8859_1 encoding.Encoding func init() { cm := &Charmap{} cm.Init() // 8859-1 is the 8-bit identity map for Unicode. ISO8859_1 = cm } encoding-1.0.0/latin1_test.go000066400000000000000000000015371340633654000160660ustar00rootroot00000000000000// Copyright 2018 Garrett D'Amore // // 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 encoding import ( "testing" ) func TestISO8859_1(t *testing.T) { t.Logf("8859-1 identity transforms") for i := 0; i < 256; i++ { verifyMap(t, ISO8859_1, byte(i), rune(i)) } t.Logf("Large UTF maps to ASCIISub") verifyFromUTF(t, ISO8859_1, ASCIISub, '㿿') } encoding-1.0.0/latin5.go000066400000000000000000000016031340633654000150250ustar00rootroot00000000000000// Copyright 2015 Garrett D'Amore // // 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 encoding import ( "golang.org/x/text/encoding" ) // ISO8859_9 represents the 8-bit ISO8859-9 scheme. var ISO8859_9 encoding.Encoding func init() { cm := &Charmap{Map: map[byte]rune{ 0xD0: 'Ğ', 0xDD: 'İ', 0xDE: 'Ş', 0xF0: 'ğ', 0xFD: 'ı', 0xFE: 'ş', }} cm.Init() ISO8859_9 = cm } encoding-1.0.0/latin5_test.go000066400000000000000000000020171340633654000160640ustar00rootroot00000000000000// Copyright 2018 Garrett D'Amore // // 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 encoding import ( "testing" ) func TestISO8859_9(t *testing.T) { t.Logf("8859-9 identity transforms") for i := 0; i < 256; i++ { r := rune(i) switch i { case 0xd0: r = 'Ğ' case 0xdd: r = 'İ' case 0xde: r = 'Ş' case 0xf0: r = 'ğ' case 0xfd: r = 'ı' case 0xfe: r = 'ş' } verifyMap(t, ISO8859_9, byte(i), r) } t.Logf("Large UTF maps to ASCIISub") verifyFromUTF(t, ISO8859_9, ASCIISub, '㿿') } encoding-1.0.0/utf8.go000066400000000000000000000022531340633654000145210ustar00rootroot00000000000000// Copyright 2015 Garrett D'Amore // // 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 encoding import ( "golang.org/x/text/encoding" ) type validUtf8 struct{} // UTF8 is an encoding for UTF-8. All it does is verify that the UTF-8 // in is valid. The main reason for its existence is that it will detect // and report ErrSrcShort or ErrDstShort, whereas the Nop encoding just // passes every byte, blithely. var UTF8 encoding.Encoding = validUtf8{} func (validUtf8) NewDecoder() *encoding.Decoder { return &encoding.Decoder{Transformer: encoding.UTF8Validator} } func (validUtf8) NewEncoder() *encoding.Encoder { return &encoding.Encoder{Transformer: encoding.UTF8Validator} } encoding-1.0.0/utf8_test.go000066400000000000000000000015301340633654000155550ustar00rootroot00000000000000// Copyright 2018 Garrett D'Amore // // 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 encoding import ( "testing" ) func TestUTF8(t *testing.T) { t.Logf("ASCII UTF8 identity transforms") for i := 0; i < 128; i++ { verifyMap(t, UTF8, byte(i), rune(i)) } // We need to add tests for ErrSrcShort, ErrDstShort, and // larger rune values. }