multistring

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 2 Imported by: 0

README

Multistring

Introduction

multistring defines a new type, Wrap, that has the underlying type []string, with methods defined on it that correspond to1 the free functions in the standard library's strings package. Each method performs the equivalent of applying the corresponding function over the slice, like the map higher-order function would.

Example

w := multistring.Wrap{" apple  ", "banana ", "  celery"}
s := w.TrimSpace().ToUpper().Repeat(3).JoinBy(",")

println(s)
// APPLEAPPLEAPPLE,BANANABANANABANANA,CELERYCELERYCELERY

Method signatures

The methods' signatures are derived from those of the corresponding functions, subject to two transformations:

  1. The elements in the Wrap will be implicitly passed as the first string argument to the strings function
  2. Every return type T will be transformed to the slice type []T, unless T is string, in which case it will be transformed to Wrap, to allow chaining.

Other methods

There are two additional methods on Wrap that are not derived from strings, JoinBy and MapString:

  • JoinBy can be used for cases where, as a final step, you want to join a group of strings (contained in a Wrap) with the same separator, rather than join a group of strings (passed to the Join method) with a group of separators contained in a Wrap.
  • MapString is the analogue of strings.Map, but taking a function that takes a string (func(string) string), instead of a rune.

Why did I do this?

For a side project, I needed to apply functions from strings over slices of strings. After writing a few for loops, I decided to abstract it out into what is now Wrap, and then I thought - what if I could generate these method definitions?

A couple of days later...

Documentation

Overview

Package multistring provides wrappers around the standard library's strings package's functions that take string slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Wrap

type Wrap []string

func (Wrap) Contains

func (w Wrap) Contains(substr string) []bool

func (Wrap) ContainsAny

func (w Wrap) ContainsAny(chars string) []bool

func (Wrap) ContainsRune

func (w Wrap) ContainsRune(r rune) []bool

func (Wrap) Count

func (w Wrap) Count(substr string) []int

func (Wrap) Cut

func (w Wrap) Cut(sep string) (before, after Wrap, found []bool)

func (Wrap) EqualFold

func (w Wrap) EqualFold(t string) []bool

func (Wrap) Fields

func (w Wrap) Fields() []Wrap

func (Wrap) FieldsFunc

func (w Wrap) FieldsFunc(f func(rune) bool) []Wrap

func (Wrap) HasPrefix

func (w Wrap) HasPrefix(prefix string) []bool

func (Wrap) HasSuffix

func (w Wrap) HasSuffix(suffix string) []bool

func (Wrap) Index

func (w Wrap) Index(substr string) []int

func (Wrap) IndexAny

func (w Wrap) IndexAny(chars string) []int

func (Wrap) IndexByte

func (w Wrap) IndexByte(c byte) []int

func (Wrap) IndexFunc

func (w Wrap) IndexFunc(f func(rune) bool) []int

func (Wrap) IndexRune

func (w Wrap) IndexRune(r rune) []int

func (Wrap) Join

func (w Wrap) Join(elems []string) Wrap

func (Wrap) JoinBy

func (w Wrap) JoinBy(sep string) string

JoinBy joins the strings in the `Wrap` using a separator.

func (Wrap) LastIndex

func (w Wrap) LastIndex(substr string) []int

func (Wrap) LastIndexAny

func (w Wrap) LastIndexAny(chars string) []int

func (Wrap) LastIndexByte

func (w Wrap) LastIndexByte(c byte) []int

func (Wrap) LastIndexFunc

func (w Wrap) LastIndexFunc(f func(rune) bool) []int

func (Wrap) Map

func (w Wrap) Map(mapping func(rune) rune) Wrap

func (Wrap) MapString

func (w Wrap) MapString(mapping func(string) string) Wrap

MapString applies a function over the strings in the Wrap, returning a new Wrap.

func (Wrap) Repeat

func (w Wrap) Repeat(count int) Wrap

func (Wrap) Replace

func (w Wrap) Replace(old string, new string, n int) Wrap

func (Wrap) ReplaceAll

func (w Wrap) ReplaceAll(old string, new string) Wrap

func (Wrap) Split

func (w Wrap) Split(sep string) []Wrap

func (Wrap) SplitAfter

func (w Wrap) SplitAfter(sep string) []Wrap

func (Wrap) SplitAfterN

func (w Wrap) SplitAfterN(sep string, n int) []Wrap

func (Wrap) SplitN

func (w Wrap) SplitN(sep string, n int) []Wrap

func (Wrap) Title

func (w Wrap) Title() Wrap

func (Wrap) ToLower

func (w Wrap) ToLower() Wrap

func (Wrap) ToLowerSpecial

func (w Wrap) ToLowerSpecial(c unicode.SpecialCase) Wrap

func (Wrap) ToTitle

func (w Wrap) ToTitle() Wrap

func (Wrap) ToTitleSpecial

func (w Wrap) ToTitleSpecial(c unicode.SpecialCase) Wrap

func (Wrap) ToUpper

func (w Wrap) ToUpper() Wrap

func (Wrap) ToUpperSpecial

func (w Wrap) ToUpperSpecial(c unicode.SpecialCase) Wrap

func (Wrap) ToValidUTF8

func (w Wrap) ToValidUTF8(replacement string) Wrap

func (Wrap) Trim

func (w Wrap) Trim(cutset string) Wrap

func (Wrap) TrimFunc

func (w Wrap) TrimFunc(f func(rune) bool) Wrap

func (Wrap) TrimLeft

func (w Wrap) TrimLeft(cutset string) Wrap

func (Wrap) TrimLeftFunc

func (w Wrap) TrimLeftFunc(f func(rune) bool) Wrap

func (Wrap) TrimPrefix

func (w Wrap) TrimPrefix(prefix string) Wrap

func (Wrap) TrimRight

func (w Wrap) TrimRight(cutset string) Wrap

func (Wrap) TrimRightFunc

func (w Wrap) TrimRightFunc(f func(rune) bool) Wrap

func (Wrap) TrimSpace

func (w Wrap) TrimSpace() Wrap

func (Wrap) TrimSuffix

func (w Wrap) TrimSuffix(suffix string) Wrap

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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