xstrings

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: MIT Imports: 4 Imported by: 420

README

xstrings

Build Status Go Doc Go Report Coverage Status

Go package xstrings is a collection of string functions, which are widely used in other languages but absent in Go package strings.

All functions are well tested and carefully tuned for performance.

Propose a new function

Please review contributing guideline and create new issue to state why it should be included.

Install

Use go get to install this library.

go get github.com/huandu/xstrings

API document

See GoDoc for full document.

Function list

Go functions have a unique naming style. One, who has experience in other language but new in Go, may have difficulties to find out right string function to use.

Here is a list of functions in strings and xstrings with enough extra information about how to map these functions to their friends in other languages. Hope this list could be helpful for fresh gophers.

Package xstrings functions

Keep this table sorted by Function in ascending order.

Function Friends #
Center str.center in Python; String#center in Ruby #30
Count String#count in Ruby #16
Delete String#delete in Ruby #17
ExpandTabs str.expandtabs in Python #27
FirstRuneToLower lcfirst in PHP or Perl #15
FirstRuneToUpper String#capitalize in Ruby; ucfirst in PHP or Perl #15
Insert String#insert in Ruby #18
LastPartition str.rpartition in Python; String#rpartition in Ruby #19
LeftJustify str.ljust in Python; String#ljust in Ruby #28
Len mb_strlen in PHP #23
Partition str.partition in Python; String#partition in Ruby #10
Reverse String#reverse in Ruby; strrev in PHP; reverse in Perl #7
RightJustify str.rjust in Python; String#rjust in Ruby #29
RuneWidth - #27
Scrub String#scrub in Ruby #20
Shuffle str_shuffle in PHP #13
ShuffleSource str_shuffle in PHP #13
Slice mb_substr in PHP #9
Squeeze String#squeeze in Ruby #11
Successor String#succ or String#next in Ruby #22
SwapCase str.swapcase in Python; String#swapcase in Ruby #12
ToCamelCase String#camelize in RoR #1
ToKebab - #41
ToPascalCase - #1
ToSnakeCase String#underscore in RoR #1
Translate str.translate in Python; String#tr in Ruby; strtr in PHP; tr/// in Perl #21
Width mb_strwidth in PHP #26
WordCount str_word_count in PHP #14
WordSplit - #14
Package strings functions

Keep this table sorted by Function in ascending order.

Function Friends
Contains String#include? in Ruby
ContainsAny -
ContainsRune -
Count str.count in Python; substr_count in PHP
EqualFold stricmp in PHP; String#casecmp in Ruby
Fields str.split in Python; split in Perl; String#split in Ruby
FieldsFunc -
HasPrefix str.startswith in Python; String#start_with? in Ruby
HasSuffix str.endswith in Python; String#end_with? in Ruby
Index str.index in Python; String#index in Ruby; strpos in PHP; index in Perl
IndexAny -
IndexByte -
IndexFunc -
IndexRune -
Join str.join in Python; Array#join in Ruby; implode in PHP; join in Perl
LastIndex str.rindex in Python; String#rindex; strrpos in PHP; rindex in Perl
LastIndexAny -
LastIndexFunc -
Map String#each_codepoint in Ruby
Repeat operator * in Python and Ruby; str_repeat in PHP
Replace str.replace in Python; String#sub in Ruby; str_replace in PHP
Split str.split in Python; String#split in Ruby; explode in PHP; split in Perl
SplitAfter -
SplitAfterN -
SplitN str.split in Python; String#split in Ruby; explode in PHP; split in Perl
Title str.title in Python
ToLower str.lower in Python; String#downcase in Ruby; strtolower in PHP; lc in Perl
ToLowerSpecial -
ToTitle -
ToTitleSpecial -
ToUpper str.upper in Python; String#upcase in Ruby; strtoupper in PHP; uc in Perl
ToUpperSpecial -
Trim str.strip in Python; String#strip in Ruby; trim in PHP
TrimFunc -
TrimLeft str.lstrip in Python; String#lstrip in Ruby; ltrim in PHP
TrimLeftFunc -
TrimPrefix -
TrimRight str.rstrip in Python; String#rstrip in Ruby; rtrim in PHP
TrimRightFunc -
TrimSpace str.strip in Python; String#strip in Ruby; trim in PHP
TrimSuffix String#chomp in Ruby; chomp in Perl

License

This library is licensed under MIT license. See LICENSE for details.

Documentation

Overview

Package xstrings is to provide string algorithms which are useful but not included in `strings` package. See project home page for details. https://github.com/huandu/xstrings

Package xstrings assumes all strings are encoded in utf8.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Center

func Center(str string, length int, pad string) string

Center returns a string with pad string at both side if str's rune length is smaller than length. If str's rune length is larger than length, str itself will be returned.

If pad is an empty string, str will be returned.

Samples:

Center("hello", 4, " ")    => "hello"
Center("hello", 10, " ")   => "  hello   "
Center("hello", 10, "123") => "12hello123"

func Count

func Count(str, pattern string) int

Count how many runes in str match the pattern. Pattern is defined in Translate function.

Samples:

Count("hello", "aeiou") => 3
Count("hello", "a-k")   => 3
Count("hello", "^a-k")  => 2

func Delete

func Delete(str, pattern string) string

Delete runes in str matching the pattern. Pattern is defined in Translate function.

Samples:

Delete("hello", "aeiou") => "hll"
Delete("hello", "a-k")   => "llo"
Delete("hello", "^a-k")  => "he"

func ExpandTabs

func ExpandTabs(str string, tabSize int) string

ExpandTabs can expand tabs ('\t') rune in str to one or more spaces dpending on current column and tabSize. The column number is reset to zero after each newline ('\n') occurring in the str.

ExpandTabs uses RuneWidth to decide rune's width. For example, CJK characters will be treated as two characters.

If tabSize <= 0, ExpandTabs panics with error.

Samples:

ExpandTabs("a\tbc\tdef\tghij\tk", 4) => "a   bc  def ghij    k"
ExpandTabs("abcdefg\thij\nk\tl", 4)  => "abcdefg hij\nk   l"
ExpandTabs("z中\t文\tw", 4)           => "z中 文  w"

func FirstRuneToLower

func FirstRuneToLower(str string) string

FirstRuneToLower converts first rune to lower case if necessary.

func FirstRuneToUpper

func FirstRuneToUpper(str string) string

FirstRuneToUpper converts first rune to upper case if necessary.

func Insert

func Insert(dst, src string, index int) string

Insert src into dst at given rune index. Index is counted by runes instead of bytes.

If index is out of range of dst, panic with out of range.

func LastPartition

func LastPartition(str, sep string) (head, match, tail string)

LastPartition splits a string by last instance of sep into three parts. The return value is a slice of strings with head, match and tail.

If str contains sep, for example "hello" and "l", LastPartition returns

"hel", "l", "o"

If str doesn't contain sep, for example "hello" and "x", LastPartition returns

"", "", "hello"

func LeftJustify

func LeftJustify(str string, length int, pad string) string

LeftJustify returns a string with pad string at right side if str's rune length is smaller than length. If str's rune length is larger than length, str itself will be returned.

If pad is an empty string, str will be returned.

Samples:

LeftJustify("hello", 4, " ")    => "hello"
LeftJustify("hello", 10, " ")   => "hello     "
LeftJustify("hello", 10, "123") => "hello12312"

func Len

func Len(str string) int

Len returns str's utf8 rune length.

func Partition

func Partition(str, sep string) (head, match, tail string)

Partition splits a string by sep into three parts. The return value is a slice of strings with head, match and tail.

If str contains sep, for example "hello" and "l", Partition returns

"he", "l", "lo"

If str doesn't contain sep, for example "hello" and "x", Partition returns

"hello", "", ""

func Reverse

func Reverse(str string) string

Reverse a utf8 encoded string.

func RightJustify

func RightJustify(str string, length int, pad string) string

RightJustify returns a string with pad string at left side if str's rune length is smaller than length. If str's rune length is larger than length, str itself will be returned.

If pad is an empty string, str will be returned.

Samples:

RightJustify("hello", 4, " ")    => "hello"
RightJustify("hello", 10, " ")   => "     hello"
RightJustify("hello", 10, "123") => "12312hello"

func RuneWidth

func RuneWidth(r rune) int

RuneWidth returns character width in monotype font. Multi-byte characters are usually twice the width of single byte characters.

Algorithm comes from `mb_strwidth` in PHP. http://php.net/manual/en/function.mb-strwidth.php

func Scrub

func Scrub(str, repl string) string

Scrub scrubs invalid utf8 bytes with repl string. Adjacent invalid bytes are replaced only once.

func Shuffle

func Shuffle(str string) string

Shuffle randomizes runes in a string and returns the result. It uses default random source in `math/rand`.

func ShuffleSource

func ShuffleSource(str string, src rand.Source) string

ShuffleSource randomizes runes in a string with given random source.

func Slice

func Slice(str string, start, end int) string

Slice a string by rune.

Start must satisfy 0 <= start <= rune length.

End can be positive, zero or negative. If end >= 0, start and end must satisfy start <= end <= rune length. If end < 0, it means slice to the end of string.

Otherwise, Slice will panic as out of range.

func Squeeze

func Squeeze(str, pattern string) string

Squeeze deletes adjacent repeated runes in str. If pattern is not empty, only runes matching the pattern will be squeezed.

Samples:

Squeeze("hello", "")             => "helo"
Squeeze("hello", "m-z")          => "hello"
Squeeze("hello   world", " ")    => "hello world"

func Successor

func Successor(str string) string

Successor returns the successor to string.

If there is one alphanumeric rune is found in string, increase the rune by 1. If increment generates a "carry", the rune to the left of it is incremented. This process repeats until there is no carry, adding an additional rune if necessary.

If there is no alphanumeric rune, the rightmost rune will be increased by 1 regardless whether the result is a valid rune or not.

Only following characters are alphanumeric.

  • a - z
  • A - Z
  • 0 - 9

Samples (borrowed from ruby's String#succ document):

"abcd"      => "abce"
"THX1138"   => "THX1139"
"<<koala>>" => "<<koalb>>"
"1999zzz"   => "2000aaa"
"ZZZ9999"   => "AAAA0000"
"***"       => "**+"

func SwapCase

func SwapCase(str string) string

SwapCase will swap characters case from upper to lower or lower to upper.

func ToCamelCase

func ToCamelCase(str string) string

ToCamelCase is to convert words separated by space, underscore and hyphen to camel case.

Some samples.

"some_words"      => "someWords"
"http_server"     => "httpServer"
"no_https"        => "noHttps"
"_complex__case_" => "_complex_Case_"
"some words"      => "someWords"
"GOLANG_IS_GREAT" => "golangIsGreat"

func ToKebabCase added in v1.2.0

func ToKebabCase(str string) string

ToKebabCase can convert all upper case characters in a string to kebab case format.

Some samples.

"FirstName"    => "first-name"
"HTTPServer"   => "http-server"
"NoHTTPS"      => "no-https"
"GO_PATH"      => "go-path"
"GO PATH"      => "go-path"  // space is converted to '-'.
"GO-PATH"      => "go-path"  // hyphen is converted to '-'.
"http2xx"      => "http-2xx" // insert an underscore before a number and after an alphabet.
"HTTP20xOK"    => "http-20x-ok"
"Duration2m3s" => "duration-2m3s"
"Bld4Floor3rd" => "bld4-floor-3rd"

func ToPascalCase added in v1.5.0

func ToPascalCase(str string) string

ToPascalCase is to convert words separated by space, underscore and hyphen to pascal case.

Some samples.

"some_words"      => "SomeWords"
"http_server"     => "HttpServer"
"no_https"        => "NoHttps"
"_complex__case_" => "_Complex_Case_"
"some words"      => "SomeWords"
"GOLANG_IS_GREAT" => "GolangIsGreat"

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase can convert all upper case characters in a string to snake case format.

Some samples.

"FirstName"    => "first_name"
"HTTPServer"   => "http_server"
"NoHTTPS"      => "no_https"
"GO_PATH"      => "go_path"
"GO PATH"      => "go_path"  // space is converted to underscore.
"GO-PATH"      => "go_path"  // hyphen is converted to underscore.
"http2xx"      => "http_2xx" // insert an underscore before a number and after an alphabet.
"HTTP20xOK"    => "http_20x_ok"
"Duration2m3s" => "duration_2m3s"
"Bld4Floor3rd" => "bld4_floor_3rd"

func Translate

func Translate(str, from, to string) string

Translate str with the characters defined in from replaced by characters defined in to.

From and to are patterns representing a set of characters. Pattern is defined as following.

Special characters:

  1. '-' means a range of runes, e.g. "a-z" means all characters from 'a' to 'z' inclusive; "z-a" means all characters from 'z' to 'a' inclusive.
  2. '^' as first character means a set of all runes excepted listed, e.g. "^a-z" means all characters except 'a' to 'z' inclusive.
  3. '\' escapes special characters.

Normal character represents itself, e.g. "abc" is a set including 'a', 'b' and 'c'.

Translate will try to find a 1:1 mapping from from to to. If to is smaller than from, last rune in to will be used to map "out of range" characters in from.

Note that '^' only works in the from pattern. It will be considered as a normal character in the to pattern.

If the to pattern is an empty string, Translate works exactly the same as Delete.

Samples:

Translate("hello", "aeiou", "12345")    => "h2ll4"
Translate("hello", "a-z", "A-Z")        => "HELLO"
Translate("hello", "z-a", "a-z")        => "svool"
Translate("hello", "aeiou", "*")        => "h*ll*"
Translate("hello", "^l", "*")           => "**ll*"
Translate("hello ^ world", `\^lo`, "*") => "he*** * w*r*d"

func Width

func Width(str string) int

Width returns string width in monotype font. Multi-byte characters are usually twice the width of single byte characters.

Algorithm comes from `mb_strwidth` in PHP. http://php.net/manual/en/function.mb-strwidth.php

func WordCount

func WordCount(str string) int

WordCount returns number of words in a string.

Word is defined as a locale dependent string containing alphabetic characters, which may also contain but not start with `'` and `-` characters.

func WordSplit

func WordSplit(str string) []string

WordSplit splits a string into words. Returns a slice of words. If there is no word in a string, return nil.

Word is defined as a locale dependent string containing alphabetic characters, which may also contain but not start with `'` and `-` characters.

Types

type Translator

type Translator struct {
	// contains filtered or unexported fields
}

Translator can translate string with pre-compiled from and to patterns. If a from/to pattern pair needs to be used more than once, it's recommended to create a Translator and reuse it.

func NewTranslator

func NewTranslator(from, to string) *Translator

NewTranslator creates new Translator through a from/to pattern pair.

func (*Translator) HasPattern

func (tr *Translator) HasPattern() bool

HasPattern returns true if Translator has one pattern at least.

func (*Translator) Translate

func (tr *Translator) Translate(str string) string

Translate str with a from/to pattern pair.

See comment in Translate function for usage and samples.

func (*Translator) TranslateRune

func (tr *Translator) TranslateRune(r rune) (result rune, translated bool)

TranslateRune return translated rune and true if r matches the from pattern. If r doesn't match the pattern, original r is returned and translated is false.

Jump to

Keyboard shortcuts

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