-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AppendInt function with positive/negative number convertion to string #90
Changes from 4 commits
e60c5a7
87790b8
9c0bea3
ac59843
14f52bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,3 +151,34 @@ func ToString(arg any, timeFormat ...string) string { | |
return fmt.Sprint(arg) | ||
} | ||
} | ||
|
||
// AppendInt appends the string representation of the int n to dst and returns the extended buffer. | ||
func AppendInt(dst []byte, n int) []byte { | ||
isNegative := n < 0 | ||
if isNegative { | ||
// Convert the number to positive | ||
n = -n | ||
} | ||
Comment on lines
+156
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correctness: Handle integer overflow for negative values. When converting a negative integer to positive, be cautious of integer overflow. The edge case for if isNegative {
if n == math.MinInt {
// Handle the edge case for the smallest possible integer
dst = append(dst, "-9223372036854775808"...)
return dst
}
n = -n
} |
||
|
||
var b [20]byte | ||
buf := b[:] | ||
i := len(buf) | ||
var q int | ||
for n >= 10 { | ||
i-- | ||
q = n / 10 | ||
buf[i] = '0' + byte(n-q*10) | ||
n = q | ||
} | ||
i-- | ||
buf[i] = '0' + byte(n) | ||
|
||
if isNegative { | ||
// add '-' in front of the number | ||
dst = append(dst, '-') | ||
} | ||
|
||
dst = append(dst, buf[i:]...) | ||
|
||
return dst | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ package utils | |
|
||
import ( | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -232,6 +233,19 @@ func TestByteSize(t *testing.T) { | |
} | ||
} | ||
|
||
func Test_AppendInt(t *testing.T) { | ||
t.Parallel() | ||
|
||
dst := make([]byte, 0) | ||
|
||
require.Equal(t, []byte("42"), AppendInt(dst, 42)) | ||
require.Equal(t, []byte("1500"), AppendInt(dst, 1500)) | ||
require.Equal(t, []byte("0"), AppendInt(dst, 0)) | ||
require.Equal(t, []byte("-1"), AppendInt(dst, -1)) | ||
require.Equal(t, []byte("-2"), AppendInt(dst, -2)) | ||
require.Equal(t, []byte("-4500"), AppendInt(dst, -4500)) | ||
} | ||
|
||
// go test -v -run=^$ -bench=ToString -benchmem -count=4 | ||
func Benchmark_ToString(b *testing.B) { | ||
for _, value := range dataTypeExamples { | ||
|
@@ -295,3 +309,70 @@ func Benchmark_UnsafeString(b *testing.B) { | |
require.Equal(b, "Hello, World!", res) | ||
}) | ||
} | ||
|
||
// go test -v -run=^$ -bench=ItoA -benchmem -count=4 | ||
func Benchmark_ItoA(b *testing.B) { | ||
number := 4242 | ||
number64 := int64(number) | ||
numberString := "4242" | ||
numberN := -4242 | ||
number64N := int64(numberN) | ||
numberNString := "-4242" | ||
|
||
var resB []byte | ||
var resS string | ||
b.Run("fiber (positiv number)", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for n := 0; n < b.N; n++ { | ||
resB = AppendInt(resB[:0], number) | ||
} | ||
require.Equal(b, []byte(numberString), resB) | ||
}) | ||
|
||
b.Run("default - strconv.Itoa (positiv number)", func(b *testing.B) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. positive* |
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
resS = strconv.Itoa(number) | ||
} | ||
require.Equal(b, numberString, resS) | ||
}) | ||
|
||
b.Run("default - strconv.FormatInt (positiv number)", func(b *testing.B) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. positive* |
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
resS = strconv.FormatInt(number64, 10) | ||
} | ||
require.Equal(b, numberString, resS) | ||
}) | ||
|
||
b.Run("fiber (negative number)", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
resB = AppendInt(resB[:0], numberN) | ||
} | ||
require.Equal(b, []byte(numberNString), resB) | ||
}) | ||
|
||
b.Run("default - strconv.Itoa (negative number)", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
resS = strconv.Itoa(numberN) | ||
} | ||
require.Equal(b, numberNString, resS) | ||
}) | ||
|
||
b.Run("default - strconv.FormatInt (negative number)", func(b *testing.B) { | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
resS = strconv.FormatInt(number64N, 10) | ||
} | ||
require.Equal(b, numberNString, resS) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need a separate function for
AppendUint
that usesuint
as param, else we won't be able to handle values bigger than2,147,483,647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
math
module in Golang defines these: