Skip to content

Commit c182d09

Browse files
committed
✨ feat: strutil - add new func: IsUint, IsPositiveNum
1 parent 8af351f commit c182d09

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

internal/checkfn/check.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,31 @@ func StringsContains(ss []string, s string) bool {
118118
return false
119119
}
120120

121-
// check is number: int or float
122-
var numReg = regexp.MustCompile(`^[-+]?\d*\.?\d+$`)
121+
var (
122+
// check is number: int or float
123+
numReg = regexp.MustCompile(`^[-+]?\d*\.?\d+$`)
124+
// is positive number: int or float
125+
pNumReg = regexp.MustCompile(`^\d*\.?\d+$`)
126+
)
123127

124128
// IsNumeric returns true if the given string is a numeric, otherwise false.
125-
func IsNumeric(s string) bool { return numReg.MatchString(s) }
129+
func IsNumeric(s string) bool {
130+
if s == "" {
131+
return false
132+
}
133+
return numReg.MatchString(s)
134+
}
135+
136+
// IsPositiveNum check input string is positive number
137+
func IsPositiveNum(s string) bool {
138+
if s == "" {
139+
return false
140+
}
141+
if s[0] == '-' {
142+
return false
143+
}
144+
return pNumReg.MatchString(s)
145+
}
126146

127147
// IsHttpURL check input is http/https url
128148
func IsHttpURL(s string) bool {

strutil/check.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,43 @@ var IsHttpURL = checkfn.IsHttpURL
1717
// IsNumChar returns true if the given character is a numeric, otherwise false.
1818
func IsNumChar(c byte) bool { return c >= '0' && c <= '9' }
1919

20-
var intReg = regexp.MustCompile(`^\d+$`)
21-
var floatReg = regexp.MustCompile(`^[-+]?\d*\.?\d+$`)
20+
var (
21+
uintReg = regexp.MustCompile(`^\d+$`)
22+
intReg = regexp.MustCompile(`^[-+]?\d+$`)
23+
24+
floatReg = regexp.MustCompile(`^[-+]?\d*\.?\d+$`)
25+
)
2226

2327
// IsInt check the string is an integer number
24-
func IsInt(s string) bool { return intReg.MatchString(s) }
28+
func IsInt(s string) bool {
29+
if s == "" {
30+
return false
31+
}
32+
return intReg.MatchString(s)
33+
}
34+
35+
// IsUint check the string is an unsigned integer number
36+
func IsUint(s string) bool {
37+
if s == "" {
38+
return false
39+
}
40+
return uintReg.MatchString(s)
41+
}
2542

2643
// IsFloat check the string is a float number
27-
func IsFloat(s string) bool { return floatReg.MatchString(s) }
44+
func IsFloat(s string) bool {
45+
if s == "" {
46+
return false
47+
}
48+
return floatReg.MatchString(s)
49+
}
2850

2951
// IsNumeric returns true if the given string is a numeric(int/float), otherwise false.
3052
func IsNumeric(s string) bool { return checkfn.IsNumeric(s) }
3153

54+
// IsPositiveNum check the string is a positive number
55+
func IsPositiveNum(s string) bool { return checkfn.IsPositiveNum(s) }
56+
3257
// IsAlphabet char
3358
func IsAlphabet(char uint8) bool {
3459
// A 65 -> Z 90

strutil/check_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,26 @@ import (
88
)
99

1010
func TestIsInt(t *testing.T) {
11+
// IsInt
1112
assert.True(t, strutil.IsInt("123"))
1213
assert.True(t, strutil.IsInt("0"))
1314
assert.True(t, strutil.IsInt("456789"))
15+
assert.True(t, strutil.IsInt("-123"))
1416

1517
assert.False(t, strutil.IsInt("123.45"))
1618
assert.False(t, strutil.IsInt("abc"))
1719
assert.False(t, strutil.IsInt("123abc"))
1820
assert.False(t, strutil.IsInt(""))
21+
22+
// IsUint
23+
assert.True(t, strutil.IsUint("123"))
24+
assert.True(t, strutil.IsUint("0"))
25+
assert.True(t, strutil.IsUint("456789"))
26+
27+
assert.False(t, strutil.IsUint("-123"))
28+
assert.False(t, strutil.IsUint("123.45"))
29+
assert.False(t, strutil.IsUint("ab3"))
30+
assert.False(t, strutil.IsUint(""))
1931
}
2032

2133
func TestIsFloat(t *testing.T) {
@@ -33,6 +45,26 @@ func TestIsFloat(t *testing.T) {
3345
assert.False(t, strutil.IsFloat(""))
3446
}
3547

48+
func TestIsNumeric(t *testing.T) {
49+
// IsNumeric
50+
assert.True(t, strutil.IsNumeric("0"))
51+
assert.True(t, strutil.IsNumeric("234"))
52+
assert.True(t, strutil.IsNumeric("-234"))
53+
assert.True(t, strutil.IsNumeric("23.4"))
54+
assert.True(t, strutil.IsNumeric("-23.4"))
55+
assert.False(t, strutil.IsNumeric(""))
56+
assert.False(t, strutil.IsNumeric("a34"))
57+
58+
// IsPositiveNum
59+
assert.True(t, strutil.IsPositiveNum("0.0"))
60+
assert.True(t, strutil.IsPositiveNum("234"))
61+
assert.True(t, strutil.IsPositiveNum("23.4"))
62+
assert.False(t, strutil.IsPositiveNum("-23.4"))
63+
assert.False(t, strutil.IsPositiveNum("-234"))
64+
assert.False(t, strutil.IsPositiveNum("a34"))
65+
assert.False(t, strutil.IsPositiveNum(""))
66+
}
67+
3668
func TestIsAlphabet(t *testing.T) {
3769
assert.True(t, strutil.IsNumChar('9'))
3870
assert.False(t, strutil.IsNumChar('A'))
@@ -44,9 +76,6 @@ func TestIsAlphabet(t *testing.T) {
4476
assert.True(t, strutil.IsAlphabet('a'))
4577
assert.True(t, strutil.IsAlphabet('Z'))
4678
assert.True(t, strutil.IsAlphabet('z'))
47-
48-
assert.True(t, strutil.IsNumeric("234"))
49-
assert.False(t, strutil.IsNumeric("a34"))
5079
}
5180

5281
func TestIsAlphaNum(t *testing.T) {

0 commit comments

Comments
 (0)