trimmer

package module
v0.0.0-...-c678ff6 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README ΒΆ

trimmer

Go Reference Coveralls

License

Summary

trimmer is an alternative implementation of the standard Go strings.Trim, strings.TrimLeft, and strings.TrimRight functions.

Runes to be trimmed away are stored in a bitset for quick retrieval.

On average, trimmer executes at least twice as fast as strings.Trim.

Spot any bugs? Report them here.

Installation

go get github.com/elliotwutingfeng/trimmer

Example

const charsToTrim string = "@πŸ‘πŸ½ζ–° "
var cutset *intset.Rune = MakeRuneSet(charsToTrim)

fmt.Println(FastTrim("", cutset, TrimBoth))
fmt.Println(strings.Trim("", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim(" ", cutset, TrimBoth))
fmt.Println(strings.Trim(" ", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘πŸ½ζ–°", cutset, TrimBoth))
fmt.Println(strings.Trim("@bπŸ‘πŸ½ζ–°", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘πŸ½ζ–°", cutset, TrimLeft))
fmt.Println(strings.TrimLeft("@bπŸ‘πŸ½ζ–°", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘πŸ½ζ–°", cutset, TrimRight))
fmt.Println(strings.TrimRight("@bπŸ‘πŸ½ζ–°", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘ζ–°", cutset, TrimRight))
fmt.Println(strings.TrimRight("@bπŸ‘ζ–°", "@πŸ‘πŸ½ζ–° "))
//Output:
//
//
//b
//b
//bπŸ‘πŸ½ζ–°
//bπŸ‘πŸ½ζ–°
//@b
//@b
//@b
//@b

Testing

make tests

# Alternatively, run tests without race detection
# Useful for systems that do not support the -race flag like windows/386
# See https://tip.golang.org/src/cmd/dist/test.go
make tests_without_race

Benchmarks

make bench
Results

The following chart indicates total time taken to trim characters @πŸ‘πŸ½ζ–° from strings in https://github.com/minimaxir/big-list-of-naughty-strings. The strings have been modified to include prefix and/or suffix permutations with replacement of up to length 4 from the set @πŸ‘πŸ½ζ–°.

CPU: AMD Ryzen 7 5800X
Time in milliseconds (ms) | Lower is better

  MakeRuneSet ▏ .08

     FastTrim ▏ 252 🟦🟦🟦🟦🟦🟦🟦🟦🟦

 FastTrimLeft ▏ 113 🟦🟦🟦🟦

FastTrimRight ▏ 167 🟦🟦🟦🟦🟦🟦

         Trim ▏ 680 πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯

     TrimLeft ▏ 342 πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯

    TrimRight ▏ 382 πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯

Limitations

The cutset (characters to be trimmed) must be initialized as *intset.Rune first. This incurs a small time cost.

const charsToTrim string = "@πŸ‘πŸ½ζ–° "
var cutset *intset.Rune = MakeRuneSet(charsToTrim) // this incurs a small time cost

Use the Go Standard library if you only need to process a small amount of strings (10 strings or less) per given cutset. However, for larger amounts of strings per given cutset, trimmer will greatly outperform the standard library.

Documentation ΒΆ

Overview ΒΆ

Package trimmer is an alternative implementation of the standard Go strings.Trim, strings.TrimLeft, and strings.TrimRight functions.

Example (Basic) ΒΆ
const charsToTrim string = "@πŸ‘πŸ½ζ–° "
var cutset *intset.Rune = MakeRuneSet(charsToTrim)

fmt.Println(FastTrim("", cutset, TrimBoth))
fmt.Println(strings.Trim("", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim(" ", cutset, TrimBoth))
fmt.Println(strings.Trim(" ", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘πŸ½ζ–°", cutset, TrimBoth))
fmt.Println(strings.Trim("@bπŸ‘πŸ½ζ–°", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘πŸ½ζ–°", cutset, TrimLeft))
fmt.Println(strings.TrimLeft("@bπŸ‘πŸ½ζ–°", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘πŸ½ζ–°", cutset, TrimRight))
fmt.Println(strings.TrimRight("@bπŸ‘πŸ½ζ–°", "@πŸ‘πŸ½ζ–° "))

fmt.Println(FastTrim("@bπŸ‘ζ–°", cutset, TrimRight))
fmt.Println(strings.TrimRight("@bπŸ‘ζ–°", "@πŸ‘πŸ½ζ–° "))
Output:

b
b
bπŸ‘πŸ½ζ–°
bπŸ‘πŸ½ζ–°
@b
@b
@b
@b

Index ΒΆ

Examples ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func FastTrim ΒΆ

func FastTrim(s string, cutset *intset.Rune, mode TrimMode) string

FastTrim works like strings.Trim but uses cutset of type *intset.Rune

func MakeRuneSet ΒΆ

func MakeRuneSet(s string) (iset *intset.Rune)

MakeRuneSet converts a string to a set of unique runes

Types ΒΆ

type TrimMode ΒΆ

type TrimMode int

TrimMode specifies which parts of string to trim for FastTrim()

const (
	// TrimBoth trims both ends of the string
	TrimBoth TrimMode = iota
	// TrimLeft trims the left end of the string
	TrimLeft
	// TrimRight trims the right end of the string
	TrimRight
)

Jump to

Keyboard shortcuts

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