localize

package
v2.10.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package localize is an attempt at implementing locale-aware parsing of numbers, integrating with golang.org/x/text.

This is the frozen version of the package previously at `tawesoft.co.uk/go/lxstrconv`. See migration instructions.

Todo:

  • This is proof of concept and could be tidied up
  • Checks for integer overflow
  • Support 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.

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