https://github.com/elliotwutingfeng/trimmer
An alternative implementation of the standard Go strings.Trim, strings.TrimLeft, and strings.TrimRight functions. Executes at least twice as fast as strings.Trim.
https://github.com/elliotwutingfeng/trimmer
golang rune string trim unicode whitespace
Last synced: over 1 year ago
JSON representation
An alternative implementation of the standard Go strings.Trim, strings.TrimLeft, and strings.TrimRight functions. Executes at least twice as fast as strings.Trim.
- Host: GitHub
- URL: https://github.com/elliotwutingfeng/trimmer
- Owner: elliotwutingfeng
- License: bsd-3-clause
- Created: 2023-02-06T01:05:30.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-25T10:19:51.000Z (over 2 years ago)
- Last Synced: 2025-01-10T06:49:07.056Z (over 1 year ago)
- Topics: golang, rune, string, trim, unicode, whitespace
- Language: Go
- Homepage:
- Size: 51.8 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# trimmer
[](https://pkg.go.dev/github.com/elliotwutingfeng/trimmer)
[](https://goreportcard.com/report/github.com/elliotwutingfeng/trimmer)
[](https://coveralls.io/github/elliotwutingfeng/trimmer?branch=main)
[](LICENSE)
## Summary
**trimmer** is an alternative implementation of the standard Go [strings.Trim](https://pkg.go.dev/strings#Trim), [strings.TrimLeft](https://pkg.go.dev/strings#TrimLeft), and [strings.TrimRight](https://pkg.go.dev/strings#TrimRight) functions.
Runes to be trimmed away are stored in a [bitset](https://github.com/karlseguin/intset) for quick retrieval.
On average, **trimmer** executes at least *twice* as fast as [strings.Trim](https://pkg.go.dev/strings#Trim).
Spot any bugs? Report them [here](https://github.com/elliotwutingfeng/trimmer/issues).
## Installation
```sh
go get github.com/elliotwutingfeng/trimmer
```
## Example
```go
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
```sh
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
```sh
make bench
```
### Results
The following chart indicates total time taken to trim characters `@👍🏽新` from strings in . The strings have been modified to include prefix and/or suffix permutations with replacement of up to length 4 from the set `@👍🏽新`.
```text
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.
```go
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.