Coder Social home page Coder Social logo

text's Introduction

This project is not active, and moved to spiegel-im-spiegel/gnkf.

text - Encoding/Decoding Text Package by Golang

Build Status GitHub license GitHub release

Declare text module

See go.mod file.

Usage of package

Import Package

import "github.com/spiegel-im-spiegel/text"

detect

encoding := detect.EncodingJa(bytes.NewBufferString("こんにちは,世界"))
fmt.Println(encoding)
// Output:
// UTF-8

decode

jisText := []byte{0x1b, 0x24, 0x42, 0x24, 0x33, 0x24, 0x73, 0x24, 0x4b, 0x24, 0x41, 0x24, 0x4f, 0x40, 0x24, 0x33, 0x26, 0x1b, 0x28, 0x42}
res, err := decode.ToUTF8ja(bytes.NewReader(jisText))
if err != nil {
    fmt.Println(err)
    return
}
buf := new(bytes.Buffer)
io.Copy(buf, res)
fmt.Println(buf)
// Output:
// こんにちは世界

encode

utf8Text := "こんにちは,世界\n"
res, err := encode.FromUTF8To(detect.ISO2022JP, bytes.NewBufferString(utf8Text))
if err != nil {
    fmt.Println(err)
    return
}
buf := new(bytes.Buffer)
io.Copy(buf, res)
fmt.Println(buf.Bytes())
// Output:
// [27 36 66 36 51 36 115 36 75 36 65 36 79 33 36 64 36 51 38 27 40 66 10]

convert

jisText := []byte{0x1b, 0x24, 0x42, 0x24, 0x33, 0x24, 0x73, 0x24, 0x4b, 0x24, 0x41, 0x24, 0x4f, 0x40, 0x24, 0x33, 0x26, 0x1b, 0x28, 0x42}
res, err := convert.FromTo(detect.ISO2022JP, detect.UTF8, bytes.NewReader(jisText))
if err != nil {
    fmt.Println(err)
    return
}
buf := new(bytes.Buffer)
io.Copy(buf, res)
fmt.Println(buf)
// Output:
// こんにちは世界

newline

res := newline.Reader(strings.NewReader("こんにちは\nこんにちは\rこんにちは\r\nこんにちは"), newline.LF)
buf := new(bytes.Buffer)
io.Copy(buf, res)
fmt.Println(buf)
// Output:
// こんにちは
// こんにちは
// こんにちは
// こんにちは
res := newline.String("こんにちは\nこんにちは\rこんにちは\r\nこんにちは", newline.LF)
fmt.Println(res)
// Output:
// こんにちは
// こんにちは
// こんにちは
// こんにちは

normalize

res := normalize.Reader(strings.NewReader("ペンギン"), normalize.NFKC)
buf := new(bytes.Buffer)
io.Copy(buf, res)
fmt.Println(buf)
// Output:
// ペンギン
res := normalize.String("ペンギン", normalize.NFKC)
fmt.Println(res)
// Output:
// ペンギン

width

res := width.Reader(strings.NewReader("1234567890アイウエオカキクケコABCDEFGHIJK"), width.Fold)
buf := new(bytes.Buffer)
io.Copy(buf, res)
fmt.Println(buf)
// Output:
// 1234567890アイウエオカキクケコABCDEFGHIJK
res := width.String("1234567890アイウエオカキクケコABCDEFGHIJK", width.Fold)
fmt.Printlnres)
// Output:
// 1234567890アイウエオカキクケコABCDEFGHIJK

Command Line Interface

Binaries

See latest release.

Usage

$ gonkf -h
Network Kanji Filter by Golang

Usage:
  gonkf [flags]
  gonkf [command]

Available Commands:
  conv        Convert character encoding of text
  guess       Guess character encoding of text
  help        Help about any command
  norm        Unicode normalization
  nwline      Convert newline of text
  version     Print the version number of gonkf
  width       Convert character width of text

Flags:
  -h, --help   help for gonkf

Use "gonkf [command] --help" for more information about a command.

guess sub-command

$ gonkf guess -h
Guess character encoding of text

Usage:
  gonkf guess [flags] [text file]

Flags:
  -h, --help   help for guess

$ echo こんにちは。世界の国から | gonkf guess
UTF-8

conv sub-command

$ gonkf conv -h
Convert character encoding of text

Usage:
  gonkf conv [flags] [text file]

Flags:
  -d, --dst-encoding string   encoding of dest [euc|jis|sjis|utf8] (default "utf8")
  -h, --help                  help for conv
  -o, --output string         output file path
  -s, --src-encoding string   encoding of src [euc|jis|sjis|utf8]

$ gonkf conv -d utf8 testdata/SHIFT_JIS.txt
こんにちは。世界の国から。

norm sub-command

$ gonkf norm -h
Unicode normalization (UTF-8 text only)

Usage:
  gonkf norm [flags] [text file]

Flags:
  -f, --form string     normalization form [nfc|nfd|nfkc|nfkd] (default "nfc")
  -h, --help            help for norm
  -o, --output string   output file path

$ echo ペンギン | gonkf norm -f NFKC
ペンギン

width sub-command

$ gonkf width -h
Convert character width of text (UTF-8 text only)

Usage:
  gonkf width [flags] [text file]

Flags:
  -f, --form string     form of width [fold|narrow|widen] (default "fold")
  -h, --help            help for width
  -o, --output string   output file path

$ echo 1234567890アイウエオカキクケコABCDEFGHIJK | gonkf width -f fold
1234567890アイウエオカキクケコABCDEFGHIJK

text's People

Contributors

spiegel-im-spiegel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.