confusables

package module
v0.0.0-...-5e6b245 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: MIT Imports: 9 Imported by: 0

README

confusables

GoDoc Build Status Go Report Card

Unicode confusable detection

Overview

package main

import (
	"fmt"

	"github.com/eskriett/confusables"
)

func main() {
	fmt.Println(confusables.ToSkeleton("𝐞х⍺𝓂𝕡Іꬲ"))
	// exarnple

	fmt.Println(confusables.IsConfusable("example", "𝐞х⍺𝓂𝕡Іꬲ"))
	// true

	fmt.Println(confusables.IsConfusable("example", "𝐞х⍺𝓂𝕡І"))
	// false
}

Documentation

Overview

Package confusables provides functions for identifying words that appear to be similar but use different characters.

Index

Constants

This section is empty.

Variables

View Source
var ErrIgnoreLine = errors.New("line should be ignored")

ErrIgnoreLine is raised when processing a line which should be ignored.

Functions

func AddMapping

func AddMapping(r rune, confusable string)

AddMapping allows custom mappings to be defined for a rune.

func AddMappingWithDesc

func AddMappingWithDesc(r rune, confusable, runeDesc, confusableDesc string)

AddMappingWithDesc allows a custom mapping to be defined between a rune and its confusable and for a description to be provided for that mapping.

func IsConfusable

func IsConfusable(s1, s2 string) bool

IsConfusable checks if two strings are confusable of one another.

func LoadMappings

func LoadMappings(r io.Reader) error

LoadMappings reads r and loads in confusable mappings. Where a confusable already exists, this will override the mapping.

func ToASCII

func ToASCII(s string) string

ToASCII converts characters in a string to their ASCII equivalent if possible.

func ToNumber

func ToNumber(s string) string

ToNumber converts characters in a string to their numeric values if possible.

func ToSkeleton

func ToSkeleton(s string) string

ToSkeleton converts a string to its skeleton form as defined by the skeleton algorithm in https://www.unicode.org/reports/tr39/#def-skeleton.

Types

type ConfusableEntry

type ConfusableEntry struct {
	Description Description
	Target      string
	Source      rune
}

ConfusableEntry defines a parsed entry from a confusable mapping file.

func ParseLine

func ParseLine(line string) (*ConfusableEntry, error)

ParseLine takes a confusable line and returns a ConfusableEntry. If a line should be skipped an ErrIgnoreLine error is raised.

type Confusables

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

Confusables provides functions for identifying words that appear to be similar but use different characters.

func New

func New() *Confusables

New creates a new instance of Confusables.

func (*Confusables) ToASCII

func (c *Confusables) ToASCII(s string) string

ToASCII converts characters in a string to their ASCII equivalent if possible.

func (*Confusables) ToASCIIDiff

func (c *Confusables) ToASCIIDiff(s string) (string, []Diff)

func (*Confusables) ToNumber

func (c *Confusables) ToNumber(s string) string

ToNumber converts characters in a string that look like numbers into numbers.

type Description

type Description struct {
	From string
	To   string
}

Description describes a mapping for a confusable.

type Diff

type Diff struct {
	Confusable  *string
	Description *Description
	Rune        rune
}

Diff details the mapping from a rune to its confusable if it exists.

func ToASCIIDiff

func ToASCIIDiff(s string) (string, []Diff)

ToASCIIDiff converts characters in a string to their ASCII equivalent if possible.

func ToSkeletonDiff

func ToSkeletonDiff(s string) []Diff

ToSkeletonDiff returns a slice of Diff detailing the changes that have been made within the string to reach its skeleton form.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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