From 1a890a1e75aa5212c4625efea60e6b21dd54c098 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 13 Feb 2025 09:10:40 -0500 Subject: [PATCH] Refactoring - remove duplicate code --- regex/compile.go | 60 ++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/regex/compile.go b/regex/compile.go index 62dbfb9..904bf26 100644 --- a/regex/compile.go +++ b/regex/compile.go @@ -493,20 +493,14 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) { } } else if re_postfix[i] == 'p' || re_postfix[i] == 'P' { charClassInverted := (re_postfix[i] == 'P') + charsInClass := []rune{} i++ if isUnicodeCharClassLetter(re_postfix[i]) { - chars, err := unicodeCharClassToRange(string(re_postfix[i])) + var err error + charsInClass, err = unicodeCharClassToRange(string(re_postfix[i])) if err != nil { return nil, err } - var toAppend postfixNode - if !charClassInverted { - toAppend = newPostfixNode(chars...) - } else { - toAppend = newPostfixDotNode() - toAppend.except = append([]postfixNode{}, newPostfixNode(chars...)) - } - outQueue = append(outQueue, toAppend) } else if re_postfix[i] == '{' { i++ // Skip opening bracket unicodeCharClassStr := "" @@ -514,21 +508,22 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) { unicodeCharClassStr += string(re_postfix[i]) i++ } - chars, err := unicodeCharClassToRange(unicodeCharClassStr) + var err error + charsInClass, err = unicodeCharClassToRange(unicodeCharClassStr) if err != nil { return nil, err } - var toAppend postfixNode - if !charClassInverted { // \p - toAppend = newPostfixNode(chars...) - } else { // \P - toAppend = newPostfixDotNode() - toAppend.except = append([]postfixNode{}, newPostfixNode(chars...)) - } - outQueue = append(outQueue, toAppend) } else { return nil, fmt.Errorf("error parsing unicode character class in expression") } + var toAppend postfixNode + if !charClassInverted { // \p + toAppend = newPostfixNode(charsInClass...) + } else { // \P + toAppend = newPostfixDotNode() + toAppend.except = append([]postfixNode{}, newPostfixNode(charsInClass...)) + } + outQueue = append(outQueue, toAppend) } else if re_postfix[i] == '0' { // Octal value var octVal int64 var octValStr string @@ -713,19 +708,14 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) { } } else if re_postfix[i] == 'p' || re_postfix[i] == 'P' { charClassInverted := (re_postfix[i] == 'P') + charsInList := []rune{} i++ if isUnicodeCharClassLetter(re_postfix[i]) { - charsInList, err := unicodeCharClassToRange(string(re_postfix[i])) + var err error + charsInList, err = unicodeCharClassToRange(string(re_postfix[i])) if err != nil { return nil, err } - if !charClassInverted { - chars = append(chars, newPostfixNode(charsInList...)) - } else { - toAppend := newPostfixDotNode() - toAppend.except = append([]postfixNode{}, newPostfixNode(charsInList...)) - chars = append(chars, toAppend) - } } else if re_postfix[i] == '{' { i++ // Skip opening bracket unicodeCharClassStr := "" @@ -733,21 +723,21 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) { unicodeCharClassStr += string(re_postfix[i]) i++ } - charsInList, err := unicodeCharClassToRange(unicodeCharClassStr) + var err error + charsInList, err = unicodeCharClassToRange(unicodeCharClassStr) if err != nil { return nil, err } - if !charClassInverted { - chars = append(chars, newPostfixNode(charsInList...)) - } else { - toAppend := newPostfixDotNode() - toAppend.except = append([]postfixNode{}, newPostfixNode(charsInList...)) - chars = append(chars, toAppend) - } } else { return nil, fmt.Errorf("error parsing unicode character class in expression") } - + if !charClassInverted { + chars = append(chars, newPostfixNode(charsInList...)) + } else { + toAppend := newPostfixDotNode() + toAppend.except = append([]postfixNode{}, newPostfixNode(charsInList...)) + chars = append(chars, toAppend) + } } else if re_postfix[i] == '0' { // Octal value var octVal int64