pax_global_header00006660000000000000000000000064141734516070014522gustar00rootroot0000000000000052 comment=31b1ec211d40131031a9fcfcb630c76903e891da golang-github-andybalholm-crlf-0.0~git20171020.670099a/000077500000000000000000000000001417345160700217615ustar00rootroot00000000000000golang-github-andybalholm-crlf-0.0~git20171020.670099a/LICENSE000077500000000000000000000024151417345160700227730ustar00rootroot00000000000000Copyright (c) 2015 Andy Balholm. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. golang-github-andybalholm-crlf-0.0~git20171020.670099a/README.md000066400000000000000000000025531417345160700232450ustar00rootroot00000000000000# crlf -- import "crlf" The crlf package helps in dealing with files that have DOS-style CR/LF line endings. ## Usage #### func Create ```go func Create(name string) (io.WriteCloser, error) ``` Create opens a text file for writing, with platform-appropriate line ending conversion. #### func NewReader ```go func NewReader(r io.Reader) io.Reader ``` NewReader returns an io.Reader that converts CR or CRLF line endings to LF. #### func NewWriter ```go func NewWriter(w io.Writer) io.Writer ``` NewWriter returns an io.Writer that converts LF line endings to CRLF. #### func Open ```go func Open(name string) (io.ReadCloser, error) ``` Open opens a text file for reading, with platform-appropriate line ending conversion. #### type Normalize ```go type Normalize struct { } ``` Normalize takes CRLF, CR, or LF line endings in src, and converts them to LF in dst. #### func (*Normalize) Reset ```go func (n *Normalize) Reset() ``` #### func (*Normalize) Transform ```go func (n *Normalize) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) ``` #### type ToCRLF ```go type ToCRLF struct{} ``` ToCRLF takes LF line endings in src, and converts them to CRLF in dst. #### func (ToCRLF) Reset ```go func (ToCRLF) Reset() ``` #### func (ToCRLF) Transform ```go func (ToCRLF) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) ``` golang-github-andybalholm-crlf-0.0~git20171020.670099a/file_unix.go000066400000000000000000000006121417345160700242710ustar00rootroot00000000000000// +build !windows package crlf import ( "io" "os" ) // Open opens a text file for reading, with platform-appropriate line ending // conversion. func Open(name string) (io.ReadCloser, error) { return os.Open(name) } // Create opens a text file for writing, with platform-appropriate line ending // conversion. func Create(name string) (io.WriteCloser, error) { return os.Create(name) } golang-github-andybalholm-crlf-0.0~git20171020.670099a/file_windows.go000066400000000000000000000012601417345160700250000ustar00rootroot00000000000000package crlf import ( "io" "os" ) type readCloser struct { io.Reader io.Closer } type writeCloser struct { io.Writer io.Closer } // Open opens a text file for reading, with platform-appropriate line ending // conversion. func Open(name string) (io.ReadCloser, error) { f, err := os.Open(name) if err != nil { return nil, err } return readCloser{ Reader: NewReader(f), Closer: f, }, nil } // Create opens a text file for writing, with platform-appropriate line ending // conversion. func Create(name string) (io.WriteCloser, error) { f, err := os.Create(name) if err != nil { return nil, err } return writeCloser{ Writer: NewWriter(f), Closer: f, }, nil } golang-github-andybalholm-crlf-0.0~git20171020.670099a/fuzz.go000066400000000000000000000004241417345160700233060ustar00rootroot00000000000000// +build gofuzz package crlf import "golang.org/x/text/transform" func Fuzz(data []byte) int { _, _, err := transform.Bytes(new(Normalize), data) if err != nil { panic(err) } _, _, err = transform.Bytes(ToCRLF{}, data) if err != nil { panic(err) } return 0 } golang-github-andybalholm-crlf-0.0~git20171020.670099a/transform.go000066400000000000000000000030641417345160700243260ustar00rootroot00000000000000// The crlf package helps in dealing with files that have DOS-style CR/LF line // endings. package crlf import ( "io" "golang.org/x/text/transform" ) // Normalize takes CRLF, CR, or LF line endings in src, and converts them // to LF in dst. type Normalize struct { prev byte } func (n *Normalize) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { for nDst < len(dst) && nSrc < len(src) { c := src[nSrc] switch c { case '\r': dst[nDst] = '\n' case '\n': if n.prev == '\r' { nSrc++ n.prev = c continue } dst[nDst] = '\n' default: dst[nDst] = c } n.prev = c nDst++ nSrc++ } if nSrc < len(src) { err = transform.ErrShortDst } return } func (n *Normalize) Reset() { n.prev = 0 } // ToCRLF takes LF line endings in src, and converts them to CRLF in dst. type ToCRLF struct{} func (ToCRLF) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { for nDst < len(dst) && nSrc < len(src) { if c := src[nSrc]; c == '\n' { if nDst+1 == len(dst) { break } dst[nDst] = '\r' dst[nDst+1] = '\n' nSrc++ nDst += 2 } else { dst[nDst] = c nSrc++ nDst++ } } if nSrc < len(src) { err = transform.ErrShortDst } return } func (ToCRLF) Reset() {} // NewReader returns an io.Reader that converts CR or CRLF line endings to LF. func NewReader(r io.Reader) io.Reader { return transform.NewReader(r, new(Normalize)) } // NewWriter returns an io.Writer that converts LF line endings to CRLF. func NewWriter(w io.Writer) io.Writer { return transform.NewWriter(w, ToCRLF{}) } golang-github-andybalholm-crlf-0.0~git20171020.670099a/transform_test.go000066400000000000000000000024441417345160700253660ustar00rootroot00000000000000package crlf import ( "testing" "golang.org/x/text/transform" ) func TestNormalize(t *testing.T) { testCases := []struct { in string want string }{ {"hello, world\r\n", "hello, world\n"}, {"hello, world\r", "hello, world\n"}, {"hello, world\n", "hello, world\n"}, {"", ""}, {"\r\n", "\n"}, {"hello,\r\nworld", "hello,\nworld"}, {"hello,\rworld", "hello,\nworld"}, {"hello,\nworld", "hello,\nworld"}, {"hello,\n\rworld", "hello,\n\nworld"}, {"hello,\r\n\r\nworld", "hello,\n\nworld"}, } n := new(Normalize) for _, c := range testCases { got, _, err := transform.String(n, c.in) if err != nil { t.Errorf("error transforming %q: %v", c.in, err) continue } if got != c.want { t.Errorf("transforming %q: got %q, want %q", c.in, got, c.want) } } } func TestToCRLF(t *testing.T) { testCases := []struct { in string want string }{ {"hello, world\n", "hello, world\r\n"}, {"", ""}, {"\n", "\r\n"}, {"hello,\nworld", "hello,\r\nworld"}, {"hello,\n\nworld", "hello,\r\n\r\nworld"}, } for _, c := range testCases { got, _, err := transform.String(ToCRLF{}, c.in) if err != nil { t.Errorf("error transforming %q: %v", c.in, err) continue } if got != c.want { t.Errorf("transforming %q: got %q, want %q", c.in, got, c.want) } } }