Refactoring - remove duplicate code

implementUnicodeCharClass
Aadhavan Srinivasan 4 weeks ago
parent fde3784e5a
commit 1a890a1e75

@ -493,20 +493,14 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
} }
} else if re_postfix[i] == 'p' || re_postfix[i] == 'P' { } else if re_postfix[i] == 'p' || re_postfix[i] == 'P' {
charClassInverted := (re_postfix[i] == 'P') charClassInverted := (re_postfix[i] == 'P')
charsInClass := []rune{}
i++ i++
if isUnicodeCharClassLetter(re_postfix[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 { if err != nil {
return nil, err 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] == '{' { } else if re_postfix[i] == '{' {
i++ // Skip opening bracket i++ // Skip opening bracket
unicodeCharClassStr := "" unicodeCharClassStr := ""
@ -514,21 +508,22 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
unicodeCharClassStr += string(re_postfix[i]) unicodeCharClassStr += string(re_postfix[i])
i++ i++
} }
chars, err := unicodeCharClassToRange(unicodeCharClassStr) var err error
charsInClass, err = unicodeCharClassToRange(unicodeCharClassStr)
if err != nil { if err != nil {
return nil, err 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 { } else {
return nil, fmt.Errorf("error parsing unicode character class in expression") 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 } else if re_postfix[i] == '0' { // Octal value
var octVal int64 var octVal int64
var octValStr string 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' { } else if re_postfix[i] == 'p' || re_postfix[i] == 'P' {
charClassInverted := (re_postfix[i] == 'P') charClassInverted := (re_postfix[i] == 'P')
charsInList := []rune{}
i++ i++
if isUnicodeCharClassLetter(re_postfix[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 { if err != nil {
return nil, err 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] == '{' { } else if re_postfix[i] == '{' {
i++ // Skip opening bracket i++ // Skip opening bracket
unicodeCharClassStr := "" unicodeCharClassStr := ""
@ -733,21 +723,21 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
unicodeCharClassStr += string(re_postfix[i]) unicodeCharClassStr += string(re_postfix[i])
i++ i++
} }
charsInList, err := unicodeCharClassToRange(unicodeCharClassStr) var err error
charsInList, err = unicodeCharClassToRange(unicodeCharClassStr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !charClassInverted {
chars = append(chars, newPostfixNode(charsInList...))
} else {
toAppend := newPostfixDotNode()
toAppend.except = append([]postfixNode{}, newPostfixNode(charsInList...))
chars = append(chars, toAppend)
}
} else { } else {
return nil, fmt.Errorf("error parsing unicode character class in expression") 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 } else if re_postfix[i] == '0' { // Octal value
var octVal int64 var octVal int64

Loading…
Cancel
Save