unidecode

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unidecode

func Unidecode(s string, errors ErrorHandling, replacement ...string) (string, error)

Unidecode transliterates Unicode text into plain 7-bit ASCII. For best results you might use NFC or NFKC normalization prior to transliteration.

Examples:

unidecode.Unidecode("北京kožušček", unidecode.Ignore) // Output: Bei Jing kozuscek
unidecode.Unidecode("⁐", unidecode.Strict) // Error: no replacement found for character ⁐ in position 0
unidecode.Unidecode("⁐", unidecode.Preserve) // Output: ⁐
unidecode.Unidecode("⁐", unidecode.Replace, "?") // Output: ?
Example
package main

import (
	"fmt"

	"github.com/aisbergg/go-unidecode/pkg/unidecode"
)

func main() {
	s := "北京kožušček"
	d, _ := unidecode.Unidecode(s, unidecode.Ignore)
	fmt.Println(d)
}
Output:

Bei Jing kozuscek
Example (ErrorPreserve)
package main

import (
	"fmt"

	"github.com/aisbergg/go-unidecode/pkg/unidecode"
)

func main() {
	s := "⁐"
	d, _ := unidecode.Unidecode(s, unidecode.Preserve)
	fmt.Println(d)
}
Output:

Example (ErrorReplace)
package main

import (
	"fmt"

	"github.com/aisbergg/go-unidecode/pkg/unidecode"
)

func main() {
	s := "⁐"
	d, _ := unidecode.Unidecode(s, unidecode.Replace, "?")
	fmt.Println(d)
}
Output:

?
Example (ErrorStrict)
package main

import (
	"fmt"

	"github.com/aisbergg/go-unidecode/pkg/unidecode"
)

func main() {
	s := "⁐"
	_, err := unidecode.Unidecode(s, unidecode.Strict)
	fmt.Println(err)
}
Output:

no replacement found for character ⁐ in position 0

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents an error that occurred during transliteration.

func (*Error) Error

func (e *Error) Error() string

Error returns the formatted error message.

type ErrorHandling

type ErrorHandling uint8

ErrorHandling specifies the behavior of Unidecode in case of an error.

const (
	// Ignore specifies that untransliteratable characters should be ignored.
	Ignore ErrorHandling = iota
	// Strict specifies that untransliteratable characters should cause an
	// error.
	Strict
	// Replace specifies that untransliteratable characters should be replaced
	// with a given replacement value.
	Replace
	// Preserve specifies that untransliteratable characters should be
	// preserved.
	Preserve
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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