util

package
v0.0.0-...-e8b311c Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CondenseSpaces

func CondenseSpaces(s string) string

CondenseSpaces replaces multiple spaces with a single space and trims whitespace from the start and end of the given string. For example, ' a b c ' becomes 'a b c'.

func GCD

func GCD(a, b int) int

GCD returns the greatest common divisor of the two numbers.

func IntList

func IntList(s string) ([]int, error)

IntList converts a space-separated list of numbers like '1 2 3' into a list of ints.

func LCM

func LCM(nums ...int) int

LCM returns the least common multiple of the given numbers.

func MustAtoi

func MustAtoi(s string) int

MustAtoi runs strconv.Atoi(s), panicing if the string isn't a number.

func ParseInputLines

func ParseInputLines[T any](filename string, skipBlank bool, parseLine LineParser[T]) ([]T, error)

ParseInputLines applies the parseLine function to each line in the input file.

func ParseInputLinesSections

func ParseInputLinesSections[T any](
	filename, firstSection string,
	t T,
	skipBlank bool,
	sectionParsers map[string]SectionLineParser[T],
) (T, error)

ParseInputLinesSections parses an input file that can contain multiple sections. sectionParsers is a map of section name to a SectionLineParser, which parses a line and returns the name of the section to move to. Ignores blank lines.

func ReadInputLines

func ReadInputLines(filename string) ([]string, error)

ReadInputLines returns the raw lines in the input file.

func StringLineParser

func StringLineParser(line string) (string, error)

StringLineParser returns the given line as-is.

Types

type LineParser

type LineParser[T any] func(string) (T, error)

LineParser parses a generic type from the given line.

type SectionLineParser

type SectionLineParser[T any] func(string, *T) (string, error)

SectionLineParser takes a line and result, parses the line, and returns the next section name.

Jump to

Keyboard shortcuts

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