xstrings

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

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 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 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 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 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"

func ToKebabCase

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 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
  • '-' 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

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.

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