Skip to content

Commit 2bd057e

Browse files
authored
Merge pull request #50 from kolyshkin/go120
modernize with go1.20 features
2 parents 2ad3b6d + 6a3e4f2 commit 2bd057e

4 files changed

Lines changed: 25 additions & 26 deletions

File tree

size.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,16 @@ func RAMInBytes(size string) (int64, error) {
8989

9090
// Parses the human-readable size string into the amount it represents.
9191
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
92-
// TODO: rewrite to use strings.Cut if there's a space
93-
// once Go < 1.18 is deprecated.
94-
sep := strings.LastIndexAny(sizeStr, "01234567890. ")
95-
if sep == -1 {
96-
// There should be at least a digit.
97-
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
98-
}
99-
var num, sfx string
100-
if sizeStr[sep] != ' ' {
92+
num, sfx, ok := strings.Cut(sizeStr, " ")
93+
if !ok {
94+
// No space between number and suffix. Find the suffix.
95+
sep := strings.LastIndexAny(sizeStr, "01234567890.")
96+
if sep == -1 {
97+
// There should be at least a digit.
98+
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
99+
}
101100
num = sizeStr[:sep+1]
102101
sfx = sizeStr[sep+1:]
103-
} else {
104-
// Omit the space separator.
105-
num = sizeStr[:sep]
106-
sfx = sizeStr[sep+1:]
107102
}
108103

109104
size, err := strconv.ParseFloat(num, 64)

size_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func BenchmarkParseSize(b *testing.B) {
256256
}
257257
}
258258

259-
func assertEquals(t *testing.T, expected, actual interface{}) {
259+
func assertEquals(t *testing.T, expected, actual any) {
260260
t.Helper()
261261
if expected != actual {
262262
t.Errorf("Expected '%v' but got '%v'", expected, actual)

ulimit.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ var ulimitNameMapping = map[string]int{
6464

6565
// ParseUlimit parses and returns a Ulimit from the specified string.
6666
func ParseUlimit(val string) (*Ulimit, error) {
67-
parts := strings.SplitN(val, "=", 2)
68-
if len(parts) != 2 {
67+
name, val, ok := strings.Cut(val, "=")
68+
if !ok {
6969
return nil, fmt.Errorf("invalid ulimit argument: %s", val)
7070
}
7171

72-
if _, exists := ulimitNameMapping[parts[0]]; !exists {
73-
return nil, fmt.Errorf("invalid ulimit type: %s", parts[0])
72+
if _, exists := ulimitNameMapping[name]; !exists {
73+
return nil, fmt.Errorf("invalid ulimit type: %s", name)
7474
}
7575

7676
var (
@@ -79,21 +79,19 @@ func ParseUlimit(val string) (*Ulimit, error) {
7979
temp int64
8080
err error
8181
)
82-
switch limitVals := strings.Split(parts[1], ":"); len(limitVals) {
83-
case 2:
84-
temp, err = strconv.ParseInt(limitVals[1], 10, 64)
82+
switch softStr, hardStr, ok := strings.Cut(val, ":"); ok {
83+
case true:
84+
temp, err = strconv.ParseInt(hardStr, 10, 64)
8585
if err != nil {
8686
return nil, err
8787
}
8888
hard = &temp
8989
fallthrough
90-
case 1:
91-
soft, err = strconv.ParseInt(limitVals[0], 10, 64)
90+
case false:
91+
soft, err = strconv.ParseInt(softStr, 10, 64)
9292
if err != nil {
9393
return nil, err
9494
}
95-
default:
96-
return nil, fmt.Errorf("too many limit value arguments - %s, can only have up to two, `soft[:hard]`", parts[1])
9795
}
9896

9997
if *hard != -1 {
@@ -105,7 +103,7 @@ func ParseUlimit(val string) (*Ulimit, error) {
105103
}
106104
}
107105

108-
return &Ulimit{Name: parts[0], Soft: soft, Hard: *hard}, nil
106+
return &Ulimit{Name: name, Soft: soft, Hard: *hard}, nil
109107
}
110108

111109
// GetRlimit returns the RLimit corresponding to Ulimit.

ulimit_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ func TestParseUlimitBadFormat(t *testing.T) {
5151
if _, err := ParseUlimit("nofile=:1024"); err == nil {
5252
t.Fatal("expected error on bad syntax")
5353
}
54+
if _, err := ParseUlimit("nofile=1024=512"); err == nil {
55+
t.Fatal("expected error on bad syntax")
56+
}
57+
if _, err := ParseUlimit("nofile=1024:512=extra"); err == nil {
58+
t.Fatal("expected error on bad syntax")
59+
}
5460
}
5561

5662
func TestParseUlimitHardLessThanSoft(t *testing.T) {

0 commit comments

Comments
 (0)