sjisreplace

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2021 License: MIT Imports: 2 Imported by: 0

README

sjisreplace

ci Go Reference codecov Go Report Card

Package sjisreplace provides a encoder to safely convert to Shift-JIS.

The Shift-JIS encoder in golang.org/x/text/encoding/japanese returns an error if it finds a rune that cannot be converted to Shift-JIS.

This encoder does not return an error in the same case, replaces the target rune with a pre-specified rune, and continues processing.

Example

package sjisreplace_test

import (
	"bytes"
	"fmt"
	"io"
	"strings"

	"github.com/johejo/sjisreplace"
	"golang.org/x/text/encoding/japanese"
	"golang.org/x/text/transform"
)

func Example() {
	const (
		emoji    = "😋"
		hiragana = "あ"
		katakana = "ア"
		kanji    = "亜"
	)
	cases := []string{"a", emoji, hiragana, katakana, "1", "%", kanji}

	b := new(bytes.Buffer)
	w := transform.NewWriter(b, sjisreplace.NewEncoder('?'))
	for _, s := range cases {
		if _, err := w.Write([]byte(s)); err != nil {
			panic(err)
		}
	}
	if _, err := w.Write([]byte(strings.Join(cases, ""))); err != nil {
		panic(err)
	}

	r := transform.NewReader(b, japanese.ShiftJIS.NewDecoder())
	got, err := io.ReadAll(r)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(got)) // output: a?あア1%亜a?あア1%亜
}

Documentation

Overview

Package sjisreplace provides a encoder to safely convert to Shift-JIS.

Example
package main

import (
	"bytes"
	"fmt"
	"io"
	"strings"

	"github.com/johejo/sjisreplace"
	"golang.org/x/text/encoding/japanese"
	"golang.org/x/text/transform"
)

func main() {
	const (
		emoji    = "😋"
		hiragana = "あ"
		katakana = "ア"
		kanji    = "亜"
	)
	cases := []string{"a", emoji, hiragana, katakana, "1", "%", kanji}

	b := new(bytes.Buffer)
	w := transform.NewWriter(b, sjisreplace.NewEncoder('?'))
	for _, s := range cases {
		if _, err := w.Write([]byte(s)); err != nil {
			panic(err)
		}
	}
	if _, err := w.Write([]byte(strings.Join(cases, ""))); err != nil {
		panic(err)
	}

	r := transform.NewReader(b, japanese.ShiftJIS.NewDecoder())
	got, err := io.ReadAll(r)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(got))
}
Output:

a?あア1%亜a?あア1%亜

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEncoder

func NewEncoder(r rune) transform.Transformer

NewEncoder returns a new transformer that is very similar to the Shift-JIS encoder in golang.org/x/text/encoding/japanese The Shift-JIS encoder in golang.org/x/text/encoding/japanese returns an error if it finds a rune that cannot be converted to Shift-JIS. This encoder does not return an error in the same case, replaces the target rune with a pre-specified rune, and continues processing.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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