Skip to content

Commit 14f52bc

Browse files
committed
Add AppendInt function with positive/negative number convertion to string
1 parent ac59843 commit 14f52bc

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

convert.go

-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ func AppendInt(dst []byte, n int) []byte {
177177
// add '-' in front of the number
178178
dst = append(dst, '-')
179179
}
180-
181180
dst = append(dst, buf[i:]...)
182181

183182
return dst

convert_test.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package utils
66

77
import (
8+
"math"
89
"reflect"
910
"strconv"
1011
"testing"
@@ -244,6 +245,8 @@ func Test_AppendInt(t *testing.T) {
244245
require.Equal(t, []byte("-1"), AppendInt(dst, -1))
245246
require.Equal(t, []byte("-2"), AppendInt(dst, -2))
246247
require.Equal(t, []byte("-4500"), AppendInt(dst, -4500))
248+
require.Equal(t, []byte(strconv.Itoa(math.MaxInt)), AppendInt(dst, math.MaxInt))
249+
//require.Equal(t, []byte("-4500"), AppendInt(dst, math.MinInt+1))
247250
}
248251

249252
// go test -v -run=^$ -bench=ToString -benchmem -count=4
@@ -312,16 +315,16 @@ func Benchmark_UnsafeString(b *testing.B) {
312315

313316
// go test -v -run=^$ -bench=ItoA -benchmem -count=4
314317
func Benchmark_ItoA(b *testing.B) {
315-
number := 4242
318+
number := 2
316319
number64 := int64(number)
317-
numberString := "4242"
318-
numberN := -4242
320+
numberString := "2"
321+
numberN := -2
319322
number64N := int64(numberN)
320-
numberNString := "-4242"
323+
numberNString := "-2"
321324

322325
var resB []byte
323326
var resS string
324-
b.Run("fiber (positiv number)", func(b *testing.B) {
327+
b.Run("fiber (pos num)", func(b *testing.B) {
325328
b.ReportAllocs()
326329
b.ResetTimer()
327330

@@ -331,7 +334,17 @@ func Benchmark_ItoA(b *testing.B) {
331334
require.Equal(b, []byte(numberString), resB)
332335
})
333336

334-
b.Run("default - strconv.Itoa (positiv number)", func(b *testing.B) {
337+
b.Run("default - strconv.AppendInt (pos num)", func(b *testing.B) {
338+
b.ReportAllocs()
339+
b.ResetTimer()
340+
341+
for n := 0; n < b.N; n++ {
342+
resB = strconv.AppendInt(resB[:0], number64, 10)
343+
}
344+
require.Equal(b, []byte(numberString), resB)
345+
})
346+
347+
b.Run("default - strconv.Itoa (pos num)", func(b *testing.B) {
335348
b.ReportAllocs()
336349
b.ResetTimer()
337350
for n := 0; n < b.N; n++ {
@@ -340,7 +353,7 @@ func Benchmark_ItoA(b *testing.B) {
340353
require.Equal(b, numberString, resS)
341354
})
342355

343-
b.Run("default - strconv.FormatInt (positiv number)", func(b *testing.B) {
356+
b.Run("default - strconv.FormatInt (pos num)", func(b *testing.B) {
344357
b.ReportAllocs()
345358
b.ResetTimer()
346359
for n := 0; n < b.N; n++ {
@@ -349,7 +362,7 @@ func Benchmark_ItoA(b *testing.B) {
349362
require.Equal(b, numberString, resS)
350363
})
351364

352-
b.Run("fiber (negative number)", func(b *testing.B) {
365+
b.Run("fiber (neg num)", func(b *testing.B) {
353366
b.ReportAllocs()
354367
b.ResetTimer()
355368
for n := 0; n < b.N; n++ {
@@ -358,7 +371,17 @@ func Benchmark_ItoA(b *testing.B) {
358371
require.Equal(b, []byte(numberNString), resB)
359372
})
360373

361-
b.Run("default - strconv.Itoa (negative number)", func(b *testing.B) {
374+
b.Run("default - strconv.AppendInt (neg num)", func(b *testing.B) {
375+
b.ReportAllocs()
376+
b.ResetTimer()
377+
378+
for n := 0; n < b.N; n++ {
379+
resB = strconv.AppendInt(resB[:0], number64N, 10)
380+
}
381+
require.Equal(b, []byte(numberNString), resB)
382+
})
383+
384+
b.Run("default - strconv.Itoa (neg num)", func(b *testing.B) {
362385
b.ReportAllocs()
363386
b.ResetTimer()
364387
for n := 0; n < b.N; n++ {
@@ -367,7 +390,7 @@ func Benchmark_ItoA(b *testing.B) {
367390
require.Equal(b, numberNString, resS)
368391
})
369392

370-
b.Run("default - strconv.FormatInt (negative number)", func(b *testing.B) {
393+
b.Run("default - strconv.FormatInt (neg num)", func(b *testing.B) {
371394
b.ReportAllocs()
372395
b.ResetTimer()
373396
for n := 0; n < b.N; n++ {

0 commit comments

Comments
 (0)