File tree Expand file tree Collapse file tree 1 file changed +27
-17
lines changed Expand file tree Collapse file tree 1 file changed +27
-17
lines changed Original file line number Diff line number Diff line change 10
10
using System . IO ;
11
11
using System . Diagnostics ;
12
12
using System . Reflection ;
13
+ using System . Buffers ;
13
14
14
15
namespace Pchp . Library
15
16
{
@@ -814,27 +815,36 @@ PhpValue ReadString()
814
815
815
816
if ( length != 0 )
816
817
{
817
- var bytes = new byte [ length ] ;
818
-
819
- // :"<bytes>";
820
- Consume ( Tokens . Colon ) ;
821
- Consume ( Tokens . Quote ) ;
822
- if ( _stream . Read ( bytes , 0 , length ) != length )
823
- {
824
- ThrowEndOfStream ( ) ;
825
- }
826
- Consume ( Tokens . Quote ) ;
827
-
828
- //
818
+ var bytesBuffer = ArrayPool < byte > . Shared . Rent ( length ) ;
819
+
829
820
try
830
821
{
831
- // unicode string
832
- return PhpValue . Create ( _encoding . GetString ( bytes ) ) ;
822
+ var bytes = bytesBuffer . AsSpan ( 0 , length ) ;
823
+
824
+ // :"<bytes>";
825
+ Consume ( Tokens . Colon ) ;
826
+ Consume ( Tokens . Quote ) ;
827
+ if ( _stream . Read ( bytes ) != length )
828
+ {
829
+ ThrowEndOfStream ( ) ;
830
+ }
831
+ Consume ( Tokens . Quote ) ;
832
+
833
+ //
834
+ try
835
+ {
836
+ // unicode string
837
+ return PhpValue . Create ( _encoding . GetString ( bytes ) ) ;
838
+ }
839
+ catch ( DecoderFallbackException )
840
+ {
841
+ // binary string
842
+ return PhpValue . Create ( new PhpString ( bytes . ToArray ( ) ) ) ;
843
+ }
833
844
}
834
- catch ( DecoderFallbackException )
845
+ finally
835
846
{
836
- // binary string
837
- return PhpValue . Create ( new PhpString ( bytes ) ) ;
847
+ ArrayPool < byte > . Shared . Return ( bytesBuffer ) ;
838
848
}
839
849
}
840
850
else
You can’t perform that action at this time.
0 commit comments