@@ -282,19 +282,14 @@ public void Expect(TimeSpan timeout, params ExpectAction[] expectActions)
282
282
283
283
if ( match . Success )
284
284
{
285
- var returnText = matchText . Substring ( 0 , match . Index + match . Length ) ;
286
- var returnLength = _encoding . GetByteCount ( returnText ) ;
285
+ #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
286
+ var returnLength = _encoding . GetByteCount ( matchText . AsSpan ( 0 , match . Index + match . Length ) ) ;
287
+ #else
288
+ var returnLength = _encoding . GetByteCount ( matchText . Substring ( 0 , match . Index + match . Length ) ) ;
289
+ #endif
287
290
288
291
// Remove processed items from the queue
289
- for ( var i = 0 ; i < returnLength && _incoming . Count > 0 ; i ++ )
290
- {
291
- if ( _expect . Count == _incoming . Count )
292
- {
293
- _ = _expect . Dequeue ( ) ;
294
- }
295
-
296
- _ = _incoming . Dequeue ( ) ;
297
- }
292
+ var returnText = SyncQueuesAndReturn ( returnLength ) ;
298
293
299
294
expectAction . Action ( returnText ) ;
300
295
expectedFound = true ;
@@ -385,19 +380,14 @@ public string Expect(Regex regex, TimeSpan timeout)
385
380
386
381
if ( match . Success )
387
382
{
388
- returnText = matchText . Substring ( 0 , match . Index + match . Length ) ;
389
- var returnLength = _encoding . GetByteCount ( returnText ) ;
383
+ #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
384
+ var returnLength = _encoding . GetByteCount ( matchText . AsSpan ( 0 , match . Index + match . Length ) ) ;
385
+ #else
386
+ var returnLength = _encoding . GetByteCount ( matchText . Substring ( 0 , match . Index + match . Length ) ) ;
387
+ #endif
390
388
391
389
// Remove processed items from the queue
392
- for ( var i = 0 ; i < returnLength && _incoming . Count > 0 ; i ++ )
393
- {
394
- if ( _expect . Count == _incoming . Count )
395
- {
396
- _ = _expect . Dequeue ( ) ;
397
- }
398
-
399
- _ = _incoming . Dequeue ( ) ;
400
- }
390
+ returnText = SyncQueuesAndReturn ( returnLength ) ;
401
391
402
392
break ;
403
393
}
@@ -501,19 +491,14 @@ public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object
501
491
502
492
if ( match . Success )
503
493
{
504
- returnText = matchText . Substring ( 0 , match . Index + match . Length ) ;
505
- var returnLength = _encoding . GetByteCount ( returnText ) ;
494
+ #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
495
+ var returnLength = _encoding . GetByteCount ( matchText . AsSpan ( 0 , match . Index + match . Length ) ) ;
496
+ #else
497
+ var returnLength = _encoding . GetByteCount ( matchText . Substring ( 0 , match . Index + match . Length ) ) ;
498
+ #endif
506
499
507
500
// Remove processed items from the queue
508
- for ( var i = 0 ; i < returnLength && _incoming . Count > 0 ; i ++ )
509
- {
510
- if ( _expect . Count == _incoming . Count )
511
- {
512
- _ = _expect . Dequeue ( ) ;
513
- }
514
-
515
- _ = _incoming . Dequeue ( ) ;
516
- }
501
+ returnText = SyncQueuesAndReturn ( returnLength ) ;
517
502
518
503
expectAction . Action ( returnText ) ;
519
504
callback ? . Invoke ( asyncResult ) ;
@@ -614,15 +599,7 @@ public string ReadLine(TimeSpan timeout)
614
599
var bytesProcessed = _encoding . GetByteCount ( text + CrLf ) ;
615
600
616
601
// remove processed bytes from the queue
617
- for ( var i = 0 ; i < bytesProcessed ; i ++ )
618
- {
619
- if ( _expect . Count == _incoming . Count )
620
- {
621
- _ = _expect . Dequeue ( ) ;
622
- }
623
-
624
- _ = _incoming . Dequeue ( ) ;
625
- }
602
+ SyncQueuesAndDequeue ( bytesProcessed ) ;
626
603
627
604
break ;
628
605
}
@@ -687,7 +664,7 @@ public override int Read(byte[] buffer, int offset, int count)
687
664
{
688
665
for ( ; i < count && _incoming . Count > 0 ; i ++ )
689
666
{
690
- if ( _expect . Count == _incoming . Count )
667
+ if ( _incoming . Count == _expect . Count )
691
668
{
692
669
_ = _expect . Dequeue ( ) ;
693
670
}
@@ -869,5 +846,37 @@ private void OnDataReceived(byte[] data)
869
846
{
870
847
DataReceived ? . Invoke ( this , new ShellDataEventArgs ( data ) ) ;
871
848
}
849
+
850
+ private string SyncQueuesAndReturn ( int bytesToDequeue )
851
+ {
852
+ string incomingText ;
853
+
854
+ lock ( _incoming )
855
+ {
856
+ var incomingLength = _incoming . Count - _expect . Count + bytesToDequeue ;
857
+ incomingText = _encoding . GetString ( _incoming . ToArray ( ) , 0 , incomingLength ) ;
858
+
859
+ SyncQueuesAndDequeue ( bytesToDequeue ) ;
860
+ }
861
+
862
+ return incomingText ;
863
+ }
864
+
865
+ private void SyncQueuesAndDequeue ( int bytesToDequeue )
866
+ {
867
+ lock ( _incoming )
868
+ {
869
+ while ( _incoming . Count > _expect . Count )
870
+ {
871
+ _ = _incoming . Dequeue ( ) ;
872
+ }
873
+
874
+ for ( var count = 0 ; count < bytesToDequeue && _incoming . Count > 0 ; count ++ )
875
+ {
876
+ _ = _incoming . Dequeue ( ) ;
877
+ _ = _expect . Dequeue ( ) ;
878
+ }
879
+ }
880
+ }
872
881
}
873
882
}
0 commit comments