github.com/richardwilkes/toolbox@v1.121.0/xio/bom_stripper.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package xio 11 12 import ( 13 "bufio" 14 "io" 15 16 "github.com/richardwilkes/toolbox/errs" 17 ) 18 19 const utf8BOM = '\uFEFF' 20 21 // NewBOMStripper strips a leading UTF-8 BOM marker from the input. The reader that is returned will be the same as the 22 // one passed in if it was a *bufio.Reader, otherwise, the original reader will be wrapped with a *bufio.Reader and 23 // returned. 24 func NewBOMStripper(r io.Reader) (*bufio.Reader, error) { 25 buffer, ok := r.(*bufio.Reader) 26 if !ok { 27 buffer = bufio.NewReader(r) 28 } 29 ch, _, err := buffer.ReadRune() 30 if err != nil { 31 return nil, errs.Wrap(err) 32 } 33 if ch != utf8BOM { 34 if err = buffer.UnreadRune(); err != nil { 35 return nil, errs.Wrap(err) 36 } 37 } 38 return buffer, nil 39 }