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.
Click to show internal directories.
Click to hide internal directories.