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 ¶
- func Center(str string, length int, pad string) string
- func Count(str, pattern string) int
- func Delete(str, pattern string) string
- func ExpandTabs(str string, tabSize int) string
- func FirstRuneToLower(str string) string
- func FirstRuneToUpper(str string) string
- func Insert(dst, src string, index int) string
- func LastPartition(str, sep string) (head, match, tail string)
- func LeftJustify(str string, length int, pad string) string
- func Len(str string) int
- func Partition(str, sep string) (head, match, tail string)
- func Reverse(str string) string
- func RightJustify(str string, length int, pad string) string
- func RuneWidth(r rune) int
- func Scrub(str, repl string) string
- func Shuffle(str string) string
- func ShuffleSource(str string, src rand.Source) string
- func Slice(str string, start, end int) string
- func Squeeze(str, pattern string) string
- func Successor(str string) string
- func SwapCase(str string) string
- func ToCamelCase(str string) string
- func ToKebabCase(str string) string
- func ToPascalCase(str string) string
- func ToSnakeCase(str string) string
- func Translate(str, from, to string) string
- func Width(str string) int
- func WordCount(str string) int
- func WordSplit(str string) []string
- type Translator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Center ¶
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 ¶
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 ¶
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 ¶
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 ¶
FirstRuneToLower converts first rune to lower case if necessary.
func FirstRuneToUpper ¶
FirstRuneToUpper converts first rune to upper case if necessary.
func Insert ¶
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 ¶
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 ¶
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 Partition ¶
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 RightJustify ¶
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 ¶
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 ¶
Scrub scrubs invalid utf8 bytes with repl string. Adjacent invalid bytes are replaced only once.
func Shuffle ¶
Shuffle randomizes runes in a string and returns the result. It uses default random source in `math/rand`.
func ShuffleSource ¶
ShuffleSource randomizes runes in a string with given random source.
func Slice ¶
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 ¶
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 ¶
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 ToCamelCase ¶
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
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
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 ¶
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 ¶
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:
- '-' 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.
- '^' as first character means a set of all runes excepted listed, e.g. "^a-z" means all characters except 'a' to 'z' inclusive.
- '\' 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 ¶
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 ¶
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.
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.