strings

package module
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 13 Imported by: 35

README

godoc codecov Go Report Card

strings - text and string-type utilities

strings is a package for manipulating strings of text.

Installation

> go get github.com/go-corelibs/strings@latest

Examples

PathToSnake

func main() {
    snakePath := PathToSnake("/this/path")
    // snakePath == "this__path"
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FancyQuotes = []QuotePair{
		{Start: '“', End: '”'},
		{Start: '‘', End: '’'},
		{Start: '‹', End: '›'},
		{Start: '«', End: '»'},
		{Start: '»', End: '«'},
		{Start: '„', End: '“'},
		{Start: '„', End: '”'},
		{Start: '「', End: '」'},
		{Start: '『', End: '』'},
	}
)

Functions

func AddLastSpace added in v1.1.0

func AddLastSpace(value string) (modified string)

AddLastSpace will add a space to value if value is non-empty and the last rune in value is not unicode.IsSpace

func AppendWithSpace

func AppendWithSpace(src, add string) (combined string)

AppendWithSpace appends add to src with a space, ensuring that only one space is separating the two given strings. If the add string starts with punctuation, no space is used.

AppendWithSpace is intended to be used within the Go-Enjin page format features that need to join text with html constructs. The `.njn` block format for example joins strings and json objects describing html elements into the rendered output.

Enjin Block Example:

{
   "type": "p",
   "text": [
     "This sentence ends with a",
     { "type": "a", "href": "https://go-enjin.org", "text": "link" },
     "."
   ]
}

Is rendered without a space between the "link" and the "." but does have a space after the first sentence text.

func BookendRunes added in v1.3.0

func BookendRunes(input string) (start, end rune, ok bool)

BookendRunes returns the first and last runes of the string, with `ok` being true when there is more than one rune in the input string

func Carve added in v1.4.0

func Carve(src, start, end string) (before, middle, after string, found bool)

Carve finds the `start` and `end` markers in `src` and carves out the "before carve", "middle of start/end range" and "after carve" segments. If the `start` and `end` range is not `found` then the `before` will contain the entire `src` input

Carve is based on two calls to strings.Cut, one for the `start` and again for the `end`

func CollapseSpaces added in v1.8.0

func CollapseSpaces(input string) (collapsed string)

CollapseSpaces returns the input string with all consecutive space characters collapsed to just one space character (tabs are not considered spaces)

func Empty

func Empty(value string) (empty bool)

Empty returns true if the string has a length of zero or is all spaces

func EscapeHtmlAttribute

func EscapeHtmlAttribute(unescaped string) (escaped string)

EscapeHtmlAttribute trims any matching outer quotations (single or double), replaces all double-quotes with `"` and returns the result

func FirstName

func FirstName(fullName string) (firstName string)

FirstName is a wrapper around fullname_parser.ParseFullName

func GetBasicMime

func GetBasicMime(mime string) (basic string)

GetBasicMime removes the semicolon and any trailing bits from the given mime string

func IsAnyQuote added in v1.3.0

func IsAnyQuote[V uint8 | rune](runes ...V) (quote bool)

IsAnyQuote returns true if all `runes` given match IsQuote or IsFancyQuote

func IsFalse deprecated

func IsFalse(text string) bool

IsFalse is a wrapper around values.IsTruthy with the state inverted

Deprecated: please use values.IsTruthy instead

func IsFancyQuote added in v1.3.0

func IsFancyQuote[V uint8 | rune](runes ...V) (quote bool)

IsFancyQuote returns true if all `runes` given match IsFancyQuote

func IsLastSpace added in v1.1.0

func IsLastSpace(value string) (space, ok bool)

IsLastSpace returns ok=true if the string has a length greater than zero and space=true if the last rune is unicode.IsSpace

func IsQuote added in v1.3.0

func IsQuote[V uint8 | rune](runes ...V) (quote bool)

IsQuote returns true if all `runes` given are one of double-quote ("), single-quote (') or backtick (`)

func IsQuoted

func IsQuoted(maybeQuoted string) (quoted bool)

IsQuoted returns true if the first and last characters in the input are the same and are one of the three main quote types: single ('), double (") and literal (`)

func IsQuotedFancy added in v1.2.0

func IsQuotedFancy(maybeQuoted string) (start, end rune, quoted bool)

IsQuotedFancy finds the starting and ending runes, returning true if they match any of the following pairs of quotations:

func IsSpaceOrPunct added in v1.1.0

func IsSpaceOrPunct[T rune | byte](value T) (is bool)

IsSpaceOrPunct returns true if the value given is either unicode.IsSpace or unicode.IsPunct

func IsTrue deprecated

func IsTrue(text string) bool

IsTrue is a wrapper around values.IsTrue

Deprecated: please use values.IsTruthy instead

func LastName

func LastName(fullName string) (lastName string)

LastName is a wrapper around fullname_parser.ParseFullName

func NameFromEmail

func NameFromEmail(email string) (name string)

NameFromEmail returns a user's default name based on just their email address, intended to be used as an interesting placeholder on a text input field for the user to supply something better

func ParseDomainName

func ParseDomainName(input string) (tld, name string, subdomains []string)

ParseDomainName returns the given name split into is component parts, in reverse order

func PathToSnake

func PathToSnake(path string) (snake string)

PathToSnake trims any leading and trailing slashes and converts the string to a snake_cased__directory_separated format where the directory separator is two underscores

func PruneSet added in v1.8.0

func PruneSet(source string, runes ...rune) (pruned string)

PruneSet returns the source string without any of the given runes

func PruneSpaces added in v1.7.0

func PruneSpaces(input string) (pruned string)

PruneSpaces returns the input string without any space characters as defined by unicode.IsSpace

func PruneTmplActions added in v1.1.0

func PruneTmplActions(value string) (clean string)

PruneTmplActions removes all go template action statements

Note that this does not remove the content between if, else and end statements. For example:

`{{ if ... }}stuff{{ else }}moar stuff{{ end }}.`

Turns into:

	`stuff moar stuff.`
       ^
       |

Also note that PruneTmplActions takes care to not concatenate non-space text when pruning surrounding actions though will allow punctuation, similarly to AppendWithSpace.

See: https://pkg.go.dev/text/template#hdr-Actions

func QuoteJsonValue

func QuoteJsonValue(value string) (out string)

QuoteJsonValue is intended to be used when marshalling json content and is applied to only the json values as strings

func Scan added in v1.5.0

func Scan(src, sep string) (before, after string, found bool)

Scan is a text scanner which looks for unquoted and unescaped `sep`

func ScanBothCarve added in v1.6.0

func ScanBothCarve(src, start, end string) (before, middle, after string, found bool)

ScanBothCarve is like ScanCarve except that ScanBothCarve uses Scan to find both the `start` and `end` separators

func ScanCarve added in v1.5.0

func ScanCarve(src, start, end string) (before, middle, after string, found bool)

ScanCarve is like Carve except that ScanCarve uses Scan instead of strings.Cut for finding the `end` separator

func ScanLine added in v1.7.0

func ScanLine(source string, line int) (text string, ok bool)

ScanLine searches for newline characters, counting up to the desired line number and returns just the text for that line. The line number starts at one, not zero

func ScanQuote added in v1.6.0

func ScanQuote(src string) (before, quoted, after string, found bool)

ScanQuote looks for the first single, double or backtick quoted text, returning the `before`, `quoted` and `after` strings if `found`. Note that the `quoted` string is unquoted (and unescaped). Use strconv.Quote to restore double-quoting

func SortedByLastName

func SortedByLastName(fullNames []string) (sorted []string)

SortedByLastName returns a natual-sorted list of the full names given

func SplitSortReversed

func SplitSortReversed(input, separator string) (split []string)

SplitSortReversed is a wrapper around strings.Split and reverses the order of the results

func ToDeepKey

func ToDeepKey(text string) (deepKey string)

ToDeepKey converts go template variables like `.ThisThing.Variable` to a map key used in like `.this-thing.variable`

func ToDeepVar

func ToDeepVar(text string) (deepVar string)

ToDeepVar is the opposite of ToDeepKey, translating `.this-thing.variable` to `.ThisThing.Variable` format

func ToKebabs

func ToKebabs(inputs ...string) (out []string)

ToKebabs converts all the given strings to kebab-case

func ToLowers

func ToLowers(in ...string) (out []string)

ToLowers converts all the given strings to lower case

func ToSpaced

func ToSpaced(text string) (spaced string)

ToSpaced is a wrapper around strcase.ToDelimited with a space delimiter

func ToSpacedCamel

func ToSpacedCamel(text string) (spacedCamel string)

ToSpacedCamel is a wrapper around ToSpaced and strcase.ToCamel for each word. The difference between ToSpacedTitle and ToSpacedCamel is in what is considered a capital letter. ToSpacedTitle uses unicode.ToTitle to figure that out while ToSpacedCamel uses strcase.ToCamel for that

func ToSpacedTitle

func ToSpacedTitle(text string) (spacedTitle string)

ToSpacedTitle is a wrapper around ToSpaced and ToTitleWords

func ToTitleWords

func ToTitleWords(text string) (capitalized string)

ToTitleWords title-cases all words in the given text

func TrimPrefixes

func TrimPrefixes(value string, prefixes ...string) (trimmed string)

TrimPrefixes trims the first path prefix matching value, used to prune known things from arbitrary path strings which may or may not be prefixed with any of the prefixes given

func TrimQuotes

func TrimQuotes(maybeQuoted string) (unquoted string)

TrimQuotes returns the string with the first and last characters trimmed from the string if the string IsQuoted and returns the unmodified input string otherwise

func TrimTmplVar added in v1.1.0

func TrimTmplVar(name string) (trimmed string)

TrimTmplVar returns the name with leading `$` and `.` characters removed

func UniqueFromSpaceSep

func UniqueFromSpaceSep(value string, original []string) (updated []string)

UniqueFromSpaceSep splits the given value on spaces and only appends it to the original slice if not already present, returning the updated results

Types

type ByteBuffer

type ByteBuffer struct {
	bytes.Buffer
}

ByteBuffer is a wrapper around bytes.Buffer which implements the io.Closer interface so that it can be used in io.WriteCloser contexts

func NewByteBuffer

func NewByteBuffer() (c *ByteBuffer)

NewByteBuffer returns a new ByteBuffer instance

func (*ByteBuffer) Close

func (c *ByteBuffer) Close() error

Close fulfils the io.Closer interface and always returns nil because there isn't anything to actually close

type QuotePair added in v1.3.0

type QuotePair struct {
	Start rune
	End   rune
}

func GetFancyQuote added in v1.3.0

func GetFancyQuote[V uint8 | rune](r V) (quote QuotePair, ok bool)

GetFancyQuote returns the FancyQuote pair matching the given rune

type SortByLength

type SortByLength []string

SortByLength implements the sort.Interface, sorting the slice from longest to shortest and sorting equal length strings using natural.Less

func (SortByLength) Len

func (a SortByLength) Len() int

func (SortByLength) Less

func (a SortByLength) Less(i, j int) bool

func (SortByLength) Swap

func (a SortByLength) Swap(i, j int)

Jump to

Keyboard shortcuts

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