photoprism/pkg/txt/numeric.go
Michael Mayer 52ab802731 Pkg: Apply "golangci-lint" recommendations to txt/... packages #5330
Signed-off-by: Michael Mayer <michael@photoprism.app>
2025-11-21 16:16:42 +01:00

34 lines
474 B
Go

package txt
import (
"strings"
)
// Numeric removes non-numeric characters from a string and returns the result.
func Numeric(s string) string {
if s == "" {
return ""
}
sep := '.'
if c := strings.Count(s, "."); c == 0 || c > 1 {
sep = ','
}
// Remove invalid characters.
s = strings.Map(func(r rune) rune {
switch {
case r == sep:
return '.'
case r == '-':
return '-'
case r < '0' || r > '9':
return -1
}
return r
}, s)
return s
}