util

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BigEndianMagicNumber    uint32 = 0xde120495
	LittleEndianMagicNumber uint32 = 0x950412de
)

Variables

View Source
var (
	PoSymbols = PoLexer.Symbols()
	PoRules   = []lexer.SimpleRule{
		{Name: "WS", Pattern: `\s+`},
		{Name: "Integer", Pattern: `\d+`},
		{Name: "LB", Pattern: `\[`},
		{Name: "RB", Pattern: `\]`},
		{Name: "String", Pattern: `"(\\"|[^"])*"`},
		{Name: "Msgctxt", Pattern: "msgctxt"},
		{Name: "Msgid", Pattern: "msgid"},
		{Name: "Msgstr", Pattern: "msgstr"},
		{Name: "Plural", Pattern: "_plural"},
		{Name: "Comment", Pattern: "#[^\n]*"},
	}
	PoLexer  = lexer.MustSimple(PoRules)
	PoParser = participle.MustBuild[poFile](
		participle.Lexer(PoLexer),
		participle.Unquote("String"),
		participle.Elide(
			"WS",
			"Comment",
		),
	)
)
View Source
var IsBigEndian = (*[2]uint8)(unsafe.Pointer(&[]uint16{1}[0]))[0] == 0
View Source
var NativeEndianOrder = func() binary.ByteOrder {
	if IsBigEndian {
		return binary.BigEndian
	}
	return binary.LittleEndian
}()

Functions

func Average

func Average[T Number](values ...T) T

func CountRunes

func CountRunes(slice []rune, target rune) int

func Equal

func Equal[X, Y any](x X, y Y) bool

func FindLine

func FindLine[T ~int, B []rune | []byte | string](content B, index T) int

FindLine returns the line number (1-based) for a given index within the content. The function accepts content as string, []rune, or []byte and determines the line number by counting newline characters ('\n') that appear before the specified index.

Generic Parameters:

  • T: integer numeric type for the index (constrained to integer types)
  • B: content type, constrained to []rune, []byte, or string

Parameters:

  • content: the content to analyze (string, []rune, or []byte)
  • index: position in the content for which to determine the line number

Returns:

  • int: line number (starting from 1) corresponding to the index
  • returns -1 if the index is out of range (negative or greater than content length)

Example usage:

text := "Line 1\nLine 2\nLine 3"
lineNum := FindLine(text, 10) // Returns 2, as index 10 is on the second line

The function internally handles different content types:

  • For strings: uses strings.Count to count '\n' occurrences
  • For []rune: uses an auxiliary CountRunes function
  • For []byte: uses bytes.Count to count '\n' occurrences

Implementation notes:

  • The function uses a type assertion to determine the content type at runtime
  • Counts the number of newline characters ('\n') before the given index
  • Returns line number starting from 1
  • Returns -1 if the index is out of bounds (negative or exceeding content length)

Edge cases:

  • If the content is empty or the index is out of range, the function returns -1.

func FindLineFromReader

func FindLineFromReader[T ~int](r *bytes.Reader, index T) int

func Format

func Format[T any](args ...T) string

func FuzzyEqual

func FuzzyEqual(x, y string) bool

func IsPrime added in v2.3.0

func IsPrime(n u32) bool

func NextPrime added in v2.3.0

func NextPrime(n u32) u32

func PJWHash added in v2.3.0

func PJWHash(str string) uint32

PJWHash computes a hash value for a string using the PJW (Elf) hash algorithm.

func SafeSliceAccess

func SafeSliceAccess[T any](slice []T, index int) T

func SearchSymbol added in v2.4.0

func SearchSymbol(t lexer.TokenType) string

func ShouldSkipFile

func ShouldSkipFile(
	w *krfs.Walker,
	excludedPaths []string,
	seenMap *map[string]bool,
	logger *log.Logger,
) bool

ShouldSkipFile determines if a file should be skipped during processing.

Types

type Endianness added in v2.3.0

type Endianness int
const (
	NativeEndian Endianness = iota
	LittleEndian
	BigEndian
)

func (Endianness) MagicNumber added in v2.3.0

func (e Endianness) MagicNumber() uint32

func (Endianness) Order added in v2.3.0

func (e Endianness) Order() binary.ByteOrder

func (Endianness) String added in v2.3.0

func (e Endianness) String() string

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

Jump to

Keyboard shortcuts

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