123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- package main
- import (
- "fmt"
- "regexp"
- "strings"
- "text/template"
- "time"
- )
- func templateNewMap(args ...interface{}) (interface{}, error) {
- result := map[string]interface{}{}
- for i := 1; i < len(args); i += 2 {
- key, ok := args[i-1].(string)
- if !ok {
- return nil, fmt.Errorf("Map: key %v must be string.", key)
- }
- result[key] = args[i]
- }
- return result, nil
- }
- func templateNewList(args ...interface{}) interface{} {
- return args
- }
- func templateToString(v interface{}) string {
- return fmt.Sprintf("%s", v)
- }
- func templateCompileRegexp(reAny interface{}) (interface {}, error) {
- reStr, ok := reAny.(string)
- if !ok {
- return nil, fmt.Errorf("CompileRegexp: %v must be string.", reAny)
- }
- re, err := regexp.Compile(reStr)
- return re, err
- }
- /*
- func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte
- ExpandString is like Expand but the template and source are strings. It
- appends to and returns a byte slice in order to give the calling code
- control over allocation.
- func (re *Regexp) FindAllString(s string, n int) []string
- FindAllString is the 'All' version of FindString; it returns a slice of all
- successive matches of the expression, as defined by the 'All' description in
- the package comment. A return value of nil indicates no match.
- func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
- FindAllStringIndex is the 'All' version of FindStringIndex; it returns a
- slice of all successive matches of the expression, as defined by the 'All'
- description in the package comment. A return value of nil indicates no
- match.
- func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string
- FindAllStringSubmatch is the 'All' version of FindStringSubmatch; it returns
- a slice of all successive matches of the expression, as defined by the 'All'
- description in the package comment. A return value of nil indicates no
- match.
- func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
- FindAllStringSubmatchIndex is the 'All' version of FindStringSubmatchIndex;
- it returns a slice of all successive matches of the expression, as defined
- by the 'All' description in the package comment. A return value of nil
- indicates no match.
- func (re *Regexp) FindString(s string) string
- FindString returns a string holding the text of the leftmost match in s of
- the regular expression. If there is no match, the return value is an empty
- string, but it will also be empty if the regular expression successfully
- matches an empty string. Use FindStringIndex or FindStringSubmatch if it is
- necessary to distinguish these cases.
- func (re *Regexp) FindStringIndex(s string) (loc []int)
- FindStringIndex returns a two-element slice of integers defining the
- location of the leftmost match in s of the regular expression. The match
- itself is at s[loc[0]:loc[1]]. A return value of nil indicates no match.
- func (re *Regexp) FindStringSubmatch(s string) []string
- FindStringSubmatch returns a slice of strings holding the text of the
- leftmost match of the regular expression in s and the matches, if any, of
- its subexpressions, as defined by the 'Submatch' description in the package
- comment. A return value of nil indicates no match.
- func (re *Regexp) FindStringSubmatchIndex(s string) []int
- FindStringSubmatchIndex returns a slice holding the index pairs identifying
- the leftmost match of the regular expression in s and the matches, if any,
- of its subexpressions, as defined by the 'Submatch' and 'Index' descriptions
- in the package comment. A return value of nil indicates no match.
- func (re *Regexp) LiteralPrefix() (prefix string, complete bool)
- LiteralPrefix returns a literal string that must begin any match of the
- regular expression re. It returns the boolean true if the literal string
- comprises the entire regular expression.
-
- func (re *Regexp) MatchString(s string) bool
- MatchString reports whether the string s contains any match of the regular
- expression re.
- func (re *Regexp) FindString(s string) string
- FindString returns a string holding the text of the leftmost match in s of
- the regular expression. If there is no match, the return value is an empty
- string, but it will also be empty if the regular expression successfully
- matches an empty string. Use FindStringIndex or FindStringSubmatch if it is
- necessary to distinguish these cases.
- func (re *Regexp) FindStringIndex(s string) (loc []int)
- FindStringIndex returns a two-element slice of integers defining the
- location of the leftmost match in s of the regular expression. The match
- itself is at s[loc[0]:loc[1]]. A return value of nil indicates no match.
-
- func (re *Regexp) Split(s string, n int) []string
- Split slices s into substrings separated by the expression and returns a
- slice of the substrings between those expression matches.
- The slice returned by this method consists of all the substrings of s not
- contained in the slice returned by FindAllString. When called on an
- expression that contains no metacharacters, it is equivalent to
- strings.SplitN.
- Example:
- s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)
- // s: ["", "b", "b", "c", "cadaaae"]
- The count determines the number of substrings to return:
- n > 0: at most n substrings; the last substring will be the unsplit remainder.
- n == 0: the result is nil (zero substrings)
- n < 0: all substrings
- func (re *Regexp) String() string
- String returns the source text used to compile the regular expression.
- func (re *Regexp) SubexpNames() []string
- SubexpNames returns the names of the parenthesized subexpressions in this
- Regexp. The name for the first sub-expression is names[1], so that if m is a
- match slice, the name for m[i] is SubexpNames()[i]. Since the Regexp as a
- whole cannot be named, names[0] is always the empty string. The slice should
- not be modified.
- */
- func iadd(i1, i2 int) int {
- return i1 + i2
- }
- func isub(i1, i2 int) int {
- return i1 - i2
- }
- func imul(i1, i2 int) int {
- return i1 * i2
- }
- func idiv(i1, i2 int) int {
- return i1 / i2
- }
- func fadd(i1, i2 float64) float64 {
- return i1 + i2
- }
- func fsub(i1, i2 float64) float64 {
- return i1 - i2
- }
- func fmul(i1, i2 float64) float64 {
- return i1 * i2
- }
- func fdiv(i1, i2 float64) float64 {
- return i1 / i2
- }
-
- var templateFunctionMap template.FuncMap = template.FuncMap{
- "CompileRegexp": templateCompileRegexp,
- "Compare": strings.Compare,
- "Contains": strings.Contains,
- "ContainsAny": strings.ContainsAny,
- "ContainsRune": strings.ContainsRune,
- "Count": strings.Count,
- "EqualFold": strings.EqualFold,
- "Fields": strings.Fields,
- "FieldsFunc": strings.FieldsFunc,
- "HasPrefix": strings.HasPrefix,
- "HasSuffix": strings.HasSuffix,
- "Index": strings.Index,
- "IndexAny": strings.IndexAny,
- "IndexByte": strings.IndexByte,
- "IndexFunc": strings.IndexFunc,
- "IndexRune": strings.IndexRune,
- "IsEpsilon": IsEpsilon,
- "IsNonterminal": IsNonterminal,
- "Join": strings.Join,
- "LastIndex": strings.LastIndex,
- "LastIndexAny": strings.LastIndexAny,
- "LastIndexByte": strings.LastIndexByte,
- "LastIndexFunc": strings.LastIndexFunc,
- "Map": strings.Map,
- "MatchString": regexp.MatchString,
- "NewMap": templateNewMap,
- "NewList": templateNewList,
- "Now": time.Now,
- "Repeat": strings.Repeat,
- "Replace": strings.Replace,
- "ReplaceAll": strings.ReplaceAll,
- "Split": strings.Split,
- "SplitAfter": strings.SplitAfter,
- "SplitAfterN": strings.SplitAfterN,
- "SplitN": strings.SplitN,
- "Title": strings.Title,
- "ToLower": strings.ToLower,
- "ToLowerSpecial": strings.ToLowerSpecial,
- "ToString": templateToString,
- "ToTitle": strings.ToTitle,
- "ToTitleSpecial": strings.ToTitleSpecial,
- "ToUpper": strings.ToUpper,
- "ToUpperSpecial": strings.ToUpperSpecial,
- "Trim": strings.Trim,
- "TrimFunc": strings.TrimFunc,
- "TrimLeft": strings.TrimLeft,
- "TrimLeftFunc": strings.TrimLeftFunc,
- "TrimPrefix": strings.TrimPrefix,
- "TrimRight": strings.TrimRight,
- "TrimRightFunc": strings.TrimRightFunc,
- "TrimSpace": strings.TrimSpace,
- "TrimSuffix": strings.TrimSuffix,
- "iadd": iadd,
- "isub": isub,
- "imul": imul,
- "idiv": idiv,
- "fadd": fadd,
- "fsub": fsub,
- "fmul": fmul,
- "fdiv": fdiv,
- }
|