stringz

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 2 Imported by: 2

README

gopherz

ezpkg.io/stringz

PkgGoDev GitHub License version

Package stringz extends the standard library strings with additional functions.

Installation

go get -u ezpkg.io/stringz@v0.1.2

Examples

stringz.Builder

The stdlib strings.Builder provides many functions that always return nil error. They have their counterparts in stringz.Builder that eliminate the need of error handling.

// stdlib: strings.Builder
_, err = b.WriteString()
if err != nil {
    return err
}
_, err = fmt.Fprintf(&b, "Hello, %s!", "world")
if err != nil {
    return err
}

// ezpkg.io/stringz.Builder
b.WriteStringZ()
b.Printf("Hello, %s!", "world")

About ezpkg.io

As I work on various Go projects, I often find myself creating utility functions, extending existing packages, or developing packages to solve specific problems. Moving from one project to another, I usually have to copy or rewrite these solutions. So I created this repository to have all these utilities and packages in one place. Hopefully, you'll find them useful as well.

For more information, see the main repository.

Author

Oliver Nguyen  github

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FirstNParts

func FirstNParts(s string, n int, sep string) string

func FirstNPartsIdx added in v0.1.0

func FirstNPartsIdx(s string, n int, sep string) int

func FirstNPartsX added in v0.1.0

func FirstNPartsX(s string, n int, sep string) (parts, remain string)

func FirstPart added in v0.1.0

func FirstPart(s, sep string) string

func FirstPartIdx added in v0.1.0

func FirstPartIdx(s, sep string) int

func FirstPartX added in v0.1.0

func FirstPartX(s, sep string) (first, remain string)

func JoinFunc added in v0.1.2

func JoinFunc[T any](list []T, sep string, fn func(T) string) string

func LastNParts

func LastNParts(s string, n int, sep string) string

func LastNPartsIdx added in v0.1.0

func LastNPartsIdx(s string, n int, sep string) int

func LastNPartsX added in v0.1.0

func LastNPartsX(s string, n int, sep string) (parts, remain string)

func LastPart added in v0.1.0

func LastPart(s, sep string) string

func LastPartIdx added in v0.1.0

func LastPartIdx(s, sep string) int

func LastPartX added in v0.1.0

func LastPartX(s, sep string) (last, remain string)

Types

type Builder

type Builder strings.Builder

func (*Builder) Cap

func (b *Builder) Cap() int

func (*Builder) Grow

func (b *Builder) Grow(n int)

func (*Builder) Len

func (b *Builder) Len() int

func (*Builder) Print

func (b *Builder) Print(args ...any)

func (*Builder) PrintBytes

func (b *Builder) PrintBytes(p []byte)

func (*Builder) Printf

func (b *Builder) Printf(format string, args ...any)

func (*Builder) Println

func (b *Builder) Println(args ...any)

func (*Builder) Reset

func (b *Builder) Reset()

func (*Builder) String

func (b *Builder) String() string

func (*Builder) Write

func (b *Builder) Write(p []byte) (int, error)

func (*Builder) WriteByte

func (b *Builder) WriteByte(c byte) error

func (*Builder) WriteByteZ

func (b *Builder) WriteByteZ(c byte)

func (*Builder) WriteRune

func (b *Builder) WriteRune(r rune) (int, error)

func (*Builder) WriteRuneZ

func (b *Builder) WriteRuneZ(r rune) int

func (*Builder) WriteString

func (b *Builder) WriteString(s string) (int, error)

func (*Builder) WriteStringZ

func (b *Builder) WriteStringZ(s string) int

func (*Builder) WriteZ

func (b *Builder) WriteZ(p []byte) int

type BuilderFunc added in v0.1.2

type BuilderFunc func(b *Builder)

StringFunc wraps a function to make it implement fmt.Stringer. It is useful when you want to delay evaluation of the function. The function receives a stringz.Builder. For example:

msg := BuilderFunc(func(b *stringz.Builder) string {
  b.Printf("JSON(obj)=%v", formatJSON(obj))
})
log.Debug(msg)  // 👈 only run formatJSON() if debug level is enabled

func (BuilderFunc) String added in v0.1.2

func (fn BuilderFunc) String() string

type StringFunc added in v0.1.1

type StringFunc func() string

StringFunc wraps a function to make it implement fmt.Stringer. It is useful when you want to delay evaluation of the function. For example:

msg := StringFunc(func() string {
  return fmt.Sprintf("JSON(obj)=%v", formatJSON(obj))
})
log.Debug(msg)  // 👈 only run formatJSON() if debug level is enabled

func (StringFunc) String added in v0.1.1

func (fn StringFunc) String() string

Jump to

Keyboard shortcuts

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