Skip to content

Commit

Permalink
Negative number process
Browse files Browse the repository at this point in the history
  • Loading branch information
Fangliding authored Oct 5, 2024
1 parent cb43fee commit 1e771ec
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion infra/conf/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ func (v *Int32Range) UnmarshalJSON(data []byte) error {
return nil
}
// for range value, like "114-514"
pair := strings.SplitN(str, "-", 2)
var pair []string
// Process sth like "-114-514" "-1919--810"
if strings.HasPrefix(str, "-") {
pair = splitFromSecondDash(str)
} else {
pair = strings.SplitN(str, "-", 2)
}
if len(pair) == 2 {
left, err := strconv.Atoi(pair[0])
right, err2 := strconv.Atoi(pair[1])
Expand All @@ -298,3 +304,13 @@ func (r *Int32Range) ensureOrder() {
r.From, r.To = r.To, r.From
}
}

// "-114-514" → ["114","-514"]
// "-1919--810" → ["-1919","-810"]
func splitFromSecondDash(s string) []string {
parts := strings.SplitN(s, "-", 3)
if len(parts) < 3 {
return []string{s}
}
return []string{parts[0] + "-" + parts[1], parts[2]}
}

0 comments on commit 1e771ec

Please sign in to comment.