opentofu/vendor/github.com/dimchansky/utfbom
Martin Atkins 6db3cf8e5b vendor: upgrade Azure SDK and Azure/go-autorest
This involves some minor changes to the "azure" backend code to account
for upstream breaking changes.
2019-12-02 10:04:49 -08:00
..
.gitignore backend/azurerm: upgrading the SDK / support for proxies (#19414) 2018-11-21 22:06:03 +01:00
.travis.yml vendor: upgrade Azure SDK and Azure/go-autorest 2019-12-02 10:04:49 -08:00
go.mod backend/azurerm: upgrading the SDK / support for proxies (#19414) 2018-11-21 22:06:03 +01:00
LICENSE backend/azurerm: upgrading the SDK / support for proxies (#19414) 2018-11-21 22:06:03 +01:00
README.md vendor: upgrade Azure SDK and Azure/go-autorest 2019-12-02 10:04:49 -08:00
utfbom.go vendor: upgrade Azure SDK and Azure/go-autorest 2019-12-02 10:04:49 -08:00

utfbom Godoc License Build Status Go Report Card Coverage Status

The package utfbom implements the detection of the BOM (Unicode Byte Order Mark) and removing as necessary. It can also return the encoding detected by the BOM.

Installation

go get -u github.com/dimchansky/utfbom

Example

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"

	"github.com/dimchansky/utfbom"
)

func main() {
	trySkip([]byte("\xEF\xBB\xBFhello"))
	trySkip([]byte("hello"))
}

func trySkip(byteData []byte) {
	fmt.Println("Input:", byteData)

	// just skip BOM
	output, err := ioutil.ReadAll(utfbom.SkipOnly(bytes.NewReader(byteData)))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("ReadAll with BOM skipping", output)

	// skip BOM and detect encoding
	sr, enc := utfbom.Skip(bytes.NewReader(byteData))
	fmt.Printf("Detected encoding: %s\n", enc)
	output, err = ioutil.ReadAll(sr)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("ReadAll with BOM detection and skipping", output)
	fmt.Println()
}

Output:

$ go run main.go
Input: [239 187 191 104 101 108 108 111]
ReadAll with BOM skipping [104 101 108 108 111]
Detected encoding: UTF8
ReadAll with BOM detection and skipping [104 101 108 108 111]

Input: [104 101 108 108 111]
ReadAll with BOM skipping [104 101 108 108 111]
Detected encoding: Unknown
ReadAll with BOM detection and skipping [104 101 108 108 111]