@@ -8,6 +8,7 @@ package bsoncore // import "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
8
8
9
9
import (
10
10
"bytes"
11
+ "encoding/binary"
11
12
"fmt"
12
13
"math"
13
14
"strconv"
@@ -706,17 +707,16 @@ func ReserveLength(dst []byte) (int32, []byte) {
706
707
707
708
// UpdateLength updates the length at index with length and returns the []byte.
708
709
func UpdateLength (dst []byte , index , length int32 ) []byte {
709
- dst [index ] = byte (length )
710
- dst [index + 1 ] = byte (length >> 8 )
711
- dst [index + 2 ] = byte (length >> 16 )
712
- dst [index + 3 ] = byte (length >> 24 )
710
+ binary .LittleEndian .PutUint32 (dst [index :], uint32 (length ))
713
711
return dst
714
712
}
715
713
716
714
func appendLength (dst []byte , l int32 ) []byte { return appendi32 (dst , l ) }
717
715
718
716
func appendi32 (dst []byte , i32 int32 ) []byte {
719
- return append (dst , byte (i32 ), byte (i32 >> 8 ), byte (i32 >> 16 ), byte (i32 >> 24 ))
717
+ b := []byte {0 , 0 , 0 , 0 }
718
+ binary .LittleEndian .PutUint32 (b , uint32 (i32 ))
719
+ return append (dst , b ... )
720
720
}
721
721
722
722
// ReadLength reads an int32 length from src and returns the length and the remaining bytes. If
@@ -734,51 +734,47 @@ func readi32(src []byte) (int32, []byte, bool) {
734
734
if len (src ) < 4 {
735
735
return 0 , src , false
736
736
}
737
- return ( int32 (src [ 0 ]) | int32 (src [ 1 ]) << 8 | int32 ( src [ 2 ]) << 16 | int32 ( src [ 3 ]) << 24 ), src [4 :], true
737
+ return int32 (binary . LittleEndian . Uint32 (src ) ), src [4 :], true
738
738
}
739
739
740
740
func appendi64 (dst []byte , i64 int64 ) []byte {
741
- return append (dst ,
742
- byte (i64 ), byte (i64 >> 8 ), byte (i64 >> 16 ), byte (i64 >> 24 ),
743
- byte (i64 >> 32 ), byte (i64 >> 40 ), byte (i64 >> 48 ), byte (i64 >> 56 ),
744
- )
741
+ b := []byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
742
+ binary .LittleEndian .PutUint64 (b , uint64 (i64 ))
743
+ return append (dst , b ... )
745
744
}
746
745
747
746
func readi64 (src []byte ) (int64 , []byte , bool ) {
748
747
if len (src ) < 8 {
749
748
return 0 , src , false
750
749
}
751
- i64 := (int64 (src [0 ]) | int64 (src [1 ])<< 8 | int64 (src [2 ])<< 16 | int64 (src [3 ])<< 24 |
752
- int64 (src [4 ])<< 32 | int64 (src [5 ])<< 40 | int64 (src [6 ])<< 48 | int64 (src [7 ])<< 56 )
753
- return i64 , src [8 :], true
750
+ return int64 (binary .LittleEndian .Uint64 (src )), src [8 :], true
754
751
}
755
752
756
753
func appendu32 (dst []byte , u32 uint32 ) []byte {
757
- return append (dst , byte (u32 ), byte (u32 >> 8 ), byte (u32 >> 16 ), byte (u32 >> 24 ))
754
+ b := []byte {0 , 0 , 0 , 0 }
755
+ binary .LittleEndian .PutUint32 (b , u32 )
756
+ return append (dst , b ... )
758
757
}
759
758
760
759
func readu32 (src []byte ) (uint32 , []byte , bool ) {
761
760
if len (src ) < 4 {
762
761
return 0 , src , false
763
762
}
764
763
765
- return ( uint32 ( src [ 0 ]) | uint32 ( src [ 1 ]) << 8 | uint32 ( src [ 2 ]) << 16 | uint32 ( src [ 3 ]) << 24 ), src [4 :], true
764
+ return binary . LittleEndian . Uint32 ( src ), src [4 :], true
766
765
}
767
766
768
767
func appendu64 (dst []byte , u64 uint64 ) []byte {
769
- return append (dst ,
770
- byte (u64 ), byte (u64 >> 8 ), byte (u64 >> 16 ), byte (u64 >> 24 ),
771
- byte (u64 >> 32 ), byte (u64 >> 40 ), byte (u64 >> 48 ), byte (u64 >> 56 ),
772
- )
768
+ b := []byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
769
+ binary .LittleEndian .PutUint64 (b , u64 )
770
+ return append (dst , b ... )
773
771
}
774
772
775
773
func readu64 (src []byte ) (uint64 , []byte , bool ) {
776
774
if len (src ) < 8 {
777
775
return 0 , src , false
778
776
}
779
- u64 := (uint64 (src [0 ]) | uint64 (src [1 ])<< 8 | uint64 (src [2 ])<< 16 | uint64 (src [3 ])<< 24 |
780
- uint64 (src [4 ])<< 32 | uint64 (src [5 ])<< 40 | uint64 (src [6 ])<< 48 | uint64 (src [7 ])<< 56 )
781
- return u64 , src [8 :], true
777
+ return binary .LittleEndian .Uint64 (src ), src [8 :], true
782
778
}
783
779
784
780
// keep in sync with readcstringbytes
0 commit comments