byebom

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2021 License: MIT Imports: 6 Imported by: 0

README

byebom

byebom trims Byte Order Mark(BOM) of UTF-8.

Usage

package main

import (
	"fmt"
	"io"
	"strings"

	"github.com/bellwood4486/byebom"
)

const utf8BOM = "\ufeff"

func main() {
	// with BOM
	withBOM := strings.NewReader(utf8BOM + "a")
	s1, _ := io.ReadAll(byebom.NewUTF8Reader(withBOM))
	fmt.Println(string(s1) == "a") // -> true

	// without BOM
	withoutBOM := strings.NewReader("b")
	s2, _ := io.ReadAll(byebom.NewUTF8Reader(withoutBOM))
	fmt.Println(string(s2) == "b") // -> true

	// error when encoding is not UTF-8
	eucjp := strings.NewReader("\xa5\xa8\xa5\xe9\xa1\xbc")
	_, err := io.ReadAll(byebom.NewUTF8Reader(eucjp))
	fmt.Println(err) // -> encoding: invalid UTF-8
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReaderWithoutBOM

func NewReaderWithoutBOM(r io.Reader, fallbackEncName string) (io.Reader, error)

func NewUTF8Reader

func NewUTF8Reader(r io.Reader) io.Reader

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL