From 083fb396bf11de27af5a9560718191ff4db9f460 Mon Sep 17 00:00:00 2001 From: itchyny Date: Sat, 20 Apr 2024 01:06:55 +0900 Subject: [PATCH] refactor code using built-in min and max functions --- cli/encoder.go | 6 +----- cli/error.go | 19 +++++-------------- compare.go | 6 +----- encoder.go | 6 +----- func.go | 5 +---- scope_stack.go | 10 +++------- stack.go | 10 +++------- 7 files changed, 15 insertions(+), 47 deletions(-) diff --git a/cli/encoder.go b/cli/encoder.go index 8841baac..312c8400 100644 --- a/cli/encoder.go +++ b/cli/encoder.go @@ -81,11 +81,7 @@ func (e *encoder) encodeFloat64(f float64) { e.write([]byte("null"), nullColor) return } - if f >= math.MaxFloat64 { - f = math.MaxFloat64 - } else if f <= -math.MaxFloat64 { - f = -math.MaxFloat64 - } + f = min(max(f, -math.MaxFloat64), math.MaxFloat64) format := byte('f') if x := math.Abs(f); x != 0 && x < 1e-6 || x >= 1e21 { format = 'e' diff --git a/cli/error.go b/cli/error.go index 3d7ced5b..17c26c4a 100644 --- a/cli/error.go +++ b/cli/error.go @@ -152,26 +152,17 @@ func getLineByOffset(str string, offset int) (linestr string, line, column int) break } } - if offset > len(linestr) { - offset = len(linestr) - } else if offset > 0 { - offset-- - } else { - offset = 0 - } + offset = min(max(offset-1, 0), len(linestr)) if offset > 48 { skip := len(trimLastInvalidRune(linestr[:offset-48])) linestr = linestr[skip:] offset -= skip } - if len(linestr) > 64 { - linestr = linestr[:64] - } - linestr = trimLastInvalidRune(linestr) - if offset >= len(linestr) { - offset = len(linestr) - } else { + linestr = trimLastInvalidRune(linestr[:min(64, len(linestr))]) + if offset < len(linestr) { offset = len(trimLastInvalidRune(linestr[:offset])) + } else { + offset = len(linestr) } column = runewidth.StringWidth(linestr[:offset]) return diff --git a/compare.go b/compare.go index 6ab22754..9791e9dc 100644 --- a/compare.go +++ b/compare.go @@ -35,11 +35,7 @@ func Compare(l, r any) int { } }, func(l, r []any) any { - n := len(l) - if len(r) < n { - n = len(r) - } - for i := 0; i < n; i++ { + for i, n := 0, min(len(l), len(r)); i < n; i++ { if cmp := Compare(l[i], r[i]); cmp != 0 { return cmp } diff --git a/encoder.go b/encoder.go index 3233e8a9..518904d7 100644 --- a/encoder.go +++ b/encoder.go @@ -79,11 +79,7 @@ func (e *encoder) encodeFloat64(f float64) { e.w.WriteString("null") return } - if f >= math.MaxFloat64 { - f = math.MaxFloat64 - } else if f <= -math.MaxFloat64 { - f = -math.MaxFloat64 - } + f = min(max(f, -math.MaxFloat64), math.MaxFloat64) format := byte('f') if x := math.Abs(f); x != 0 && x < 1e-6 || x >= 1e21 { format = 'e' diff --git a/func.go b/func.go index e06b4ff7..d04275c4 100644 --- a/func.go +++ b/func.go @@ -1504,10 +1504,7 @@ func (a allocator) makeObject(l int) map[string]any { } func (a allocator) makeArray(l, c int) []any { - if c < l { - c = l - } - v := make([]any, l, c) + v := make([]any, l, max(l, c)) if a != nil { a[reflect.ValueOf(v).Pointer()] = struct{}{} } diff --git a/scope_stack.go b/scope_stack.go index e140ca15..f652e299 100644 --- a/scope_stack.go +++ b/scope_stack.go @@ -17,13 +17,9 @@ func newScopeStack() *scopeStack { func (s *scopeStack) push(v scope) { b := scopeBlock{v, s.index} - i := s.index + 1 - if i <= s.limit { - i = s.limit + 1 - } - s.index = i - if i < len(s.data) { - s.data[i] = b + s.index = max(s.index, s.limit) + 1 + if s.index < len(s.data) { + s.data[s.index] = b } else { s.data = append(s.data, b) } diff --git a/stack.go b/stack.go index a0e265c8..0983ed26 100644 --- a/stack.go +++ b/stack.go @@ -17,13 +17,9 @@ func newStack() *stack { func (s *stack) push(v any) { b := block{v, s.index} - i := s.index + 1 - if i <= s.limit { - i = s.limit + 1 - } - s.index = i - if i < len(s.data) { - s.data[i] = b + s.index = max(s.index, s.limit) + 1 + if s.index < len(s.data) { + s.data[s.index] = b } else { s.data = append(s.data, b) }