Skip to content

Commit 083fb39

Browse files
committed
refactor code using built-in min and max functions
1 parent 470db58 commit 083fb39

File tree

7 files changed

+15
-47
lines changed

7 files changed

+15
-47
lines changed

cli/encoder.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ func (e *encoder) encodeFloat64(f float64) {
8181
e.write([]byte("null"), nullColor)
8282
return
8383
}
84-
if f >= math.MaxFloat64 {
85-
f = math.MaxFloat64
86-
} else if f <= -math.MaxFloat64 {
87-
f = -math.MaxFloat64
88-
}
84+
f = min(max(f, -math.MaxFloat64), math.MaxFloat64)
8985
format := byte('f')
9086
if x := math.Abs(f); x != 0 && x < 1e-6 || x >= 1e21 {
9187
format = 'e'

cli/error.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,17 @@ func getLineByOffset(str string, offset int) (linestr string, line, column int)
152152
break
153153
}
154154
}
155-
if offset > len(linestr) {
156-
offset = len(linestr)
157-
} else if offset > 0 {
158-
offset--
159-
} else {
160-
offset = 0
161-
}
155+
offset = min(max(offset-1, 0), len(linestr))
162156
if offset > 48 {
163157
skip := len(trimLastInvalidRune(linestr[:offset-48]))
164158
linestr = linestr[skip:]
165159
offset -= skip
166160
}
167-
if len(linestr) > 64 {
168-
linestr = linestr[:64]
169-
}
170-
linestr = trimLastInvalidRune(linestr)
171-
if offset >= len(linestr) {
172-
offset = len(linestr)
173-
} else {
161+
linestr = trimLastInvalidRune(linestr[:min(64, len(linestr))])
162+
if offset < len(linestr) {
174163
offset = len(trimLastInvalidRune(linestr[:offset]))
164+
} else {
165+
offset = len(linestr)
175166
}
176167
column = runewidth.StringWidth(linestr[:offset])
177168
return

compare.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ func Compare(l, r any) int {
3535
}
3636
},
3737
func(l, r []any) any {
38-
n := len(l)
39-
if len(r) < n {
40-
n = len(r)
41-
}
42-
for i := 0; i < n; i++ {
38+
for i, n := 0, min(len(l), len(r)); i < n; i++ {
4339
if cmp := Compare(l[i], r[i]); cmp != 0 {
4440
return cmp
4541
}

encoder.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ func (e *encoder) encodeFloat64(f float64) {
7979
e.w.WriteString("null")
8080
return
8181
}
82-
if f >= math.MaxFloat64 {
83-
f = math.MaxFloat64
84-
} else if f <= -math.MaxFloat64 {
85-
f = -math.MaxFloat64
86-
}
82+
f = min(max(f, -math.MaxFloat64), math.MaxFloat64)
8783
format := byte('f')
8884
if x := math.Abs(f); x != 0 && x < 1e-6 || x >= 1e21 {
8985
format = 'e'

func.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,10 +1504,7 @@ func (a allocator) makeObject(l int) map[string]any {
15041504
}
15051505

15061506
func (a allocator) makeArray(l, c int) []any {
1507-
if c < l {
1508-
c = l
1509-
}
1510-
v := make([]any, l, c)
1507+
v := make([]any, l, max(l, c))
15111508
if a != nil {
15121509
a[reflect.ValueOf(v).Pointer()] = struct{}{}
15131510
}

scope_stack.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ func newScopeStack() *scopeStack {
1717

1818
func (s *scopeStack) push(v scope) {
1919
b := scopeBlock{v, s.index}
20-
i := s.index + 1
21-
if i <= s.limit {
22-
i = s.limit + 1
23-
}
24-
s.index = i
25-
if i < len(s.data) {
26-
s.data[i] = b
20+
s.index = max(s.index, s.limit) + 1
21+
if s.index < len(s.data) {
22+
s.data[s.index] = b
2723
} else {
2824
s.data = append(s.data, b)
2925
}

stack.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ func newStack() *stack {
1717

1818
func (s *stack) push(v any) {
1919
b := block{v, s.index}
20-
i := s.index + 1
21-
if i <= s.limit {
22-
i = s.limit + 1
23-
}
24-
s.index = i
25-
if i < len(s.data) {
26-
s.data[i] = b
20+
s.index = max(s.index, s.limit) + 1
21+
if s.index < len(s.data) {
22+
s.data[s.index] = b
2723
} else {
2824
s.data = append(s.data, b)
2925
}

0 commit comments

Comments
 (0)