util

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

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

Variables

View Source
var IsBigEndian = (*[2]uint8)(unsafe.Pointer(&[]uint16{1}[0]))[0] == 0
View Source
var NativeEndian = 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 SafeSliceAccess

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

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 Number

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

Jump to

Keyboard shortcuts

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