list

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 1 Imported by: 20

README

list

Treats a delimited string as a list, without ever allocating a []string. Generic free functions (Head, Tail, First, Last, Split, SplitTail, At, PushHead, PushTail, RemoveLast, Index, LastIndex, IsEmpty) operate on any ~string | []byte value plus a delimiter byte. Named wrapper types (Comma, Dot, Slash, Semicolon, Space) bind a delimiter so you can call the same operations as methods. Part of rosetta.

Go Reference

What matters here

  • This is string slicing, not slice building — there is no []string in sight. Head/Tail walk the string to the next delimiter and return substrings; iterating a list is a Head/Tail loop, not a for range over a slice. This avoids allocation for the common "peel off the first path segment" case (e.g. routing on a /-delimited path). If you actually want a []string, use strings.Split instead.
  • The delimiter is a single byte, not a string. Multi-character delimiters aren't supported by design; the named types (Comma = ,, Dot = ., Slash = /, …) each pin one byte via a Delimiter… constant.
  • Tail/SplitTail/PushHead/PushTail return the list's own type T, while Head/First/Last/At return a plain string. The "remaining list" stays in the list type so you can keep chaining; an extracted single item drops to string. Mind the return types when composing them.
  • The named types are immutable value types. PushHead/PushTail/RemoveLast return a new list rather than mutating the receiver — assign the result.

Documentation

Overview

Package list treats a delimited string as a list, without splitting it into a slice.

Head, Tail, Split, At, Last, PushHead, PushTail, and the rest read and rewrite a string in place around a single-byte delimiter, so walking a dotted path or a comma-separated field costs no allocations for the intermediate pieces. The named types — Dot, Comma, Slash, Semicolon, Space, Equal — bind a delimiter to a string so the same operations read as methods, and the List interface lets code accept any of them.

The delimiter is a byte, not a rune. A multi-byte delimiter cannot be expressed here, and the functions are written for the ASCII punctuation that path and header syntaxes actually use.

Index

Constants

View Source
const DelimiterComma = ','

DelimiterComma is the delimiter used by the Comma list type.

View Source
const DelimiterDot = '.'

DelimiterDot is the delimiter used by the Dot list type.

View Source
const DelimiterEqual = '='

DelimiterEqual is the delimiter used by the Equal list type.

View Source
const DelimiterSemicolon = ';'

DelimiterSemicolon is the delimiter used by the Semicolon list type.

View Source
const DelimiterSlash = '/'

DelimiterSlash is the delimiter used by the Slash list type.

View Source
const DelimiterSpace = ' '

DelimiterSpace is the delimiter used by the Space list type.

Variables

This section is empty.

Functions

func At

func At[T Stringlike](value T, delimiter byte, index int) string

At returns the list value at a particular index

func First added in v0.7.0

func First[T Stringlike](value T, delimiter byte) string

First returns the FIRST item in a list (alias for Head)

func First2 added in v0.18.3

func First2(l List) (string, string)

First2 returns the first two items in the list.

func First3 added in v0.18.3

func First3(l List) (string, string, string)

First3 returns the first three items in the list.

func First4 added in v0.18.3

func First4(l List) (string, string, string, string)

First4 returns the first four items in the list.

func Head[T Stringlike](value T, delimiter byte) string

Head returns the FIRST item in a list

func Index added in v0.3.0

func Index[T Stringlike](value T, delimiter byte) int

Index finds the first occurrence of the delimiter (-1 if not found)

func IsEmpty added in v0.3.0

func IsEmpty[T Stringlike](value T) bool

IsEmpty returns TRUE if the list is empty.

func IsEmptyTail added in v0.3.0

func IsEmptyTail[T Stringlike](value T, delimiter byte) bool

IsEmptyTail returns TRUE if this list only has one element

func Last

func Last[T Stringlike](value T, delimiter byte) string

Last returns the LAST item in a T-based-list

func Last2 added in v0.18.3

func Last2(l List) (string, string)

Last2 returns the final two items in the list (last, then second-to-last).

func Last3 added in v0.18.3

func Last3(l List) (string, string, string)

Last3 returns the final three items in the list (last first).

func Last4 added in v0.18.3

func Last4(l List) (string, string, string, string)

Last4 returns the final four items in the list (last first).

func LastIndex added in v0.3.0

func LastIndex[T Stringlike](value T, delimiter byte) int

LastIndex finds the last occurrence of the delimiter (-1 if not found)

func PushHead

func PushHead[T Stringlike](value T, headValue string, delimiter byte) T

PushHead adds a new item to the beginning of the list

func PushTail

func PushTail[T Stringlike](value T, tailValue string, delimiter byte) T

PushTail adds a new item to the end of the list

func RemoveLast

func RemoveLast[T Stringlike](value T, delimiter byte) T

RemoveLast returns the full list, with the last element removed.

func Second added in v0.18.3

func Second(l List) string

Second returns the second item in the list.

func Split

func Split[T Stringlike](value T, delimiter byte) (string, T)

Split returns the FIRST element, and the REST element in one function call

func SplitTail

func SplitTail[T Stringlike](value T, delimiter byte) (T, string)

SplitTail behaves like split, but splits the beginning of the list from the last item in the list. So, the list "a,b,c" => "a,b", "c" A list with no delimiter at all keeps the whole value as the leading list and returns an empty last item ("photo" => "photo", ""), which is what callers splitting a filename from its extension rely on. Note this is NOT the RemoveLast/Last decomposition, which would give "", "photo".

func Tail

func Tail[T Stringlike](value T, delimiter byte) T

Tail returns any values in the list AFTER the first item

func Third added in v0.18.3

func Third(l List) string

Third returns the third item in the list.

Types

type Comma added in v0.3.0

type Comma string

Comma is a List backed by a comma-delimited string.

func (Comma) At added in v0.3.0

func (list Comma) At(index int) string

At returns the item at the given index, or "" if out of range.

func (Comma) Bytes added in v0.8.1

func (list Comma) Bytes() []byte

Bytes returns the list as its underlying delimited byte slice.

func (Comma) First added in v0.7.0

func (list Comma) First() string

First returns the first item in the list.

func (Comma) Head added in v0.3.0

func (list Comma) Head() string

Head returns the first item in the list.

func (Comma) IsEmpty added in v0.3.0

func (list Comma) IsEmpty() bool

IsEmpty returns TRUE if the list contains no items.

func (Comma) IsEmptyTail added in v0.3.0

func (list Comma) IsEmptyTail() bool

IsEmptyTail returns TRUE if the list has no items after the head.

func (Comma) Last added in v0.3.0

func (list Comma) Last() string

Last returns the final item in the list.

func (Comma) PushHead added in v0.3.0

func (list Comma) PushHead(value string) List

PushHead returns a new list with the value prepended as the new head.

func (Comma) PushTail added in v0.3.0

func (list Comma) PushTail(value string) List

PushTail returns a new list with the value appended as the new tail.

func (Comma) RemoveLast added in v0.3.0

func (list Comma) RemoveLast() List

RemoveLast returns a new list with the final item removed.

func (Comma) Split added in v0.3.0

func (list Comma) Split() (string, List)

Split returns the head item and a list of the remaining items.

func (Comma) SplitTail added in v0.3.0

func (list Comma) SplitTail() (List, string)

SplitTail returns a list of the leading items and the final item.

func (Comma) String added in v0.3.0

func (list Comma) String() string

String returns the list as its underlying delimited string.

func (Comma) Tail added in v0.3.0

func (list Comma) Tail() List

Tail returns a new list containing every item after the head.

type Dot added in v0.3.0

type Dot string

Dot is a List backed by a dot-delimited string.

func (Dot) At added in v0.3.0

func (list Dot) At(index int) string

At returns the item at the given index, or "" if out of range.

func (Dot) Bytes added in v0.8.1

func (list Dot) Bytes() []byte

Bytes returns the list as its underlying delimited byte slice.

func (Dot) First added in v0.7.0

func (list Dot) First() string

First returns the first item in the list.

func (Dot) Head added in v0.3.0

func (list Dot) Head() string

Head returns the first item in the list.

func (Dot) IsEmpty added in v0.3.0

func (list Dot) IsEmpty() bool

IsEmpty returns TRUE if the list contains no items.

func (Dot) IsEmptyTail added in v0.3.0

func (list Dot) IsEmptyTail() bool

IsEmptyTail returns TRUE if the list has no items after the head.

func (Dot) Last added in v0.3.0

func (list Dot) Last() string

Last returns the final item in the list.

func (Dot) PushHead added in v0.3.0

func (list Dot) PushHead(value string) List

PushHead returns a new list with the value prepended as the new head.

func (Dot) PushTail added in v0.3.0

func (list Dot) PushTail(value string) List

PushTail returns a new list with the value appended as the new tail.

func (Dot) RemoveLast added in v0.3.0

func (list Dot) RemoveLast() List

RemoveLast returns a new list with the final item removed.

func (Dot) Split added in v0.3.0

func (list Dot) Split() (string, List)

Split returns the head item and a list of the remaining items.

func (Dot) SplitTail added in v0.3.0

func (list Dot) SplitTail() (List, string)

SplitTail returns a list of the leading items and the final item.

func (Dot) String added in v0.3.0

func (list Dot) String() string

String returns the list as its underlying delimited string.

func (Dot) Tail added in v0.3.0

func (list Dot) Tail() List

Tail returns a new list containing every item after the head.

type Equal added in v0.3.0

type Equal string

Equal is a List backed by an equals-delimited string.

func (Equal) At added in v0.3.0

func (list Equal) At(index int) string

At returns the item at the given index, or "" if out of range.

func (Equal) Bytes added in v0.8.1

func (list Equal) Bytes() []byte

Bytes returns the list as its underlying delimited byte slice.

func (Equal) First added in v0.7.0

func (list Equal) First() string

First returns the first item in the list.

func (Equal) Head added in v0.3.0

func (list Equal) Head() string

Head returns the first item in the list.

func (Equal) IsEmpty added in v0.3.0

func (list Equal) IsEmpty() bool

IsEmpty returns TRUE if the list contains no items.

func (Equal) IsEmptyTail added in v0.3.0

func (list Equal) IsEmptyTail() bool

IsEmptyTail returns TRUE if the list has no items after the head.

func (Equal) Last added in v0.3.0

func (list Equal) Last() string

Last returns the final item in the list.

func (Equal) PushHead added in v0.3.0

func (list Equal) PushHead(value string) List

PushHead returns a new list with the value prepended as the new head.

func (Equal) PushTail added in v0.3.0

func (list Equal) PushTail(value string) List

PushTail returns a new list with the value appended as the new tail.

func (Equal) RemoveLast added in v0.3.0

func (list Equal) RemoveLast() List

RemoveLast returns a new list with the final item removed.

func (Equal) Split added in v0.3.0

func (list Equal) Split() (string, List)

Split returns the head item and a list of the remaining items.

func (Equal) SplitTail added in v0.3.0

func (list Equal) SplitTail() (List, string)

SplitTail returns a list of the leading items and the final item.

func (Equal) String added in v0.3.0

func (list Equal) String() string

String returns the list as its underlying delimited string.

func (Equal) Tail added in v0.3.0

func (list Equal) Tail() List

Tail returns a new list containing every item after the head.

type List added in v0.6.0

type List interface {

	// IsEmpty returns TRUE if the list contains no items
	IsEmpty() bool

	// IsEmptyTail returns TRUE if the list contains at most one item, so its tail is empty
	IsEmptyTail() bool

	// Head returns the first item in the list
	Head() string

	// Tail returns a list containing every item except the first
	Tail() List

	// First returns the first item in the list
	First() string

	// Last returns the last item in the list
	Last() string

	// RemoveLast returns a list containing every item except the last
	RemoveLast() List

	// Split returns the first item in the list, along with a list of the remaining items
	Split() (string, List)

	// SplitTail returns a list of every item except the last, along with the last item
	SplitTail() (List, string)

	// At returns the item at the specified index, or an empty string if the index is out of bounds
	At(index int) string

	// PushHead returns a new list with the value prepended to the front
	PushHead(value string) List

	// PushTail returns a new list with the value appended to the end
	PushTail(value string) List

	// String returns the delimited string representation of the list
	String() string

	// Bytes returns the delimited byte-slice representation of the list
	Bytes() []byte
}

List interface wraps all of the list manipulation methods implemented by standard lists in this library.

func ByComma added in v0.6.0

func ByComma(value ...string) List

ByComma returns a List that joins/splits its items on commas.

func ByDot added in v0.6.0

func ByDot(value ...string) List

ByDot returns a List that joins/splits its items on dots.

func ByEqual added in v0.6.0

func ByEqual(value ...string) List

ByEqual returns a List that joins/splits its items on equals signs.

func BySemicolon added in v0.7.0

func BySemicolon(value ...string) List

BySemicolon returns a List that joins/splits its items on semicolons.

func BySlash added in v0.6.0

func BySlash(value ...string) List

BySlash returns a List that joins/splits its items on slashes.

func BySpace added in v0.6.0

func BySpace(value ...string) List

BySpace returns a List that joins/splits its items on spaces.

type Semicolon added in v0.7.0

type Semicolon string

Semicolon is a List backed by a semicolon-delimited string.

func (Semicolon) At added in v0.7.0

func (list Semicolon) At(index int) string

At returns the item at the given index, or "" if out of range.

func (Semicolon) Bytes added in v0.8.1

func (list Semicolon) Bytes() []byte

Bytes returns the list as its underlying delimited byte slice.

func (Semicolon) First added in v0.7.0

func (list Semicolon) First() string

First returns the first item in the list.

func (Semicolon) Head added in v0.7.0

func (list Semicolon) Head() string

Head returns the first item in the list.

func (Semicolon) IsEmpty added in v0.7.0

func (list Semicolon) IsEmpty() bool

IsEmpty returns TRUE if the list contains no items.

func (Semicolon) IsEmptyTail added in v0.7.0

func (list Semicolon) IsEmptyTail() bool

IsEmptyTail returns TRUE if the list has no items after the head.

func (Semicolon) Last added in v0.7.0

func (list Semicolon) Last() string

Last returns the final item in the list.

func (Semicolon) PushHead added in v0.7.0

func (list Semicolon) PushHead(value string) List

PushHead returns a new list with the value prepended as the new head.

func (Semicolon) PushTail added in v0.7.0

func (list Semicolon) PushTail(value string) List

PushTail returns a new list with the value appended as the new tail.

func (Semicolon) RemoveLast added in v0.7.0

func (list Semicolon) RemoveLast() List

RemoveLast returns a new list with the final item removed.

func (Semicolon) Split added in v0.7.0

func (list Semicolon) Split() (string, List)

Split returns the head item and a list of the remaining items.

func (Semicolon) SplitTail added in v0.7.0

func (list Semicolon) SplitTail() (List, string)

SplitTail returns a list of the leading items and the final item.

func (Semicolon) String added in v0.7.0

func (list Semicolon) String() string

String returns the list as its underlying delimited string.

func (Semicolon) Tail added in v0.7.0

func (list Semicolon) Tail() List

Tail returns a new list containing every item after the head.

type Slash added in v0.3.0

type Slash string

Slash is a List backed by a slash-delimited string.

func (Slash) At added in v0.3.0

func (list Slash) At(index int) string

At returns the item at the given index, or "" if out of range.

func (Slash) Bytes added in v0.8.1

func (list Slash) Bytes() []byte

Bytes returns the list as its underlying delimited byte slice.

func (Slash) First added in v0.7.0

func (list Slash) First() string

First returns the first item in the list.

func (Slash) Head added in v0.3.0

func (list Slash) Head() string

Head returns the first item in the list.

func (Slash) IsEmpty added in v0.3.0

func (list Slash) IsEmpty() bool

IsEmpty returns TRUE if the list contains no items.

func (Slash) IsEmptyTail added in v0.3.0

func (list Slash) IsEmptyTail() bool

IsEmptyTail returns TRUE if the list has no items after the head.

func (Slash) Last added in v0.3.0

func (list Slash) Last() string

Last returns the final item in the list.

func (Slash) PushHead added in v0.3.0

func (list Slash) PushHead(value string) List

PushHead returns a new list with the value prepended as the new head.

func (Slash) PushTail added in v0.3.0

func (list Slash) PushTail(value string) List

PushTail returns a new list with the value appended as the new tail.

func (Slash) RemoveLast added in v0.3.0

func (list Slash) RemoveLast() List

RemoveLast returns a new list with the final item removed.

func (Slash) Split added in v0.3.0

func (list Slash) Split() (string, List)

Split returns the head item and a list of the remaining items.

func (Slash) SplitTail added in v0.3.0

func (list Slash) SplitTail() (List, string)

SplitTail returns a list of the leading items and the final item.

func (Slash) String added in v0.3.0

func (list Slash) String() string

String returns the list as its underlying delimited string.

func (Slash) Tail added in v0.3.0

func (list Slash) Tail() List

Tail returns a new list containing every item after the head.

type Space added in v0.3.0

type Space string

Space is a List backed by a space-delimited string.

func (Space) At added in v0.3.0

func (list Space) At(index int) string

At returns the item at the given index, or "" if out of range.

func (Space) Bytes added in v0.8.1

func (list Space) Bytes() []byte

Bytes returns the list as its underlying delimited byte slice.

func (Space) First added in v0.7.0

func (list Space) First() string

First returns the first item in the list.

func (Space) Head added in v0.3.0

func (list Space) Head() string

Head returns the first item in the list.

func (Space) IsEmpty added in v0.3.0

func (list Space) IsEmpty() bool

IsEmpty returns TRUE if the list contains no items.

func (Space) IsEmptyTail added in v0.3.0

func (list Space) IsEmptyTail() bool

IsEmptyTail returns TRUE if the list has no items after the head.

func (Space) Last added in v0.3.0

func (list Space) Last() string

Last returns the final item in the list.

func (Space) PushHead added in v0.3.0

func (list Space) PushHead(value string) List

PushHead returns a new list with the value prepended as the new head.

func (Space) PushTail added in v0.3.0

func (list Space) PushTail(value string) List

PushTail returns a new list with the value appended as the new tail.

func (Space) RemoveLast added in v0.3.0

func (list Space) RemoveLast() List

RemoveLast returns a new list with the final item removed.

func (Space) Split added in v0.3.0

func (list Space) Split() (string, List)

Split returns the head item and a list of the remaining items.

func (Space) SplitTail added in v0.3.0

func (list Space) SplitTail() (List, string)

SplitTail returns a list of the leading items and the final item.

func (Space) String added in v0.3.0

func (list Space) String() string

String returns the list as its underlying delimited string.

func (Space) Tail added in v0.3.0

func (list Space) Tail() List

Tail returns a new list containing every item after the head.

type Stringlike added in v0.3.0

type Stringlike interface {
	~string | []byte
}

Stringlike constrains the underlying string-like types a list may be built from.

Jump to

Keyboard shortcuts

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