pystrconv

package
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

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

View Source
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

View Source
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.

View Source
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

func CharMask(c byte) byte

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

func CompareInsensitive(a, b string) int

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

func CompareInsensitiveN(a, b string, size int) int

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

func Flags(c byte) uint32

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

func Hex(buf []byte) string

Hex returns the hex of buf without a separator. Mirrors _Py_strhex.

CPython: Python/pystrhex.c:L145 _Py_strhex

func HexBytes

func HexBytes(buf []byte) []byte

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

func HexBytesWithSep(buf []byte, sep byte, bytesPerGroup int) []byte

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

func HexWithSep(buf []byte, sep byte, bytesPerGroup int) string

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 IsAlnum

func IsAlnum(c byte) bool

IsAlnum mirrors Py_ISALNUM.

CPython: Include/cpython/pyctype.h:L26 Py_ISALNUM

func IsAlpha

func IsAlpha(c byte) bool

IsAlpha mirrors Py_ISALPHA.

CPython: Include/cpython/pyctype.h:L23 Py_ISALPHA

func IsDigit

func IsDigit(c byte) bool

IsDigit mirrors Py_ISDIGIT.

CPython: Include/cpython/pyctype.h:L24 Py_ISDIGIT

func IsLower

func IsLower(c byte) bool

IsLower mirrors Py_ISLOWER.

CPython: Include/cpython/pyctype.h:L21 Py_ISLOWER

func IsSpace

func IsSpace(c byte) bool

IsSpace mirrors Py_ISSPACE.

CPython: Include/cpython/pyctype.h:L27 Py_ISSPACE

func IsUpper

func IsUpper(c byte) bool

IsUpper mirrors Py_ISUPPER.

CPython: Include/cpython/pyctype.h:L22 Py_ISUPPER

func IsXDigit

func IsXDigit(c byte) bool

IsXDigit mirrors Py_ISXDIGIT.

CPython: Include/cpython/pyctype.h:L25 Py_ISXDIGIT

func ParseFloat

func ParseFloat(s string) (float64, error)

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

func ToLower

func ToLower(c byte) byte

ToLower mirrors Py_TOLOWER. ASCII-only, locale-independent.

CPython: Python/pyctype.c:L145 _Py_ctype_tolower

func ToUpper

func ToUpper(c byte) byte

ToUpper mirrors Py_TOUPPER. ASCII-only, locale-independent.

CPython: Python/pyctype.c:L180 _Py_ctype_toupper

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

Jump to

Keyboard shortcuts

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