package remap import ( "regexp" ) /* Compile is a convenience shorthand for: var err error var r *remap.ReMap = new(remap.ReMap) if r.Regexp, err = regexp.Compile(expr); err != nil { // ... } It corresponds to [regexp.Compile]. */ func Compile(expr string) (r *ReMap, err error) { var p *regexp.Regexp if p, err = regexp.Compile(expr); err != nil { return } r = &ReMap{ Regexp: p, } return } /* CompilePOSIX is a convenience shorthand for: var err error var r *remap.ReMap = new(remap.ReMap) if r.Regexp, err = regexp.CompilePOSIX(expr); err != nil { // ... } It corresponds to [regexp.CompilePOSIX]. */ func CompilePOSIX(expr string) (r *ReMap, err error) { var p *regexp.Regexp if p, err = regexp.CompilePOSIX(expr); err != nil { return } r = &ReMap{ Regexp: p, } return } /* MustCompile is a convenience shorthand for: var r *remap.ReMap = &remap.ReMap{ Regexp: regexp.MustCompile(expr), } It corresponds to [regexp.MustCompile]. */ func MustCompile(expr string) (r *ReMap) { var err error var p *regexp.Regexp // We panic ourselves instead of wrapping regexp.MustCompile. // Makes debuggers a little more explicit. if p, err = regexp.Compile(expr); err != nil { panic(err) } r = &ReMap{ Regexp: p, } return } /* MustCompilePOSIX is a convenience shorthand for: var r *remap.ReMap = &remap.ReMap{ Regexp: regexp.MustCompilePOSIX(expr), } It corresponds to [regexp.MustCompilePOSIX]. */ func MustCompilePOSIX(expr string) (r *ReMap) { var err error var p *regexp.Regexp // We panic ourselves instead of wrapping regexp.MustCompilePOSIX. // Makes debuggers a little more explicit. if p, err = regexp.CompilePOSIX(expr); err != nil { panic(err) } r = &ReMap{ Regexp: p, } return } /* strIdxSlicer takes string s, and returns the substring marked by idxPair, where: idxPair = [2]int{ , , } That is, to get `oo` from `foobar`, idxPair = [2]int{1, 3} # NOT: #idxPair = [2]int{1, 2} subStr will be empty and matched will be false if: * idxPair[0] < 0 * idxPair[1] < 0 It will panic with [ErrShortStr] if: * idxPair[0] > len(s)-1 * idxPair[1] > len(s) It will panic with [ErrInvalidIdxPair] if: * idxPair[0] > idxPair[1] It will properly handle single-character addresses (i.e. idxPair[0] == idxPair[1]). */ func strIdxSlicer(s string, idxPair [2]int) (subStr string, matched bool) { if idxPair[0] < 0 || idxPair[1] < 0 { return } matched = true if (idxPair[0] > (len(s) - 1)) || (idxPair[1] > len(s)) { panic(ErrShortStr) } if idxPair[0] > idxPair[1] { panic(ErrInvalidIdxPair) } if idxPair[0] == idxPair[1] { // single character subStr = string(s[idxPair[0]]) } else { // multiple characters subStr = s[idxPair[0]:idxPair[1]] } return }