@@ -15,6 +15,7 @@ package wiremessage
15
15
16
16
import (
17
17
"bytes"
18
+ "encoding/binary"
18
19
"strings"
19
20
"sync/atomic"
20
21
@@ -238,10 +239,11 @@ func ReadHeader(src []byte) (length, requestID, responseTo int32, opcode OpCode,
238
239
if len (src ) < 16 {
239
240
return 0 , 0 , 0 , 0 , src , false
240
241
}
241
- length = (int32 (src [0 ]) | int32 (src [1 ])<< 8 | int32 (src [2 ])<< 16 | int32 (src [3 ])<< 24 )
242
- requestID = (int32 (src [4 ]) | int32 (src [5 ])<< 8 | int32 (src [6 ])<< 16 | int32 (src [7 ])<< 24 )
243
- responseTo = (int32 (src [8 ]) | int32 (src [9 ])<< 8 | int32 (src [10 ])<< 16 | int32 (src [11 ])<< 24 )
244
- opcode = OpCode (int32 (src [12 ]) | int32 (src [13 ])<< 8 | int32 (src [14 ])<< 16 | int32 (src [15 ])<< 24 )
242
+
243
+ length = readi32unsafe (src )
244
+ requestID = readi32unsafe (src [4 :])
245
+ responseTo = readi32unsafe (src [8 :])
246
+ opcode = OpCode (readi32unsafe (src [12 :]))
245
247
return length , requestID , responseTo , opcode , src [16 :], true
246
248
}
247
249
@@ -577,12 +579,16 @@ func ReadKillCursorsCursorIDs(src []byte, numIDs int32) (cursorIDs []int64, rem
577
579
return cursorIDs , src , true
578
580
}
579
581
580
- func appendi32 (dst []byte , i32 int32 ) []byte {
581
- return append (dst , byte (i32 ), byte (i32 >> 8 ), byte (i32 >> 16 ), byte (i32 >> 24 ))
582
+ func appendi32 (dst []byte , x int32 ) []byte {
583
+ b := []byte {0 , 0 , 0 , 0 }
584
+ binary .LittleEndian .PutUint32 (b , uint32 (x ))
585
+ return append (dst , b ... )
582
586
}
583
587
584
- func appendi64 (b []byte , i int64 ) []byte {
585
- return append (b , byte (i ), byte (i >> 8 ), byte (i >> 16 ), byte (i >> 24 ), byte (i >> 32 ), byte (i >> 40 ), byte (i >> 48 ), byte (i >> 56 ))
588
+ func appendi64 (dst []byte , x int64 ) []byte {
589
+ b := []byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
590
+ binary .LittleEndian .PutUint64 (b , uint64 (x ))
591
+ return append (dst , b ... )
586
592
}
587
593
588
594
func appendCString (b []byte , str string ) []byte {
@@ -594,21 +600,18 @@ func readi32(src []byte) (int32, []byte, bool) {
594
600
if len (src ) < 4 {
595
601
return 0 , src , false
596
602
}
597
-
598
- return (int32 (src [0 ]) | int32 (src [1 ])<< 8 | int32 (src [2 ])<< 16 | int32 (src [3 ])<< 24 ), src [4 :], true
603
+ return readi32unsafe (src ), src [4 :], true
599
604
}
600
605
601
606
func readi32unsafe (src []byte ) int32 {
602
- return ( int32 (src [ 0 ]) | int32 (src [ 1 ]) << 8 | int32 ( src [ 2 ]) << 16 | int32 ( src [ 3 ]) << 24 )
607
+ return int32 (binary . LittleEndian . Uint32 (src ) )
603
608
}
604
609
605
610
func readi64 (src []byte ) (int64 , []byte , bool ) {
606
611
if len (src ) < 8 {
607
612
return 0 , src , false
608
613
}
609
- i64 := (int64 (src [0 ]) | int64 (src [1 ])<< 8 | int64 (src [2 ])<< 16 | int64 (src [3 ])<< 24 |
610
- int64 (src [4 ])<< 32 | int64 (src [5 ])<< 40 | int64 (src [6 ])<< 48 | int64 (src [7 ])<< 56 )
611
- return i64 , src [8 :], true
614
+ return int64 (binary .LittleEndian .Uint64 (src )), src [8 :], true
612
615
}
613
616
614
617
func readcstring (src []byte ) (string , []byte , bool ) {
0 commit comments