pax_global_header00006660000000000000000000000064134412322720014512gustar00rootroot0000000000000052 comment=c8a2645d126110d26fd45f8787ad8dc05ca43a7d golang-github-spkg-bom-0.0~git20160624.59b7046/000077500000000000000000000000001344123227200202635ustar00rootroot00000000000000golang-github-spkg-bom-0.0~git20160624.59b7046/.gitignore000066400000000000000000000000151344123227200222470ustar00rootroot00000000000000coverage.out golang-github-spkg-bom-0.0~git20160624.59b7046/.travis.yml000066400000000000000000000005561344123227200224020ustar00rootroot00000000000000language: go go: - 1.6 - 1.5 - 1.4 - 1.3 install: - go get github.com/stretchr/testify/assert - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls script: - go test -v -covermode=count -coverprofile=coverage.out - $(go env GOPATH | awk 'BEGIN{FS=":"} {print $1}')/bin/goveralls -coverprofile=coverage.out -service=travis-ci golang-github-spkg-bom-0.0~git20160624.59b7046/LICENSE.md000066400000000000000000000020671344123227200216740ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 John Jeffery Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-spkg-bom-0.0~git20160624.59b7046/README.md000066400000000000000000000034341344123227200215460ustar00rootroot00000000000000# bom ## strip UTF-8 byte order marks [![GoDoc](https://godoc.org/github.com/spkg/bom?status.svg)](https://godoc.org/github.com/spkg/bom) [![Build Status (Linux)](https://travis-ci.org/spkg/bom.svg?branch=master)](https://travis-ci.org/spkg/bom) [![Build status (Windows)](https://ci.appveyor.com/api/projects/status/065x7yuc77xicv59?svg=true)](https://ci.appveyor.com/project/jjeffery/bom) [![License](http://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://raw.githubusercontent.com/spkg/bom/master/LICENSE.md) [![Coverage Status](https://coveralls.io/repos/github/spkg/bom/badge.svg?branch=master)](https://coveralls.io/github/spkg/bom?branch=master) [![GoReportCard](https://goreportcard.com/badge/github.com/spkg/bom)](http://goreportcard.com/report/spkg/bom) The `bom` package provides a convenient way to strip [UTF-8 byte order marks](https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8) (BOM) from the beginning of a byte slice or an `io.Reader`. The Unicode Standard defines UTF-8 byte order marks as the byte sequence `0xEF,0xBB,0xBF`, but neither requires nor recommends their use. The Go standard library provides no support for UTF-8 byte order marks, and it looks like it never will. To quote Andy Balholm in the discussion on this issue at https://groups.google.com/forum/#!topic/golang-nuts/OToNIPdfkks > The Go team includes the original designers of UTF-8, and they consider BOMs an aBOMination. They are reluctant to do anything to make life easier for people who use BOMs. :-) > (Although they did make the compiler accept source files with BOMs, if I remember right.) In the same discussion thread another participant makes the comment that it should not be difficult to write an `io.Reader` that eats the BOM. It isn't difficult, and here is one simple implementation. golang-github-spkg-bom-0.0~git20160624.59b7046/bom.go000066400000000000000000000013361344123227200213720ustar00rootroot00000000000000// Package bom is used to clean up UTF-8 Byte Order Marks. package bom import ( "bufio" "io" ) const ( bom0 = 0xef bom1 = 0xbb bom2 = 0xbf ) // Clean returns b with the 3 byte BOM stripped off the front if it is present. // If the BOM is not present, then b is returned. func Clean(b []byte) []byte { if len(b) >= 3 && b[0] == bom0 && b[1] == bom1 && b[2] == bom2 { return b[3:] } return b } // NewReader returns an io.Reader that will skip over initial UTF-8 byte order marks. func NewReader(r io.Reader) io.Reader { buf := bufio.NewReader(r) b, err := buf.Peek(3) if err != nil { // not enough bytes return buf } if b[0] == bom0 && b[1] == bom1 && b[2] == bom2 { discardBytes(buf, 3) } return buf } golang-github-spkg-bom-0.0~git20160624.59b7046/bom_test.go000066400000000000000000000027061344123227200224330ustar00rootroot00000000000000package bom_test import ( "bytes" "io/ioutil" "testing" "github.com/spkg/bom" "github.com/stretchr/testify/assert" ) var testCases = []struct { Input []byte Expected []byte }{ { Input: nil, Expected: nil, }, { Input: []byte{}, Expected: []byte{}, }, { Input: []byte{0xef}, Expected: []byte{0xef}, }, { Input: []byte{0xef, 0xbb}, Expected: []byte{0xef, 0xbb}, }, { Input: []byte{0xef, 0xbb, 0xbf}, Expected: []byte{}, }, { Input: []byte{0xef, 0xbb, 0xbf, 0x41, 0x42, 0x43}, Expected: []byte{0x41, 0x42, 0x43}, }, { Input: []byte{0xef, 0xbb, 0x41, 0x42, 0x43}, Expected: []byte{0xef, 0xbb, 0x41, 0x42, 0x43}, }, { Input: []byte{0xef, 0x41, 0x42, 0x43}, Expected: []byte{0xef, 0x41, 0x42, 0x43}, }, { Input: []byte{0x41, 0x42, 0x43}, Expected: []byte{0x41, 0x42, 0x43}, }, } func TestClean(t *testing.T) { assert := assert.New(t) for _, tc := range testCases { output := bom.Clean(tc.Input) assert.Equal(tc.Expected, output) } } func TestReader(t *testing.T) { assert := assert.New(t) for _, tc := range testCases { // An input value of nil works differently to the Clean function. // In this case it results in an empty buffer, not nil. expected := tc.Expected if tc.Input == nil { expected = []byte{} } r1 := bytes.NewReader(tc.Input) r2 := bom.NewReader(r1) output, err := ioutil.ReadAll(r2) assert.NoError(err) assert.Equal(expected, output) } } golang-github-spkg-bom-0.0~git20160624.59b7046/discard_go14.go000066400000000000000000000003221344123227200230520ustar00rootroot00000000000000// +build !go1.5 package bom import "bufio" func discardBytes(buf *bufio.Reader, n int) { // cannot use the buf.Discard method as it was introduced in Go 1.5 for i := 0; i < n; i++ { buf.ReadByte() } } golang-github-spkg-bom-0.0~git20160624.59b7046/discard_go15.go000066400000000000000000000002361344123227200230570ustar00rootroot00000000000000// +build go1.5 package bom import "bufio" func discardBytes(buf *bufio.Reader, n int) { // the Discard method was introduced in Go 1.5 buf.Discard(n) }