zstdzgo.at/zstd/zstring Index | Files

package zstring

import "zgo.at/zstd/zstring"

Package zstring implements functions for strings.

All functions work correctly on Unicode codepoints/runes, but usually *don't* work on unicode clusters. That is, things like emojis composed of multiple codepoints and combining characters aren't dealt with unless explicitly mentioned otherwise.

Index

Functions

func AlignCenter

func AlignCenter(s string, n int) string

AlignCenter centre-aligns a string, filling up any remaining width with spaces.

func AlignLeft

func AlignLeft(s string, n int) string

AlignLeft left-aligns a string, filling up any remaining width with spaces.

func AlignRight

func AlignRight(s string, n int) string

AlignRight right-aligns a string, filling up any remaining width with spaces.

func ElideCenter

func ElideCenter(s string, n int) string

ElideCenter returns the "n" characters of the string.

If the string is shorter than "n" it will return the first n/2 characters and last n/2 characters of the string with "…" inserted in the centre. Otherwise the entire string is returned as-is.

func ElideLeft

func ElideLeft(s string, n int) string

ElideLeft returns the "n" left characters of the string.

If the string is shorter than "n" it will return the first "n" characters of the string with "…" appended. Otherwise the entire string is returned as-is.

func ElideRight

func ElideRight(s string, n int) string

ElideRight returns the "n" right characters of the string.

If the string is shorter than "n" it will return the first "n" characters of the string with "…" appended. Otherwise the entire string is returned as-is.

func Fields

func Fields(s, sep string) []string

Fields slices s to all substrings separated by sep. Leading/trailing whitespace and empty elements will be removed.

e.g. "a;b", "a; b", " a ; b", and "a; b;" will all result in ["a", "b"].

func From

func From(s string, sep string) string

From slices the string from first occurrence of sep. This is a shortcut for:

if i := strings.Index(s, sep); i > -1 {
  s = s[i+len(sep):]
}

func GetLine

func GetLine(in string, n int) string

GetLine gets the nth line \n-denoted line from a string.

Line indexing starts at 1.

func HasPrefixes

func HasPrefixes(s string, prefixes ...string) bool

HasPrefixes tests whether the string s starts with any of the prefixes.

Identical to:

strings.HasPrefix(s, "one") || strings.HasPrefix(s, "two")

func HasSuffixes

func HasSuffixes(s string, suffixes ...string) bool

HasSuffixes tests whether the string s ends with any of the suffixes.

Identical to:

strings.HasSuffix(s, "one") || strings.HasSuffix(s, "two")

func HasUpper

func HasUpper(s string) bool

HasUpper reports if s has at least one upper-case character.

func Indent

func Indent(s string, n int) string

Ident adds n spaces of indentation to every line.

func IndexAll

func IndexAll(s, find string) []int

IndexAll finds all occurrences of the string "find".

func IndexN

func IndexN(s, find string, n uint) int

IndexN finds the nth occurrence of a string.

n starts at 1; returns -1 if there is no nth occurrence of this string.

func IndexPairs

func IndexPairs(str, start, end string) [][]int

IndexPairs finds the position of all start/end pairs.

Nested pairs are not supported.

The return value is from last match to first match; this makes it easier to manipulate the string based on the indexes.

func IsASCII

func IsASCII(s string) bool

IsASCII reports if this string looks like it's plain 7-bit ASCII.

func LowerFirst

func LowerFirst(s string) string

LowerFirst transforms the first character to lower case, leaving the rest of the casing alone.

func ReplacePairs

func ReplacePairs(str, start, end string, f func(int, string) string) string

ReplacePairs replaces everything starting with start and ending with end with the return value of the callback.

func Reverse

func Reverse(s string) string

Reverse a string.

func Safe

func Safe(s string) string

Safe converts a string to a "safe" value by replacing every non-letter and non-digit by a '-', squeezing consecutive dashes and trimming dashes at the start and end. Capitalisation is left intact.

If the result is an empty string, it will return a small random string instead of an empty string.

This makes the string suitable for pathnames, URLs, etc. This is more conservative than is strictly needed, but usually that's fine.

func Sub

func Sub(s string, start, end int) string

Sub returns a substring starting at start and ending at end.

Unlike regular string slicing this operates on runes/UTF-8 codepoints, rather than bytes.

func TrimPrefixes

func TrimPrefixes(s string, prefixes ...string) string

TrimPrefixes returns s without the provided leading prefixes strings.

Identical to:

s = strings.TrimPrefix(s, "one")
s = strings.TrimPrefix(s, "two")

func TrimSuffixes

func TrimSuffixes(s string, suffixes ...string) string

TrimSuffixes returns s without the provided trailing suffixes strings.

Identical to:

s = strings.TrimSuffix(s, "one")
s = strings.TrimSuffix(s, "two")

func Unwrap

func Unwrap(s string) string

Unwrap a string: single newlines become a space, whereas two or more are preserved.

Removes newlines at the start and end of the string, but leaves all other spacing intact (including before and after newlines).

func UpperFirst

func UpperFirst(s string) string

UpperFirst transforms the first character to upper case, leaving the rest of the casing alone.

func Upto

func Upto(s string, sep string) string

Upto slices the string up to the first occurrence of sep. This is a shortcut for:

if i := strings.Index(s, sep); i > -1 {
  s = s[:i]
}

Source Files

zstring.go

Version
v0.0.0-20240930202209-a63c3335042a (latest)
Published
Sep 30, 2024
Platform
linux/amd64
Imports
5 packages
Last checked
2 days ago

Tools for package owners.