lxstrconv

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: MIT, MIT-0, MIT-0 Imports: 8 Imported by: 2

README

lxstrconv - locale-aware number parsing

go get -u "tawesoft.co.uk/go"
import "tawesoft.co.uk/go/lxstrconv"

##FROZEN - PLEASE MIGRATE

These packages are moving to https://github.com/tawesoft/golib.

This is to increase security against possible supply chain attacks such as our domain name expiring in the future and being registered by someone else.

Please migrate to https://github.com/tawesoft/golib (when available) instead.

Most programs relying on a package in this monorepo, such as the dialog or lxstrconv packages, will continue to work for the foreseeable future.

Rarely used packages have been hidden for now - they are in the git commit history at https://github.com/tawesoft/go if you need to resurrect one.

Links License Stable?
homedocssrc MIT ✔ yes

About

Package lxstrconv is an attempt at implementing locale-aware parsing of numbers that integrates with golang.org/x/text.

If golang.org/x/text is ever promoted to core then there will be a new version of this package named lstrconv (dropping the 'x').

Todo:

  • checks for integer overflow

  • different representations of negative numbers e.g. (123) vs -123

  • In cases where AcceptInteger/AcceptFloat reach a syntax error, they currently underestimate how many bytes they successfully parsed when the byte length of the string is not equal to the number of Unicode code points in the string.

Example

This example demonstrates British, Dutch, and Arabic locale number parsing.

package main

import (
    "fmt"
    "golang.org/x/text/language"
    "tawesoft.co.uk/go/lxstrconv"
)

func checked(f float64, e error) float64 {
    if e != nil {
        panic(e)
    }
    return f
}

func main() {
    dutch   := lxstrconv.NewDecimalFormat(language.Dutch)
    british := lxstrconv.NewDecimalFormat(language.BritishEnglish)
    arabic  := lxstrconv.NewDecimalFormat(language.Arabic)

    fmt.Printf("%f\n", checked(british.ParseFloat("1,234.56")))
    fmt.Printf("%f\n", checked(dutch.ParseFloat("1.234,56")))
    fmt.Printf("%f\n", checked(arabic.ParseFloat("١٬٢٣٤٫٥٦")))
}

Example

You can give end-users examples of the input you expect for a given locale using the /x/text package:

package main

import (
    "golang.org/x/text/language"
    "golang.org/x/text/message"
    "golang.org/x/text/number"
)

func main() {

    message.NewPrinter(language.English).Println(number.Decimal(123456789))
    // Prints 123,456,789

    message.NewPrinter(language.Dutch).Println(number.Decimal(123456789))
    // Prints 123.456.789

    message.NewPrinter(language.Malayalam).Println(number.Decimal(123456789))
    // Prints 12,34,56,789

    message.NewPrinter(language.Bengali).Println(number.Decimal(123456789))
    // Prints ১২,৩৪,৫৬,৭৮৯
}

Getting Help

This package is part of tawesoft.co.uk/go, a monorepo for small Go modules maintained by Tawesoft®. Check out that URL for more information about other Go modules from Tawesoft plus community and commercial support options.

Documentation

Overview

Package lxstrconv is an attempt at implementing locale-aware parsing of numbers that integrates with golang.org/x/text.

If golang.org/x/text is ever promoted to core then there will be a new version of this package named `lstrconv` (dropping the 'x').

Todo:

* checks for integer overflow

* different representations of negative numbers e.g. `(123)` vs `-123`

* In cases where AcceptInteger/AcceptFloat reach a syntax error, they currently underestimate how many bytes they successfully parsed when the byte length of the string is not equal to the number of Unicode code points in the string.

Example

This example demonstrates British, Dutch, and Arabic locale number parsing.

package main

import (
    "fmt"
    "golang.org/x/text/language"
    "tawesoft.co.uk/go/lxstrconv"
)

func checked(f float64, e error) float64 {
    if e != nil {
        panic(e)
    }
    return f
}

func main() {
    dutch   := lxstrconv.NewDecimalFormat(language.Dutch)
    british := lxstrconv.NewDecimalFormat(language.BritishEnglish)
    arabic  := lxstrconv.NewDecimalFormat(language.Arabic)

    fmt.Printf("%f\n", checked(british.ParseFloat("1,234.56")))
    fmt.Printf("%f\n", checked(dutch.ParseFloat("1.234,56")))
    fmt.Printf("%f\n", checked(arabic.ParseFloat("١٬٢٣٤٫٥٦")))
}

Example

You can give end-users examples of the input you expect for a given locale using the /x/text package:

package main

import (
    "golang.org/x/text/language"
    "golang.org/x/text/message"
    "golang.org/x/text/number"
)

func main() {

    message.NewPrinter(language.English).Println(number.Decimal(123456789))
    // Prints 123,456,789

    message.NewPrinter(language.Dutch).Println(number.Decimal(123456789))
    // Prints 123.456.789

    message.NewPrinter(language.Malayalam).Println(number.Decimal(123456789))
    // Prints 12,34,56,789

    message.NewPrinter(language.Bengali).Println(number.Decimal(123456789))
    // Prints ১২,৩৪,৫৬,৭৮৯
}

FROZEN - PLEASE MIGRATE

These packages are moving to https://github.com/tawesoft/golib.

This is to increase security against possible supply chain attacks such as our domain name expiring in the future and being registered by someone else.

Please migrate to https://github.com/tawesoft/golib (when available) instead.

Most programs relying on a package in this monorepo, such as the dialog or lxstrconv packages, will continue to work for the foreseeable future.

Rarely used packages have been hidden for now - they are in the git commit history at https://github.com/tawesoft/go if you need to resurrect one.

Package Information

License: MIT (see LICENSE.txt)

Stable: yes

For more information, documentation, source code, examples, support, links, etc. please see https://www.tawesoft.co.uk/go and https://www.tawesoft.co.uk/go/lxstrconv

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NumberFormat

type NumberFormat interface {
	ParseInt(string) (int64, error)
	ParseFloat(string) (float64, error)

	// AcceptInt parses as much of an integer as possible. The second return
	// value is the number of bytes (not runes) successfully parsed. The error
	// value is always either nil or strconv.ErrRange.
	AcceptInt(string) (int64, int, error)

	// AcceptFloat parses as much of a float as possible. The second return
	// value is the number of bytes (not runes) successfully parsed. The error
	// value is always either nil or strconv.ErrRange.
	AcceptFloat(string) (float64, int, error)
}

NumberFormat defines an interface for parsing numbers in a specific format (such as a decimal number in a specific locale, with support for a digit separator such as commas and a decimal point). Numbers are assumed to be in the normal base (e.g. base 10 for decimal) for that locale.

Errors are either nil, strconv.ErrSyntax or strconv.ErrRange

func NewDecimalFormat

func NewDecimalFormat(tag language.Tag) NumberFormat

NewDecimalFormat constructs, for a given locale, a NumberFormat that defines how a decimal (base-10) number should be parsed. Note that the behaviour is undefined for locales that have non-base-10 number systems.

Jump to

Keyboard shortcuts

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