Skip to content

Commit

Permalink
refactor code using built-in min and max functions
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 14, 2024
1 parent 470db58 commit 083fb39
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 47 deletions.
6 changes: 1 addition & 5 deletions cli/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 5 additions & 14 deletions cli/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 1 addition & 4 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
}
Expand Down
10 changes: 3 additions & 7 deletions scope_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 3 additions & 7 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 083fb39

Please sign in to comment.