diff options
author | mjarkk <mkopenga@gmail.com> | 2018-11-10 08:57:02 +0100 |
---|---|---|
committer | mjarkk <mkopenga@gmail.com> | 2018-11-10 08:57:02 +0100 |
commit | 834e42897d5c2190a7f108d9be5c910138c1199e (patch) | |
tree | 7b97e01bb243da689445d9e9ce19ac872066d62d /vendor/github.com | |
parent | 500267417b9779822e908ba7997f9adb9c881cac (diff) |
Switched back to github.com/mgutz/str instaid of a copy of ToArgv
Diffstat (limited to 'vendor/github.com')
-rw-r--r-- | vendor/github.com/mgutz/str/CREDITS | 5 | ||||
-rw-r--r-- | vendor/github.com/mgutz/str/LICENSE | 21 | ||||
-rw-r--r-- | vendor/github.com/mgutz/str/README.md | 649 | ||||
-rw-r--r-- | vendor/github.com/mgutz/str/VERSION | 1 | ||||
-rw-r--r-- | vendor/github.com/mgutz/str/doc.go | 19 | ||||
-rw-r--r-- | vendor/github.com/mgutz/str/funcsAO.go | 337 | ||||
-rw-r--r-- | vendor/github.com/mgutz/str/funcsPZ.go | 534 | ||||
-rw-r--r-- | vendor/github.com/mgutz/str/str_test.go | 696 |
8 files changed, 2262 insertions, 0 deletions
diff --git a/vendor/github.com/mgutz/str/CREDITS b/vendor/github.com/mgutz/str/CREDITS new file mode 100644 index 000000000..ddb244c30 --- /dev/null +++ b/vendor/github.com/mgutz/str/CREDITS @@ -0,0 +1,5 @@ +* [string.js](http://stringjs.com) - I contributed several + functions to this project. + +* [bbgen.net](http://bbgen.net/blog/2011/06/string-to-argc-argv/) + diff --git a/vendor/github.com/mgutz/str/LICENSE b/vendor/github.com/mgutz/str/LICENSE new file mode 100644 index 000000000..6045e6c75 --- /dev/null +++ b/vendor/github.com/mgutz/str/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013-2014 Mario L. Gutierrez <mario@mgutz.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/mgutz/str/README.md b/vendor/github.com/mgutz/str/README.md new file mode 100644 index 000000000..bce814bc2 --- /dev/null +++ b/vendor/github.com/mgutz/str/README.md @@ -0,0 +1,649 @@ +# str + + import "github.com/mgutz/str" + +Package str is a comprehensive set of string functions to build more Go +awesomeness. Str complements Go's standard packages and does not duplicate +functionality found in `strings` or `strconv`. + +Str is based on plain functions instead of object-based methods, consistent with +Go standard string packages. + + str.Between("<a>foo</a>", "<a>", "</a>") == "foo" + +Str supports pipelining instead of chaining + + s := str.Pipe("\nabcdef\n", Clean, BetweenF("a", "f"), ChompLeftF("bc")) + +User-defined filters can be added to the pipeline by inserting a function or +closure that returns a function with this signature + + func(string) string + +### Index + +* [Variables](#variables) +* [func Between](#func +[godoc](https://godoc.org/github.com/mgutz/str) +between) +* [func BetweenF](#func--betweenf) +* [func Camelize](#func--camelize) +* [func Capitalize](#func--capitalize) +* [func CharAt](#func--charat) +* [func CharAtF](#func--charatf) +* [func ChompLeft](#func--chompleft) +* [func ChompLeftF](#func--chompleftf) +* [func ChompRight](#func--chompright) +* [func ChompRightF](#func--chomprightf) +* [func Classify](#func--classify) +* [func ClassifyF](#func--classifyf) +* [func Clean](#func--clean) +* [func Dasherize](#func--dasherize) +* [func DecodeHTMLEntities](#func--decodehtmlentities) +* [func EnsurePrefix](#func--ensureprefix) +* [func EnsurePrefixF](#func--ensureprefixf) +* [func EnsureSuffix](#func--ensuresuffix) +* [func EnsureSuffixF](#func--ensuresuffixf) +* [func EscapeHTML](#func--escapehtml) +* [func Humanize](#func--humanize) +* [func Iif](#func--iif) +* [func IndexOf](#func--indexof) +* [func IsAlpha](#func--isalpha) +* [func IsAlphaNumeric](#func--isalphanumeric) +* [func IsEmpty](#func--isempty) +* [func IsLower](#func--islower) +* [func IsNumeric](#func--isnumeric) +* [func IsUpper](#func--isupper) +* [func Left](#func--left) +* [func LeftF](#func--leftf) +* [func LeftOf](#func--leftof) +* [func Letters](#func--letters) +* [func Lines](#func--lines) +* [func Map](#func--map) +* [func Match](#func--match) +* [func Pad](#func--pad) +* [func PadF](#func--padf) +* [func PadLeft](#func--padleft) +* [func PadLeftF](#func--padleftf) +* [func PadRight](#func--padright) +* [func PadRightF](#func--padrightf) +* [func Pipe](#func--pipe) +* [func QuoteItems](#func--quoteitems) +* [func ReplaceF](#func--replacef) +* [func ReplacePattern](#func--replacepattern) +* [func ReplacePatternF](#func--replacepatternf) +* [func Reverse](#func--reverse) +* [func Right](#func--right) +* [func RightF](#func--rightf) +* [func RightOf](#func--rightof) +* [func SetTemplateDelimiters](#func--settemplatedelimiters) +* [func Slice](#func--slice) +* [func SliceContains](#func--slicecontains) +* [func SliceF](#func--slicef) +* [func SliceIndexOf](#func--sliceindexof) +* [func Slugify](#func--slugify) +* [func StripPunctuation](#func--strippunctuation) +* [func StripTags](#func--striptags) +* [func Substr](#func--substr) +* [func SubstrF](#func--substrf) +* [func Template](#func--template) +* [func TemplateDelimiters](#func--templatedelimiters) +* [func TemplateWithDelimiters](#func--templatewithdelimiters) +* [func ToArgv](#func--toargv) +* [func ToBool](#func--tobool) +* [func ToBoolOr](#func--toboolor) +* [func ToFloat32Or](#func--tofloat32or) +* [func ToFloat64Or](#func--tofloat64or) +* [func ToIntOr](#func--tointor) +* [func Underscore](#func--underscore) +* [func UnescapeHTML](#func--unescapehtml) +* [func WrapHTML](#func--wraphtml) +* [func WrapHTMLF](#func--wraphtmlf) + + +#### Variables + +```go +var ToFloatOr = ToFloat64Or +``` +ToFloatOr parses as a float64 or returns defaultValue. + +```go +var Verbose = false +``` +Verbose flag enables console output for those functions that have counterparts +in Go's excellent stadard packages. + +#### func [Between](#between) + +```go +func Between(s, left, right string) string +``` +Between extracts a string between left and right strings. + +#### func [BetweenF](#betweenf) + +```go +func BetweenF(left, right string) func(string) string +``` +BetweenF is the filter form for Between. + +#### func [Camelize](#camelize) + +```go +func Camelize(s string) string +``` +Camelize return new string which removes any underscores or dashes and convert a +string into camel casing. + +#### func [Capitalize](#capitalize) + +```go +func Capitalize(s string) string +``` +Capitalize uppercases the first char of s and lowercases the rest. + +#### func [CharAt](#charat) + +```go +func CharAt(s string, index int) string +``` +CharAt returns a string from the character at the specified position. + +#### func [CharAtF](#charatf) + +```go +func CharAtF(index int) func(string) string +``` +CharAtF is the filter form of CharAt. + +#### func [ChompLeft](#chompleft) + +```go +func ChompLeft(s, prefix string) string +``` +ChompLeft removes prefix at the start of a string. + +#### func [ChompLeftF](#chompleftf) + +```go +func ChompLeftF(prefix string) func(string) string +``` +ChompLeftF is the filter form of ChompLeft. + +#### func [ChompRight](#chompright) + +```go +func ChompRight(s, suffix string) string +``` +ChompRight removes suffix from end of s. + +#### func [ChompRightF](#chomprightf) + +```go +func ChompRightF(suffix string) func(string) string +``` +ChompRightF is the filter form of ChompRight. + +#### func [Classify](#classify) + +```go +func Classify(s string) string +``` +Classify returns a camelized string with the first letter upper cased. + +#### func [ClassifyF](#classifyf) + +```go +func ClassifyF(s string) func(string) string +``` +ClassifyF is the filter form of Classify. + +#### func [Clean](#clean) + +```go +func Clean(s string) string +``` +Clean compresses all adjacent whitespace to a single space and trims s. + +#### func [Dasherize](#dasherize) + +```go +func Dasherize(s string) string +``` +Dasherize converts a camel cased string into a string delimited by dashes. + +#### func [DecodeHTMLEntities](#decodehtmlentities) + +```go +func DecodeHTMLEntities(s string) string +``` +DecodeHTMLEntities decodes HTML entities into their proper string +representation. DecodeHTMLEntities is an alias for html.UnescapeString + +#### func [EnsurePrefix](#ensureprefix) + +```go +func EnsurePrefix(s, prefix string) string +``` +EnsurePrefix ensures s starts with prefix. + +#### func [EnsurePrefixF](#ensureprefixf) + +```go +func EnsurePrefixF(prefix string) func(string) string +``` +EnsurePrefixF is the filter form of EnsurePrefix. + +#### func [EnsureSuffix](#ensuresuffix) + +```go +func EnsureSuffix(s, suffix string) string +``` +EnsureSuffix ensures s ends with suffix. + +#### func [EnsureSuffixF](#ensuresuffixf) + +```go +func EnsureSuffixF(suffix string) func(string) string +``` +EnsureSuffixF is the filter form of EnsureSuffix. + +#### func [EscapeHTML](#escapehtml) + +```go +func EscapeHTML(s string) string +``` +EscapeHTML is alias for html.EscapeString. + +#### func [Humanize](#humanize) + +```go +func Humanize(s string) string +``` +Humanize transforms s into a human friendly form. + +#### func [Iif](#iif) + +```go +func Iif(condition bool, truthy string, falsey string) string +``` +Iif is short for immediate if. If condition is true return truthy else falsey. + +#### func [IndexOf](#indexof) + +```go +func IndexOf(s string, needle string, start int) int +``` +IndexOf finds the index of needle in s starting from start. + +#### func [IsAlpha](#isalpha) + +```go +func IsAlpha(s string) bool +``` +IsAlpha returns true if a string contains only letters from ASCII (a-z,A-Z). +Other letters from other languages are not supported. + +#### func [IsAlphaNumeric](#isalphanumeric) + +```go +func IsAlphaNumeric(s string) bool +``` +IsAlphaNumeric returns true if a string contains letters and digits. + +#### func [IsEmpty](#isempty) + +```go +func IsEmpty(s string) bool +``` +IsEmpty returns true if the string is solely composed of whitespace. + +#### func [IsLower](#islower) + +```go +func IsLower(s string) bool +``` +IsLower returns true if s comprised of all lower case characters. + +#### func [IsNumeric](#isnumeric) + +```go +func IsNumeric(s string) bool +``` +IsNumeric returns true if a string contains only digits from 0-9. Other digits +not in Latin (such as Arabic) are not currently supported. + +#### func [IsUpper](#isupper) + +```go +func IsUpper(s string) bool +``` +IsUpper returns true if s contains all upper case chracters. + +#### func [Left](#left) + +```go +func Left(s string, n int) string +``` +Left returns the left substring of length n. + +#### func [LeftF](#leftf) + +```go +func LeftF(n int) func(string) string +``` +LeftF is the filter form of Left. + +#### func [LeftOf](#leftof) + +```go +func LeftOf(s string, needle string) string +``` +LeftOf returns the substring left of needle. + +#### func [Letters](#letters) + +```go +func Letters(s string) []string +``` +Letters returns an array of runes as strings so it can be indexed into. + +#### func [Lines](#lines) + +```go +func Lines(s string) []string +``` +Lines convert windows newlines to unix newlines then convert to an Array of +lines. + +#### func [Map](#map) + +```go +func Map(arr []string, iterator func(string) string) []string +``` +Map maps an array's iitem through an iterator. + +#### func [Match](#match) + +```go +func Match(s, pattern string) bool +``` +Match returns true if patterns matches the string + +#### func [Pad](#pad) + +```go +func Pad(s, c string, n int) string +``` +Pad pads string s on both sides with c until it has length of n. + +#### func [PadF](#padf) + +```go +func PadF(c string, n int) func(string) string +``` +PadF is the filter form of Pad. + +#### func [PadLeft](#padleft) + +```go +func PadLeft(s, c string, n int) string +``` +PadLeft pads s on left side with c until it has length of n. + +#### func [PadLeftF](#padleftf) + +```go +func PadLeftF(c string, n int) func(string) string +``` +PadLeftF is the filter form of PadLeft. + +#### func [PadRight](#padright) + +```go +func PadRight(s, c string, n int) string +``` +PadRight pads s on right side with c until it has length of n. + +#### func [PadRightF](#padrightf) + +```go +func PadRightF(c string, n int) func(string) string +``` +PadRightF is the filter form of Padright + +#### func [Pipe](#pipe) + +```go +func Pipe(s string, funcs ...func(string) string) string +``` +Pipe pipes s through one or more string filters. + +#### func [QuoteItems](#quoteitems) + +```go +func QuoteItems(arr []string) []string +``` +QuoteItems quotes all items in array, mostly for debugging. + +#### func [ReplaceF](#replacef) + +```go +func ReplaceF(old, new string, n int) func(string) string +``` +ReplaceF is the filter form of strings.Replace. + +#### func [ReplacePattern](#replacepattern) + +```go +func ReplacePattern(s, pattern, repl string) string +``` +ReplacePattern replaces string with regexp string. ReplacePattern returns a copy +of src, replacing matches of the Regexp with the replacement string repl. Inside +repl, $ signs are interpreted as in Expand, so for instance $1 represents the +text of the first submatch. + +#### func [ReplacePatternF](#replacepatternf) + +```go +func ReplacePatternF(pattern, repl string) func(string) string +``` +ReplacePatternF is the filter form of ReplaceRegexp. + +#### func [Reverse](#reverse) + +```go +func Reverse(s string) string +``` +Reverse a string + +#### func [Right](#right) + +```go +func Right(s string, n int) string +``` +Right returns the right substring of length n. + +#### func [RightF](#rightf) + +```go +func RightF(n int) func(string) string +``` +RightF is the Filter version of Right. + +#### func [RightOf](#rightof) + +```go +func RightOf(s string, prefix string) string +``` +RightOf returns the substring to the right of prefix. + +#### func [SetTemplateDelimiters](#settemplatedelimiters) + +```go +func SetTemplateDelimiters(opening, closing string) +``` +SetTemplateDelimiters sets the delimiters for Template function. Defaults to +"{{" and "}}" + +#### func [Slice](#slice) + +```go +func Slice(s string, start, end int) string +``` +Slice slices a string. If end is negative then it is the from the end of the +string. + +#### func [SliceContains](#slicecontains) + +```go +func SliceContains(slice []string, val string) bool +``` +SliceContains determines whether val is an element in slice. + +#### func [SliceF](#slicef) + +```go +func SliceF(start, end int) func(string) string +``` +SliceF is the filter for Slice. + +#### func [SliceIndexOf](#sliceindexof) + +```go +func SliceIndexOf(slice []string, val string) int +``` +SliceIndexOf gets the indx of val in slice. Returns -1 if not found. + +#### func [Slugify](#slugify) + +```go +func Slugify(s string) string +``` +Slugify converts s into a dasherized string suitable for URL segment. + +#### func [StripPunctuation](#strippunctuation) + +```go +func StripPunctuation(s string) string +``` +StripPunctuation strips puncation from string. + +#### func [StripTags](#striptags) + +```go +func StripTags(s string, tags ...string) string +``` +StripTags strips all of the html tags or tags specified by the parameters + +#### func [Substr](#substr) + +```go +func Substr(s string, index int, n int) string +``` +Substr returns a substring of s starting at index of length n. + +#### func [SubstrF](#substrf) + +```go +func SubstrF(index, n int) func(string) string +``` +SubstrF is the filter form of Substr. + +#### func [Template](#template) + +```go +func Template(s string, values map[string]interface{}) string +``` +Template is a string template which replaces template placeholders delimited by +"{{" and "}}" with values from map. The global delimiters may be set with +SetTemplateDelimiters. + +#### func [TemplateDelimiters](#templatedelimiters) + +```go +func TemplateDelimiters() (opening string, closing string) +``` +TemplateDelimiters is the getter for the opening and closing delimiters for +Template. + +#### func [TemplateWithDelimiters](#templatewithdelimiters) + +```go +func TemplateWithDelimiters(s string, values map[string]interface{}, opening, closing string) string +``` +TemplateWithDelimiters is string template with user-defineable opening and +closing delimiters. + +#### func [ToArgv](#toargv) + +```go +func ToArgv(s string) []string +``` +ToArgv converts string s into an argv for exec. + +#### func [ToBool](#tobool) + +```go +func ToBool(s string) bool +``` +ToBool fuzzily converts truthy values. + +#### func [ToBoolOr](#toboolor) + +```go +func ToBoolOr(s string, defaultValue bool) bool +``` +ToBoolOr parses s as a bool or returns defaultValue. + +#### func [ToFloat32Or](#tofloat32or) + +```go +func ToFloat32Or(s string, defaultValue float32) float32 +``` +ToFloat32Or parses as a float32 or returns defaultValue on error. + +#### func [ToFloat64Or](#tofloat64or) + +```go +func ToFloat64Or(s string, defaultValue float64) float64 +``` +ToFloat64Or parses s as a float64 or returns defaultValue. + +#### func [ToIntOr](#tointor) + +```go +func ToIntOr(s string, defaultValue int) int +``` +ToIntOr parses s as an int or returns defaultValue. + +#### func [Underscore](#underscore) + +```go +func Underscore(s string) string +``` +Underscore returns converted camel cased string into a string delimited by +underscores. + +#### func [UnescapeHTML](#unescapehtml) + +```go +func UnescapeHTML(s string) string +``` +UnescapeHTML is an alias for html.UnescapeString. + +#### func [WrapHTML](#wraphtml) + +```go +func WrapHTML(s string, tag string, attrs map[string]string) string +``` +WrapHTML wraps s within HTML tag having attributes attrs. Note, WrapHTML does +not escape s value. + +#### func [WrapHTMLF](#wraphtmlf) + +```go +func WrapHTMLF(tag string, attrs map[string]string) func(string) string +``` +WrapHTMLF is the filter form of WrapHTML. diff --git a/vendor/github.com/mgutz/str/VERSION b/vendor/github.com/mgutz/str/VERSION new file mode 100644 index 000000000..9084fa2f7 --- /dev/null +++ b/vendor/github.com/mgutz/str/VERSION @@ -0,0 +1 @@ +1.1.0 diff --git a/vendor/github.com/mgutz/str/doc.go b/vendor/github.com/mgutz/str/doc.go new file mode 100644 index 000000000..f48742a1f --- /dev/null +++ b/vendor/github.com/mgutz/str/doc.go @@ -0,0 +1,19 @@ +// Package str is a comprehensive set of string functions to build more +// Go awesomeness. Str complements Go's standard packages and does not duplicate +// functionality found in `strings` or `strconv`. +// +// Str is based on plain functions instead of object-based methods, +// consistent with Go standard string packages. +// +// str.Between("<a>foo</a>", "<a>", "</a>") == "foo" +// +// Str supports pipelining instead of chaining +// +// s := str.Pipe("\nabcdef\n", Clean, BetweenF("a", "f"), ChompLeftF("bc")) +// +// User-defined filters can be added to the pipeline by inserting a function +// or closure that returns a function with this signature +// +// func(string) string +// +package str diff --git a/vendor/github.com/mgutz/str/funcsAO.go b/vendor/github.com/mgutz/str/funcsAO.go new file mode 100644 index 000000000..fd17c1c12 --- /dev/null +++ b/vendor/github.com/mgutz/str/funcsAO.go @@ -0,0 +1,337 @@ +package str + +import ( + "fmt" + "html" + //"log" + "regexp" + "strings" +) + +// Verbose flag enables console output for those functions that have +// counterparts in Go's excellent stadard packages. +var Verbose = false +var templateOpen = "{{" +var templateClose = "}}" + +var beginEndSpacesRe = regexp.MustCompile("^\\s+|\\s+$") +var camelizeRe = regexp.MustCompile(`(\-|_|\s)+(.)?`) +var camelizeRe2 = regexp.MustCompile(`(\-|_|\s)+`) +var capitalsRe = regexp.MustCompile("([A-Z])") +var dashSpaceRe = regexp.MustCompile(`[-\s]+`) +var dashesRe = regexp.MustCompile("-+") +var isAlphaNumericRe = regexp.MustCompile(`[^0-9a-z\xC0-\xFF]`) +var isAlphaRe = regexp.MustCompile(`[^a-z\xC0-\xFF]`) +var nWhitespaceRe = regexp.MustCompile(`\s+`) +var notDigitsRe = regexp.MustCompile(`[^0-9]`) +var slugifyRe = regexp.MustCompile(`[^\w\s\-]`) +var spaceUnderscoreRe = regexp.MustCompile("[_\\s]+") +var spacesRe = regexp.MustCompile("[\\s\\xA0]+") +var stripPuncRe = regexp.MustCompile(`[^\w\s]|_`) +var templateRe = regexp.MustCompile(`([\-\[\]()*\s])`) +var templateRe2 = regexp.MustCompile(`\$`) +var underscoreRe = regexp.MustCompile(`([a-z\d])([A-Z]+)`) +var whitespaceRe = regexp.MustCompile(`^[\s\xa0]*$`) + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +// Between extracts a string between left and right strings. +func Between(s, left, right string) string { + l := len(left) + startPos := strings.Index(s, left) + if startPos < 0 { + return "" + } + endPos := IndexOf(s, right, startPos+l) + //log.Printf("%s: left %s right %s start %d end %d", s, left, right, startPos+l, endPos) + if endPos < 0 { + return "" + } else if right == "" { + return s[endPos:] + } else { + return s[startPos+l : endPos] + } +} + +// BetweenF is the filter form for Between. +func BetweenF(left, right string) func(string) string { + return func(s string) string { + return Between(s, left, right) + } +} + +// Camelize return new string which removes any underscores or dashes and convert a string into camel casing. +func Camelize(s string) string { + return camelizeRe.ReplaceAllStringFunc(s, func(val string) string { + val = strings.ToUpper(val) + val = camelizeRe2.ReplaceAllString(val, "") + return val + }) +} + +// Capitalize uppercases the first char of s and lowercases the rest. +func Capitalize(s string) string { + return strings.ToUpper(s[0:1]) + strings.ToLower(s[1:]) +} + +// CharAt returns a string from the character at the specified position. +func CharAt(s string, index int) string { + l := len(s) + shortcut := index < 0 || index > l-1 || l == 0 + if shortcut { + return "" + } + return s[index : index+1] +} + +// CharAtF is the filter form of CharAt. +func CharAtF(index int) func(string) string { + return func(s string) string { + return CharAt(s, index) + } +} + +// ChompLeft removes prefix at the start of a string. +func ChompLeft(s, prefix string) string { + if strings.HasPrefix(s, prefix) { + return s[len(prefix):] + } + return s +} + +// ChompLeftF is the filter form of ChompLeft. +func ChompLeftF(prefix string) func(string) string { + return func(s string) string { + return ChompLeft(s, prefix) + } +} + +// ChompRight removes suffix from end of s. +func ChompRight(s, suffix string) string { + if strings.HasSuffix(s, suffix) { + return s[:len(s)-len(suffix)] + } + return s +} + +// ChompRightF is the filter form of ChompRight. +func ChompRightF(suffix string) func(string) string { + return func(s string) string { + return ChompRight(s, suffix) + } +} + +// Classify returns a camelized string with the first letter upper cased. +func Classify(s string) string { + return Camelize("-" + s) +} + +// ClassifyF is the filter form of Classify. +func ClassifyF(s string) func(string) string { + return func(s string) string { + return Classify(s) + } +} + +// Clean compresses all adjacent whitespace to a single space and trims s. +func Clean(s string) string { + s = spacesRe.ReplaceAllString(s, " ") + s = beginEndSpacesRe.ReplaceAllString(s, "") + return s +} + +// Dasherize converts a camel cased string into a string delimited by dashes. +func Dasherize(s string) string { + s = strings.TrimSpace(s) + s = spaceUnderscoreRe.ReplaceAllString(s, "-") + s = capitalsRe.ReplaceAllString(s, "-$1") + s = dashesRe.ReplaceAllString(s, "-") + s = strings.ToLower(s) + return s +} + +// EscapeHTML is alias for html.EscapeString. +func EscapeHTML(s string) string { + if Verbose { + fmt.Println("Use html.EscapeString instead of EscapeHTML") + } + return html.EscapeString(s) +} + +// DecodeHTMLEntities decodes HTML entities into their proper string representation. +// DecodeHTMLEntities is an alias for html.UnescapeString +func DecodeHTMLEntities(s string) string { + if Verbose { + fmt.Println("Use html.UnescapeString instead of DecodeHTMLEntities") + } + return html.UnescapeString(s) +} + +// EnsurePrefix ensures s starts with prefix. +func EnsurePrefix(s, prefix string) string { + if strings.HasPrefix(s, prefix) { + return s + } + return prefix + s +} + +// EnsurePrefixF is the filter form of EnsurePrefix. +func EnsurePrefixF(prefix string) func(string) string { + return func(s string) string { + return EnsurePrefix(s, prefix) + } +} + +// EnsureSuffix ensures s ends with suffix. +func EnsureSuffix(s, suffix string) string { + if strings.HasSuffix(s, suffix) { + return s + } + return s + suffix +} + +// EnsureSuffixF is the filter form of EnsureSuffix. +func EnsureSuffixF(suffix string) func(string) string { + return func(s string) string { + return EnsureSuffix(s, suffix) + } +} + +// Humanize transforms s into a human friendly form. +func Humanize(s string) string { + if s == "" { + return s + } + s = Underscore(s) + var humanizeRe = regexp.MustCompile(`_id$`) + s = humanizeRe.ReplaceAllString(s, "") + s = strings.Replace(s, "_", " ", -1) + s = strings.TrimSpace(s) + s = Capitalize(s) + return s +} + +// Iif is short for immediate if. If condition is true return truthy else falsey. +func Iif(condition bool, truthy string, falsey string) string { + if condition { + return truthy + } + return falsey +} + +// IndexOf finds the index of needle in s starting from start. +func IndexOf(s string, needle string, start int) int { + l := len(s) + if needle == "" { + if start < 0 { + return 0 + } else if start < l { + return start + } else { + return l + } + } + if start < 0 || start > l-1 { + return -1 + } + pos := strings.Index(s[start:], needle) + if pos == -1 { + return -1 + } + return start + pos +} + +// IsAlpha returns true if a string contains only letters from ASCII (a-z,A-Z). Other letters from other languages are not supported. +func IsAlpha(s string) bool { + return !isAlphaRe.MatchString(strings.ToLower(s)) +} + +// IsAlphaNumeric returns true if a string contains letters and digits. +func IsAlphaNumeric(s string) bool { + return !isAlphaNumericRe.MatchString(strings.ToLower(s)) +} + +// IsLower returns true if s comprised of all lower case characters. +func IsLower(s string) bool { + return IsAlpha(s) && s == strings.ToLower(s) +} + +// IsNumeric returns true if a string contains only digits from 0-9. Other digits not in Latin (such as Arabic) are not currently supported. +func IsNumeric(s string) bool { + return !notDigitsRe.MatchString(s) +} + +// IsUpper returns true if s contains all upper case chracters. +func IsUpper(s string) bool { + return IsAlpha(s) && s == strings.ToUpper(s) +} + +// IsEmpty returns true if the string is solely composed of whitespace. +func IsEmpty(s string) bool { + if s == "" { + return true + } + return whitespaceRe.MatchString(s) +} + +// Left returns the left substring of length n. +func Left(s string, n int) string { + if n < 0 { + return Right(s, -n) + } + return Substr(s, 0, n) +} + +// LeftF is the filter form of Left. +func LeftF(n int) func(string) string { + return func(s string) string { |