@@ -8,6 +8,7 @@ package bsoncore // import "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
88
99import (
1010 "bytes"
11+ "encoding/binary"
1112 "fmt"
1213 "math"
1314 "strconv"
@@ -706,17 +707,16 @@ func ReserveLength(dst []byte) (int32, []byte) {
706707
707708// UpdateLength updates the length at index with length and returns the []byte.
708709func 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 ))
713711 return dst
714712}
715713
716714func appendLength (dst []byte , l int32 ) []byte { return appendi32 (dst , l ) }
717715
718716func 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 ... )
720720}
721721
722722// 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) {
734734 if len (src ) < 4 {
735735 return 0 , src , false
736736 }
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
738738}
739739
740740func 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 ... )
745744}
746745
747746func readi64 (src []byte ) (int64 , []byte , bool ) {
748747 if len (src ) < 8 {
749748 return 0 , src , false
750749 }
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
754751}
755752
756753func 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 ... )
758757}
759758
760759func readu32 (src []byte ) (uint32 , []byte , bool ) {
761760 if len (src ) < 4 {
762761 return 0 , src , false
763762 }
764763
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
766765}
767766
768767func 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 ... )
773771}
774772
775773func readu64 (src []byte ) (uint64 , []byte , bool ) {
776774 if len (src ) < 8 {
777775 return 0 , src , false
778776 }
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
782778}
783779
784780// keep in sync with readcstringbytes
0 commit comments