Moved regexp.MustCompile outside functions

This commit is contained in:
Tripon Alexandru-Ionut
2019-05-07 12:46:31 +03:00
committed by Dan Christian Bogos
parent 6411b2afaf
commit 024b1f765e
4 changed files with 40 additions and 26 deletions

View File

@@ -34,6 +34,13 @@ import (
"github.com/cgrates/rpcclient"
)
var (
kamAuthReqRegexp = regexp.MustCompile(CGR_AUTH_REQUEST)
kamCallStartRegexp = regexp.MustCompile(CGR_CALL_START)
kamCallEndRegexp = regexp.MustCompile(CGR_CALL_END)
kamDlgListRegexp = regexp.MustCompile(CGR_DLG_LIST)
)
func NewKamailioAgent(kaCfg *config.KamAgentCfg,
sessionS rpcclient.RpcClientConnection, timezone string) (ka *KamailioAgent) {
ka = &KamailioAgent{
@@ -57,12 +64,10 @@ type KamailioAgent struct {
func (self *KamailioAgent) Connect() error {
var err error
eventHandlers := map[*regexp.Regexp][]func([]byte, string){
regexp.MustCompile(CGR_AUTH_REQUEST): {
self.onCgrAuth},
regexp.MustCompile(CGR_CALL_START): {
self.onCallStart},
regexp.MustCompile(CGR_CALL_END): {self.onCallEnd},
regexp.MustCompile(CGR_DLG_LIST): {self.onDlgList},
kamAuthReqRegexp: {self.onCgrAuth},
kamCallStartRegexp: {self.onCallStart},
kamCallEndRegexp: {self.onCallEnd},
kamDlgListRegexp: {self.onDlgList},
}
errChan := make(chan error)
for _, connCfg := range self.cfg.EvapiConns {

View File

@@ -26,6 +26,11 @@ import (
"github.com/cgrates/cgrates/utils"
)
var (
spltRgxp = regexp.MustCompile(`:s\/`)
rulesRgxp = regexp.MustCompile(`(?:(.*[^\\])\/(.*[^\\])*\/){1,}`)
)
func NewRSRParsers(parsersRules string, allFiltersMatch bool, rsrSeparator string) (prsrs RSRParsers, err error) {
if parsersRules == "" {
return
@@ -167,11 +172,9 @@ func NewRSRParser(parserRules string, allFiltersMatch bool) (rsrParser *RSRParse
return
}
// dynamic content via attributeNames
spltRgxp := regexp.MustCompile(`:s\/`)
spltRules := spltRgxp.Split(parserRules, -1)
rsrParser.attrName = spltRules[0][1:] // in form ~hdr_name
if len(spltRules) > 1 {
rulesRgxp := regexp.MustCompile(`(?:(.*[^\\])\/(.*[^\\])*\/){1,}`)
for _, ruleStr := range spltRules[1:] { // :s/ already removed through split
allMatches := rulesRgxp.FindStringSubmatch(ruleStr)
if len(allMatches) != 3 {

View File

@@ -46,7 +46,24 @@ import (
"github.com/cgrates/rpcclient"
)
var startCGRateSTime time.Time
var (
startCGRateSTime time.Time
rfc3339Rule = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.+$`)
sqlRule = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$`)
utcFormat = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}[T]\d{2}:\d{2}:\d{2}$`)
gotimeRule = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.?\d*\s[+,-]\d+\s\w+$`)
gotimeRule2 = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.?\d*\s[+,-]\d+\s[+,-]\d+$`)
fsTimestamp = regexp.MustCompile(`^\d{16}$`)
astTimestamp = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d*[+,-]\d+$`)
unixTimestampRule = regexp.MustCompile(`^\d{10}$`)
unixTimestampMilisecondsRule = regexp.MustCompile(`^\d{13}$`)
unixTimestampNanosecondsRule = regexp.MustCompile(`^\d{19}$`)
oneLineTimestampRule = regexp.MustCompile(`^\d{14}$`)
oneSpaceTimestampRule = regexp.MustCompile(`^\d{2}\.\d{2}.\d{4}\s{1}\d{2}:\d{2}:\d{2}$`)
eamonTimestampRule = regexp.MustCompile(`^\d{2}/\d{2}/\d{4}\s{1}\d{2}:\d{2}:\d{2}$`)
broadsoftTimestampRule = regexp.MustCompile(`^\d{14}\.\d{3}`)
)
func init() {
startCGRateSTime = time.Now()
@@ -177,20 +194,6 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
if err != nil {
return nilTime, err
}
rfc3339Rule := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.+$`)
sqlRule := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$`)
utcFormat := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}[T]\d{2}:\d{2}:\d{2}$`)
gotimeRule := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.?\d*\s[+,-]\d+\s\w+$`)
gotimeRule2 := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.?\d*\s[+,-]\d+\s[+,-]\d+$`)
fsTimestamp := regexp.MustCompile(`^\d{16}$`)
astTimestamp := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d*[+,-]\d+$`)
unixTimestampRule := regexp.MustCompile(`^\d{10}$`)
unixTimestampMilisecondsRule := regexp.MustCompile(`^\d{13}$`)
unixTimestampNanosecondsRule := regexp.MustCompile(`^\d{19}$`)
oneLineTimestampRule := regexp.MustCompile(`^\d{14}$`)
oneSpaceTimestampRule := regexp.MustCompile(`^\d{2}\.\d{2}.\d{4}\s{1}\d{2}:\d{2}:\d{2}$`)
eamonTimestampRule := regexp.MustCompile(`^\d{2}/\d{2}/\d{4}\s{1}\d{2}:\d{2}:\d{2}$`)
broadsoftTimestampRule := regexp.MustCompile(`^\d{14}\.\d{3}`)
switch {
case tmStr == UNLIMITED || tmStr == "":
// leave it at zero

View File

@@ -24,6 +24,11 @@ import (
"strings"
)
var (
spltRgxp = regexp.MustCompile(`:s\/`)
rulesRgxp = regexp.MustCompile(`(?:(.+[^\\])\/(.*[^\\])*\/){1,}`)
)
func NewRSRField(fldStr string) (fld *RSRField, err error) {
if len(fldStr) == 0 {
return nil, nil
@@ -85,14 +90,12 @@ func NewRSRField(fldStr string) (fld *RSRField, err error) {
rsrField.filters = filters
return rsrField, nil
}
spltRgxp := regexp.MustCompile(`:s\/`)
spltRules := spltRgxp.Split(fldStr, -1)
if len(spltRules) < 2 {
return nil, fmt.Errorf("Invalid Split of Search&Replace field rule. %s", fldStr)
}
rsrField.Id = spltRules[0][1:]
rsrField.filters = filters // Original id in form ~hdr_name
rulesRgxp := regexp.MustCompile(`(?:(.+[^\\])\/(.*[^\\])*\/){1,}`)
rsrField.filters = filters // Original id in form ~hdr_name
for _, ruleStr := range spltRules[1:] { // :s/ already removed through split
allMatches := rulesRgxp.FindStringSubmatch(ruleStr)
if len(allMatches) != 3 {