@@ -32,11 +32,31 @@ namespace Renci.SshNet
32
32
public partial class ScpClient : BaseClient
33
33
{
34
34
private const string Message = "filename" ;
35
- private static readonly Regex FileInfoRe = new Regex ( @"C(?<mode>\d{4}) (?<length>\d+) (?<filename>.+)" , RegexOptions . Compiled ) ;
35
+ private const string FileInfoPattern = @"C(?<mode>\d{4}) (?<length>\d+) (?<filename>.+)" ;
36
+ private const string DirectoryInfoPattern = @"D(?<mode>\d{4}) (?<length>\d+) (?<filename>.+)" ;
37
+ private const string TimestampPattern = @"T(?<mtime>\d+) 0 (?<atime>\d+) 0" ;
38
+
39
+ #if NET7_0_OR_GREATER
40
+ private static readonly Regex FileInfoRegex = GetFileInfoRegex ( ) ;
41
+ private static readonly Regex DirectoryInfoRegex = GetDirectoryInfoRegex ( ) ;
42
+ private static readonly Regex TimestampRegex = GetTimestampRegex ( ) ;
43
+
44
+ [ GeneratedRegex ( FileInfoPattern ) ]
45
+ private static partial Regex GetFileInfoRegex ( ) ;
46
+
47
+ [ GeneratedRegex ( DirectoryInfoPattern ) ]
48
+ private static partial Regex GetDirectoryInfoRegex ( ) ;
49
+
50
+ [ GeneratedRegex ( TimestampPattern ) ]
51
+ private static partial Regex GetTimestampRegex ( ) ;
52
+ #else
53
+ private static readonly Regex FileInfoRegex = new Regex ( FileInfoPattern , RegexOptions . Compiled ) ;
54
+ private static readonly Regex DirectoryInfoRegex = new Regex ( DirectoryInfoPattern , RegexOptions . Compiled ) ;
55
+ private static readonly Regex TimestampRegex = new Regex ( TimestampPattern , RegexOptions . Compiled ) ;
56
+ #endif
57
+
36
58
private static readonly byte [ ] SuccessConfirmationCode = { 0 } ;
37
59
private static readonly byte [ ] ErrorConfirmationCode = { 1 } ;
38
- private static readonly Regex DirectoryInfoRe = new Regex ( @"D(?<mode>\d{4}) (?<length>\d+) (?<filename>.+)" , RegexOptions . Compiled ) ;
39
- private static readonly Regex TimestampRe = new Regex ( @"T(?<mtime>\d+) 0 (?<atime>\d+) 0" , RegexOptions . Compiled ) ;
40
60
41
61
private IRemotePathTransformation _remotePathTransformation ;
42
62
private TimeSpan _operationTimeout ;
@@ -458,7 +478,7 @@ public void Download(string filename, Stream destination)
458
478
SendSuccessConfirmation ( channel ) ; // Send reply
459
479
460
480
var message = ReadString ( input ) ;
461
- var match = FileInfoRe . Match ( message ) ;
481
+ var match = FileInfoRegex . Match ( message ) ;
462
482
463
483
if ( match . Success )
464
484
{
@@ -757,7 +777,7 @@ private void InternalDownload(IChannelSession channel, Stream input, FileSystemI
757
777
continue ;
758
778
}
759
779
760
- var match = DirectoryInfoRe . Match ( message ) ;
780
+ var match = DirectoryInfoRegex . Match ( message ) ;
761
781
if ( match . Success )
762
782
{
763
783
SendSuccessConfirmation ( channel ) ; // Send reply
@@ -784,7 +804,7 @@ private void InternalDownload(IChannelSession channel, Stream input, FileSystemI
784
804
continue ;
785
805
}
786
806
787
- match = FileInfoRe . Match ( message ) ;
807
+ match = FileInfoRegex . Match ( message ) ;
788
808
if ( match . Success )
789
809
{
790
810
// Read file
@@ -814,7 +834,7 @@ private void InternalDownload(IChannelSession channel, Stream input, FileSystemI
814
834
continue ;
815
835
}
816
836
817
- match = TimestampRe . Match ( message ) ;
837
+ match = TimestampRegex . Match ( message ) ;
818
838
if ( match . Success )
819
839
{
820
840
// Read timestamp
0 commit comments