Documentation
¶
Overview ¶
Package pystrconv ports the locale-independent string and number conversion utilities from cpython/Python/. v0.4 covers pyctype.c, pystrcmp.c, mystrtoul.c, pystrtod.c+dtoa.c, and pystrhex.c.
Index ¶
- Constants
- Variables
- func CharMask(c byte) byte
- func CompareInsensitive(a, b string) int
- func CompareInsensitiveN(a, b string, size int) int
- func Flags(c byte) uint32
- func FormatFloat(f float64, code byte, precision int, flags FloatFormatFlag) string
- func Hex(buf []byte) string
- func HexBytes(buf []byte) []byte
- func HexBytesWithSep(buf []byte, sep byte, bytesPerGroup int) []byte
- func HexWithSep(buf []byte, sep byte, bytesPerGroup int) string
- func IsAlnum(c byte) bool
- func IsAlpha(c byte) bool
- func IsDigit(c byte) bool
- func IsLower(c byte) bool
- func IsSpace(c byte) bool
- func IsUpper(c byte) bool
- func IsXDigit(c byte) bool
- func ParseFloat(s string) (float64, error)
- func ToLower(c byte) byte
- func ToUpper(c byte) byte
- type FloatFormatFlag
- type IntParseStatus
Constants ¶
const ( CtLower = 0x01 CtUpper = 0x02 CtAlpha = CtLower | CtUpper CtDigit = 0x04 CtAlnum = CtAlpha | CtDigit CtSpace = 0x08 CtXDigit = 0x10 )
Character classification flags. Values match cpython/Include/cpython/pyctype.h so the bitmask layout is preserved.
CPython: Include/cpython/pyctype.h:L8 PY_CTF_*
Variables ¶
var ErrFloatOverflow = errors.New("value too large to convert to float")
ErrFloatOverflow is returned by ParseFloat when the literal is finite but its magnitude exceeds the float64 range.
var ErrInvalidFloat = errors.New("could not convert string to float")
ErrInvalidFloat is returned by ParseFloat when the input is not a valid Python float literal.
Functions ¶
func CharMask ¶
CharMask coerces a byte to its 0..255 range. CPython's Py_CHARMASK exists because C's char may be signed; in Go a byte is always 0..255 but the function preserves the source-shape for ports that use CHARMASK to silence sign warnings.
CPython: Include/cpython/pyport.h:L37 Py_CHARMASK
func CompareInsensitive ¶
CompareInsensitive returns negative, zero, or positive depending on whether a is lexicographically less, equal, or greater than b under ASCII case folding. Mirrors PyOS_mystricmp.
CPython: Python/pystrcmp.c:L21 PyOS_mystricmp
func CompareInsensitiveN ¶
CompareInsensitiveN compares the first size bytes of a and b under ASCII case folding. Mirrors PyOS_mystrnicmp. CPython's implementation walks size-1 bytes and then compares the size'th pair, so passing size==0 returns 0 with no comparison.
CPython: Python/pystrcmp.c:L7 PyOS_mystrnicmp
func Flags ¶
Flags returns the classification bitmask for c. Mirrors `_Py_ctype_table[c]`.
CPython: Python/pyctype.c:L5 _Py_ctype_table
func FormatFloat ¶
func FormatFloat(f float64, code byte, precision int, flags FloatFormatFlag) string
FormatFloat mirrors PyOS_double_to_string. code is one of 'r', 's', 'g', 'G', 'e', 'E', 'f', 'F', '%'. precision is -1 to use the format's default; for 'r' it must be 0.
CPython: Python/pystrtod.c:L757 PyOS_double_to_string
func Hex ¶
Hex returns the hex of buf without a separator. Mirrors _Py_strhex.
CPython: Python/pystrhex.c:L145 _Py_strhex
func HexBytes ¶
HexBytes is the []byte-returning variant of Hex. Mirrors _Py_strhex_bytes. The returned slice is freshly allocated and owned by the caller.
CPython: Python/pystrhex.c:L152 _Py_strhex_bytes
func HexBytesWithSep ¶
HexBytesWithSep is the []byte-returning variant of HexWithSep. Mirrors _Py_strhex_bytes_with_sep.
CPython: Python/pystrhex.c:L167 _Py_strhex_bytes_with_sep
func HexWithSep ¶
HexWithSep returns the hex of buf with sep inserted between groups of bytesPerGroup bytes. Positive bytesPerGroup groups from the right (the bytes.hex default), negative groups from the left. Mirrors _Py_strhex_with_sep.
CPython: Python/pystrhex.c:L159 _Py_strhex_with_sep
func ParseFloat ¶
ParseFloat mirrors PyOS_string_to_double. The string must already be stripped of outer whitespace by the caller; ParseFloat itself does not strip. Underscores following PEP 515 placement are accepted.
CPython: Python/pystrtod.c:L298 PyOS_string_to_double
Types ¶
type FloatFormatFlag ¶
type FloatFormatFlag uint32
FloatFormatFlag mirrors the Py_DTSF_* flags PyOS_double_to_string accepts.
CPython: Include/cpython/pyhash.h adjacent (Py_DTSF_* flags in pystrtod.h)
const ( // FlagAddDotZero forces an integer-valued float to render with // a trailing ".0" so the type round-trips through repr. FlagAddDotZero FloatFormatFlag = 1 << iota // FlagAlternate mirrors Py_DTSF_ALT (use_alt_formatting). FlagAlternate // FlagAlwaysSign mirrors Py_DTSF_SIGN (always_add_sign). FlagAlwaysSign // FlagSpaceSign reserves a leading space for non-negative values. // Mirrors the ' ' format-spec sign mode. FlagSpaceSign // FlagNoNegZero mirrors Py_DTSF_NO_NEG_0. FlagNoNegZero )
type IntParseStatus ¶
type IntParseStatus int
IntParseStatus is the parse outcome.
const ( // IntOK means the number parsed cleanly. IntOK IntParseStatus = iota // IntInvalid means the input had no digits at the expected position. IntInvalid // IntOverflow means the number is too large for the target type. IntOverflow )
func ParseInt ¶
func ParseInt(s string, base int) (val int64, n int, status IntParseStatus)
ParseInt mirrors PyOS_strtol. Accepts an optional leading sign.
CPython: Python/mystrtoul.c:L268 PyOS_strtol
func ParseUint ¶
func ParseUint(s string, base int) (val uint64, n int, status IntParseStatus)
ParseUint mirrors PyOS_strtoul. Returns the parsed value, the number of bytes consumed from s (the index of the first byte not part of the number after leading whitespace), and a status code.
CPython: Python/mystrtoul.c:L100 PyOS_strtoul