Skip to content

Commit

Permalink
Added/updated methods for determining a number in a string
Browse files Browse the repository at this point in the history
  • Loading branch information
goloop committed Nov 24, 2024
1 parent e507dcd commit d7ab60f
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 11 deletions.
112 changes: 103 additions & 9 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,58 @@ func Digit(s string) bool {
return true
}

// Numeric is similar to Digit, but also recognizes numeric characters
// used in other number systems.
// Numeric checks whether a string consists only of numeric characters,
// including digits, decimal separators, and an optional '+' or '-' sign at the beginning.
// The function recognizes integers and floating-point numbers, as well as
// digits from various numeral systems and scripts.
//
// Returns true if all characters in the string are any numeric and
// false otherwise. It recognizes numbers (including numbers with a
// decimal point), Roman numerals, Chinese numerals, and more.
// Returns true if all characters in the string are numeric, and false otherwise.
// The function supports decimal separators from different cultures.
//
// Example usage:
//
// is.Numeric("1234") // Output: true
// is.Numeric("Ⅳ") // Output: true
// is.Numeric("3.14") // Output: true
// is.Numeric("-456,789") // Output: true (if comma is considered a separator)
// is.Numeric("Ⅳ") // Output: true
// is.Numeric("三・十四") // Output: true
// is.Numeric("1234abc") // Output: false
// is.Numeric("1.2.3") // Output: false (more than one decimal separator)
func Numeric(s string) bool {
if len(s) == 0 {
return false
}

// Check for chinese numerals and regular digits.
for _, r := range s {
if !unicode.Is(unicode.Number, r) {
var (
hasDecimalSeparator bool
decimalSeparators = map[rune]struct{}{
'.': {}, // Dot
',': {}, // Comma
'·': {}, // Middle dot
'・': {}, // Japanese separator
'٫': {}, // Arabic decimal point
'،': {}, // Arabic comma
'۔': {}, // Urdu decimal point
}
)

for i, r := range s {
// Check if the first character is a sign.
if i == 0 && (r == '+' || r == '-') {
continue
}

// Check if the character is a decimal separator.
if _, isSeparator := decimalSeparators[r]; isSeparator {
if hasDecimalSeparator {
return false // more than one decimal separator
}
hasDecimalSeparator = true
continue
}

// Check if the character is a number.
if !unicode.IsDigit(r) && !unicode.Is(unicode.Number, r) {
if _, ok := numbers[r]; !ok {
return false
}
Expand Down Expand Up @@ -247,6 +279,68 @@ func Decimal(s string) bool {
return true
}

// Float returns true if the string represents a floating-point number,
// where the decimal separator is a dot, and all digits are ASCII digits (0-9).
// It supports an optional '+' or '-' sign at the beginning and at most one
// decimal point. It does not support exponential notation or other numeric
// formats.
//
// Example usage:
//
// is.Float("123.456") // Output: true
// is.Float("-0.001") // Output: true
// is.Float("3.14") // Output: true
// is.Float("123") // Output: true
// is.Float("123.") // Output: true
// is.Float(".456") // Output: true
// is.Float("123.456.789") // Output: false
// is.Float("abc") // Output: false
// is.Float("123a") // Output: false
// is.Float("123,456") // Output: false
func Float(s string) bool {
if len(s) == 0 {
return false
}

var (
hasDecimalPoint bool
hasDigits bool
startIndex int
)

// Check for optional sign at the beginning.
if s[0] == '+' || s[0] == '-' {
startIndex = 1
}

// Edge case: string contains only '+' or '-'.
if startIndex >= len(s) {
return false
}

for i := startIndex; i < len(s); i++ {
r := rune(s[i])

if r == '.' {
if hasDecimalPoint {
return false // more than one decimal point
}
hasDecimalPoint = true
continue
}

if r >= '0' && r <= '9' {
hasDigits = true
continue
}

return false
}

// At least one digit is required.
return hasDigits
}

// Alpha checks whether a string consists only of alphabetic
// characters (letters). It does not recognize digits, special
// characters.
Expand Down
48 changes: 46 additions & 2 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func TestNumeric(t *testing.T) {
{"Ⅳ", true},
{"1234abc", false},
{"", false},
{"1.23", false},
{"1,23", false},
{"1.23", true},
{"1,23", true},
{"三・十四", true},
{"0000", true},
{" 1234", false},
{"1234 ", false},
Expand Down Expand Up @@ -98,6 +99,49 @@ func TestDecimal(t *testing.T) {
}
}

// TestFloat tests the Float function.
func TestFloat(t *testing.T) {
testCases := []struct {
input string
want bool
}{
{"0", true},
{"123", true},
{"123.456", true},
{"+123.456", true},
{"-123.456", true},
{"123.", true},
{".456", true},
{"0.0", true},
{"-.456", true},
{"+.456", true},
{"123.456.789", false},
{"abc", false},
{"123a", false},
{"", false},
{".", false},
{"123,456", false},
{"123.456e7", false}, // exponential notation not supported
{"-0", true},
{"-0.0", true},
{"--123.456", false},
{"123..456", false},
{"12.34.56", false},
{" 123.456", false},
{"123.456 ", false},
{"123.456.789.0", false},
{"+", false},
{"-", false},
}

for _, tc := range testCases {
got := Float(tc.input)
if got != tc.want {
t.Errorf("Float(%q) = %v; want %v", tc.input, got, tc.want)
}
}
}

// TestAlpha tests the Alpha function.
func TestAlpha(t *testing.T) {
tests := []struct {
Expand Down

0 comments on commit d7ab60f

Please sign in to comment.