strutil

package module
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: MIT Imports: 7 Imported by: 0

README

String Utility Library

A string manipulation utility to complement the golang standard strings library.

Escape

You want to substitute all occurances of a word or character in a string with something else (Escape), then do the reverse (Unescape) to get back the original string. But what if that original string already contains the substitute?

src := "a %reserved% message for john & cage llc"

// a &1reserved&1 message for john &2 cage llc
escaped := strutil.Escape(src, "%", "&")

// a %reserved% message for john & cage llc
recovered := strutil.Unescape(escaped, "%", "&")

Substring

Get the first/last n characters without worrying about out of range error or negative index:

strutil.Prefix("foobar", 300)  // foobar
strutil.Suffix("foobar", 1)    // r
strutil.Prefix("foobar", -2)   // ar

Get everything before/after a substring without messing with runes:

strutil.Before("hello world", " w") // hello
strutil.After("hello world", " ")   // world

Get substring sandwiched between other substrings. Performs better than regex:

src := "we have <tag1> and <tag2> to extract"
strutil.Between(src, []string{"<"}, []string{">"}, -1) // [tag1, tag2]

String slice

Remove empty in slice:

src := []string{"fish", "cow", "bird", "", "banana", "", "coffee", ""}
strutil.RemoveEmpty(&src)
// value of src: [fish, cow, bird, banana, coffee]

A more generic select function:

src := []string{"fish", "cow", "bird", "", "banana", "", "coffee", ""}
strutil.Select(&src, func(s string) bool {
	return len(s) > 4
})
// value of src: [banana, coffee]

Documentation

Overview

Package strutil gets your string manipulation covered! Features include escaping reserved words, wildcard compare, and pattern extraction.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(s, substr string) string

After returns all characters after the the specified substring

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := "baby123"
	after1 := strutil.After(src, "1")
	after2 := strutil.After(src, "?")
	after3 := strutil.After(src, "by")
	after4 := strutil.After(src, "")
	after5 := strutil.After("", "xxx")

	fmt.Println("-->" + after1 + "<--")
	fmt.Println("-->" + after2 + "<--")
	fmt.Println("-->" + after3 + "<--")
	fmt.Println("-->" + after4 + "<--")
	fmt.Println("-->" + after5 + "<--")

}
Output:

-->23<--
-->baby123<--
-->123<--
-->baby123<--
--><--

func Before

func Before(s, substr string) string

Before returns all characters before the the specified substring

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := "baby123"
	before1 := strutil.Before(src, "1")
	before2 := strutil.Before(src, "?")
	before3 := strutil.Before(src, "b")
	before4 := strutil.Before(src, "")
	before5 := strutil.Before("", "xxx")

	fmt.Println("-->" + before1 + "<--")
	fmt.Println("-->" + before2 + "<--")
	fmt.Println("-->" + before3 + "<--")
	fmt.Println("-->" + before4 + "<--")
	fmt.Println("-->" + before5 + "<--")

}
Output:

-->baby<--
-->baby123<--
--><--
-->baby123<--
--><--

func Between

func Between(s string, head, tail []string, first int) []string

Between returns all substrings sandwiched between the specified head and tail

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := "we have <tag1> and <tag2> to extract"
	extracted := strutil.Between(src, []string{"<"}, []string{">"}, -1)
	for _, q := range extracted {
		fmt.Println("extracted text: " + q)
	}

	src2 := "substring inside $ and @ and ++123#more between and stuff @blahblah blah"
	extracted2 := strutil.Between(src2, []string{"$", "@", "++"}, []string{"#", "@"}, -1)
	for _, q := range extracted2 {
		fmt.Println("extracted2 text: " + q)
	}

}
Output:

extracted text: tag1
extracted text: tag2
extracted2 text: 123

func Escape

func Escape(text, reserved, subst string) string

Escape replaces all occurances of reserved with substitute in text, but makes sure that any substitute already in text can be recovered using Unescape().

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := "a %reserved% message for john & cage llc"
	escaped := strutil.Escape(src, "%", "&")
	recovered := strutil.Unescape(escaped, "%", "&")

	fmt.Println("Original text: " + src)
	fmt.Println("Escaped text: " + escaped)
	fmt.Println("Recovered text: " + recovered)

}
Output:

Original text: a %reserved% message for john & cage llc
Escaped text: a &1reserved&1 message for john &2 cage llc
Recovered text: a %reserved% message for john & cage llc
Example (Subst)
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := "a %reserved% message for john &22 &2 cage llc"
	escaped := strutil.Escape(src, "%", "&")
	recovered := strutil.Unescape(escaped, "%", "&")

	fmt.Println("Original text: " + src)
	fmt.Println("Escaped text: " + escaped)
	fmt.Println("Recovered text: " + recovered)

}
Output:

Original text: a %reserved% message for john &22 &2 cage llc
Escaped text: a &1reserved&1 message for john &222 &22 cage llc
Recovered text: a %reserved% message for john &22 &2 cage llc

func Prefix

func Prefix(s string, length int) string

Prefix returns the specified number of characters from the start of a string. If the number of characters specified is more than the length of the string, the entire string is returned. If the length is negative, characters are returned from the end of the string.

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := "123baby"
	prefix1 := strutil.Prefix(src, 3)
	prefix2 := strutil.Prefix(src, 300)
	prefix3 := strutil.Prefix(src, -3)

	fmt.Println("prefix1: " + prefix1)
	fmt.Println("prefix2: " + prefix2)
	fmt.Println("prefix3: " + prefix3)

}
Output:

prefix1: 123
prefix2: 123baby
prefix3: aby

func Random

func Random(n int, charset ...string) string

Random returns a random string of the specified length. Uses alphanumeric characters by default.

func RandomBase36

func RandomBase36(n int) string

RandomBase36 returns a random string of the specified length in the character range 0-9a-z

func RandomBase62

func RandomBase62(n int) string

RandomBase62 returns a random string of the specified length in the character range 0-9a-zA-Z

func RandomBase64

func RandomBase64(n int) string

RandomBase64 returns a random string of the specified length in the character range 0-9a-zA-Z+/

func RandomHex

func RandomHex(n int) string

RandomHex returns a random string of the specified length in the character range 0-9a-f

func RemoveEmpty

func RemoveEmpty(s *[]string)

RemoveEmpty removes empty strings from a string slice

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := []string{"fish", "cow", "bird", "", "banana", "", "coffee", ""}
	strutil.RemoveEmpty(&src)

	for _, q := range src {
		fmt.Println("item: " + q)
	}

}
Output:

item: fish
item: cow
item: bird
item: banana
item: coffee

func Secret

func Secret(n int, charset ...string) (string, error)

Secret generates a secure random string using crypto/rand. Possible characters are `abcdefghinpqrt`, `ABCDEFGHJKLMNPRSTUWXY`, 0-9, and the symbols `~@_/+:`. Password policy compliance is not handled by this function. Error may be returned due to crypto/rand Read().

func Select

func Select(s *[]string, f func(string) bool)

Select removes strings fror a string slice that does not pass a selector function

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := []string{"fish", "cow", "bird", "", "banana", "", "coffee", ""}
	strutil.Select(&src, func(s string) bool {
		return len(s) > 4
	})

	for _, q := range src {
		fmt.Println("item: " + q)
	}

}
Output:

item: banana
item: coffee

func Suffix

func Suffix(s string, length int) string

Suffix returns the specified number of characters from the end of a string. If the number of characters specified is more than the length of the string, the entire string is returned. If the length is negative, characters are returned from the start of the string.

Example
package main

import (
	"fmt"

	"github.com/gozl/strutil"
)

func main() {
	src := "baby123"
	suffix1 := strutil.Suffix(src, 3)
	suffix2 := strutil.Suffix(src, 300)
	suffix3 := strutil.Suffix(src, -3)

	fmt.Println("suffix1: " + suffix1)
	fmt.Println("suffix2: " + suffix2)
	fmt.Println("suffix3: " + suffix3)

}
Output:

suffix1: 123
suffix2: baby123
suffix3: bab

func Unescape

func Unescape(text, reserved, subst string) string

Unescape recovers text processed by the Escape() function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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