Skip to content

Commit cda5b8f

Browse files
committed
use extension members for string.Join etc
1 parent f85fe5c commit cda5b8f

File tree

8 files changed

+60
-51
lines changed

8 files changed

+60
-51
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#nullable enable
2+
#if NETSTANDARD2_0 || NETFRAMEWORK
3+
using System.Collections.Generic;
4+
#endif
5+
6+
namespace System
7+
{
8+
internal static class StringExtensions
9+
{
10+
extension(string text)
11+
{
12+
#if NETSTANDARD2_0 || NETFRAMEWORK
13+
public static string Join(char separator, params string?[] value)
14+
{
15+
return string.Join(separator.ToString(), value);
16+
}
17+
18+
public static string Join(char separator, IEnumerable<string?> value)
19+
{
20+
return string.Join(separator.ToString(), value);
21+
}
22+
23+
public static string Join(char separator, string?[] value, int startIndex, int count)
24+
{
25+
return string.Join(separator.ToString(), value, startIndex, count);
26+
}
27+
28+
public int IndexOf(char value, StringComparison comparisonType)
29+
{
30+
return text.IndexOf(value.ToString(), comparisonType);
31+
}
32+
#endif
33+
}
34+
}
35+
}

src/Renci.SshNet/ClientAuthentication.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,7 @@ private bool TryAuthenticate(ISession session,
105105
{
106106
authenticationException = new SshAuthenticationException(string.Format(CultureInfo.InvariantCulture,
107107
"No suitable authentication method found to complete authentication ({0}).",
108-
#if NET
109-
string.Join(',', allowedAuthenticationMethods)))
110-
#else
111-
string.Join(",", allowedAuthenticationMethods)))
112-
#endif
113-
;
108+
string.Join(',', allowedAuthenticationMethods)));
114109
return false;
115110
}
116111

src/Renci.SshNet/Common/Extensions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
#if !NET
23
using System.Collections.Generic;
4+
#endif
35
using System.Globalization;
46
#if !NET
57
using System.IO;
@@ -339,13 +341,6 @@ internal static bool IsConnected(this Socket socket)
339341
return socket.Connected;
340342
}
341343

342-
internal static string Join(this IEnumerable<string> values, string separator)
343-
{
344-
// Used to avoid analyzers asking to "use an overload with a char parameter"
345-
// which is not available on all targets.
346-
return string.Join(separator, values);
347-
}
348-
349344
#if !NET
350345
internal static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
351346
{

src/Renci.SshNet/Common/SshData.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,7 @@ protected void Write(BigInteger data)
386386
/// <param name="data">name-list data to write.</param>
387387
protected void Write(string[] data)
388388
{
389-
#if NET
390389
Write(string.Join(',', data), Ascii);
391-
#else
392-
Write(string.Join(",", data), Ascii);
393-
#endif
394390
}
395391

396392
/// <summary>

src/Renci.SshNet/Messages/Authentication/FailureMessage.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ protected override void LoadData()
5555
PartialSuccess = ReadBoolean();
5656
if (PartialSuccess)
5757
{
58-
#if NET
5958
Message = string.Join(',', AllowedAuthentications);
60-
#else
61-
Message = string.Join(",", AllowedAuthentications);
62-
#endif
6359
}
6460
}
6561

src/Renci.SshNet/Security/KeyExchange.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ from a in message.ServerHostKeyAlgorithms
9191
{
9292
_logger.LogTrace("[{SessionId}] Host key algorithm: we offer {WeOffer}",
9393
Session.SessionIdHex,
94-
session.ConnectionInfo.HostKeyAlgorithms.Keys.Join(","));
94+
string.Join(',', session.ConnectionInfo.HostKeyAlgorithms.Keys));
9595

9696
_logger.LogTrace("[{SessionId}] Host key algorithm: they offer {TheyOffer}",
9797
Session.SessionIdHex,
98-
message.ServerHostKeyAlgorithms.Join(","));
98+
string.Join(',', message.ServerHostKeyAlgorithms));
9999
}
100100

101101
if (hostKeyAlgorithmName is null)
102102
{
103103
throw new SshConnectionException(
104-
$"No matching host key algorithm (server offers {message.ServerHostKeyAlgorithms.Join(",")})",
104+
$"No matching host key algorithm (server offers {string.Join(',', message.ServerHostKeyAlgorithms)})",
105105
DisconnectReason.KeyExchangeFailed);
106106
}
107107

@@ -118,17 +118,17 @@ from a in message.EncryptionAlgorithmsClientToServer
118118
{
119119
_logger.LogTrace("[{SessionId}] Encryption client to server: we offer {WeOffer}",
120120
Session.SessionIdHex,
121-
session.ConnectionInfo.Encryptions.Keys.Join(","));
121+
string.Join(',', session.ConnectionInfo.Encryptions.Keys));
122122

123123
_logger.LogTrace("[{SessionId}] Encryption client to server: they offer {TheyOffer}",
124124
Session.SessionIdHex,
125-
message.EncryptionAlgorithmsClientToServer.Join(","));
125+
string.Join(',', message.EncryptionAlgorithmsClientToServer));
126126
}
127127

128128
if (clientEncryptionAlgorithmName is null)
129129
{
130130
throw new SshConnectionException(
131-
$"No matching client encryption algorithm (server offers {message.EncryptionAlgorithmsClientToServer.Join(",")})",
131+
$"No matching client encryption algorithm (server offers {string.Join(',', message.EncryptionAlgorithmsClientToServer)})",
132132
DisconnectReason.KeyExchangeFailed);
133133
}
134134

@@ -145,17 +145,17 @@ from a in message.EncryptionAlgorithmsServerToClient
145145
{
146146
_logger.LogTrace("[{SessionId}] Encryption server to client: we offer {WeOffer}",
147147
Session.SessionIdHex,
148-
session.ConnectionInfo.Encryptions.Keys.Join(","));
148+
string.Join(',', session.ConnectionInfo.Encryptions.Keys));
149149

150150
_logger.LogTrace("[{SessionId}] Encryption server to client: they offer {TheyOffer}",
151151
Session.SessionIdHex,
152-
message.EncryptionAlgorithmsServerToClient.Join(","));
152+
string.Join(',', message.EncryptionAlgorithmsServerToClient));
153153
}
154154

155155
if (serverDecryptionAlgorithmName is null)
156156
{
157157
throw new SshConnectionException(
158-
$"No matching server encryption algorithm (server offers {message.EncryptionAlgorithmsServerToClient.Join(",")})",
158+
$"No matching server encryption algorithm (server offers {string.Join(',', message.EncryptionAlgorithmsServerToClient)})",
159159
DisconnectReason.KeyExchangeFailed);
160160
}
161161

@@ -174,17 +174,17 @@ from a in message.MacAlgorithmsClientToServer
174174
{
175175
_logger.LogTrace("[{SessionId}] MAC client to server: we offer {WeOffer}",
176176
Session.SessionIdHex,
177-
session.ConnectionInfo.HmacAlgorithms.Keys.Join(","));
177+
string.Join(',', session.ConnectionInfo.HmacAlgorithms.Keys));
178178

179179
_logger.LogTrace("[{SessionId}] MAC client to server: they offer {TheyOffer}",
180180
Session.SessionIdHex,
181-
message.MacAlgorithmsClientToServer.Join(","));
181+
string.Join(',', message.MacAlgorithmsClientToServer));
182182
}
183183

184184
if (clientHmacAlgorithmName is null)
185185
{
186186
throw new SshConnectionException(
187-
$"No matching client MAC algorithm (server offers {message.MacAlgorithmsClientToServer.Join(",")})",
187+
$"No matching client MAC algorithm (server offers {string.Join(',', message.MacAlgorithmsClientToServer)})",
188188
DisconnectReason.KeyExchangeFailed);
189189
}
190190

@@ -204,17 +204,17 @@ from a in message.MacAlgorithmsServerToClient
204204
{
205205
_logger.LogTrace("[{SessionId}] MAC server to client: we offer {WeOffer}",
206206
Session.SessionIdHex,
207-
session.ConnectionInfo.HmacAlgorithms.Keys.Join(","));
207+
string.Join(',', session.ConnectionInfo.HmacAlgorithms.Keys));
208208

209209
_logger.LogTrace("[{SessionId}] MAC server to client: they offer {TheyOffer}",
210210
Session.SessionIdHex,
211-
message.MacAlgorithmsServerToClient.Join(","));
211+
string.Join(',', message.MacAlgorithmsServerToClient));
212212
}
213213

214214
if (serverHmacAlgorithmName is null)
215215
{
216216
throw new SshConnectionException(
217-
$"No matching server MAC algorithm (server offers {message.MacAlgorithmsServerToClient.Join(",")})",
217+
$"No matching server MAC algorithm (server offers {string.Join(',', message.MacAlgorithmsServerToClient)})",
218218
DisconnectReason.KeyExchangeFailed);
219219
}
220220

@@ -232,17 +232,17 @@ from a in message.CompressionAlgorithmsClientToServer
232232
{
233233
_logger.LogTrace("[{SessionId}] Compression client to server: we offer {WeOffer}",
234234
Session.SessionIdHex,
235-
session.ConnectionInfo.CompressionAlgorithms.Keys.Join(","));
235+
string.Join(',', session.ConnectionInfo.CompressionAlgorithms.Keys));
236236

237237
_logger.LogTrace("[{SessionId}] Compression client to server: they offer {TheyOffer}",
238238
Session.SessionIdHex,
239-
message.CompressionAlgorithmsClientToServer.Join(","));
239+
string.Join(',', message.CompressionAlgorithmsClientToServer));
240240
}
241241

242242
if (compressionAlgorithmName is null)
243243
{
244244
throw new SshConnectionException(
245-
$"No matching client compression algorithm (server offers {message.CompressionAlgorithmsClientToServer.Join(",")})",
245+
$"No matching client compression algorithm (server offers {string.Join(',', message.CompressionAlgorithmsClientToServer)})",
246246
DisconnectReason.KeyExchangeFailed);
247247
}
248248

@@ -259,17 +259,17 @@ from a in message.CompressionAlgorithmsServerToClient
259259
{
260260
_logger.LogTrace("[{SessionId}] Compression server to client: we offer {WeOffer}",
261261
Session.SessionIdHex,
262-
session.ConnectionInfo.CompressionAlgorithms.Keys.Join(","));
262+
string.Join(',', session.ConnectionInfo.CompressionAlgorithms.Keys));
263263

264264
_logger.LogTrace("[{SessionId}] Compression server to client: they offer {TheyOffer}",
265265
Session.SessionIdHex,
266-
message.CompressionAlgorithmsServerToClient.Join(","));
266+
string.Join(',', message.CompressionAlgorithmsServerToClient));
267267
}
268268

269269
if (decompressionAlgorithmName is null)
270270
{
271271
throw new SshConnectionException(
272-
$"No matching server compression algorithm (server offers {message.CompressionAlgorithmsServerToClient.Join(",")})",
272+
$"No matching server compression algorithm (server offers {string.Join(',', message.CompressionAlgorithmsServerToClient)})",
273273
DisconnectReason.KeyExchangeFailed);
274274
}
275275

src/Renci.SshNet/ServiceFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ from s in serverAlgorithms
9898

9999
if (keyExchangeAlgorithmFactory is null)
100100
{
101-
throw new SshConnectionException($"No matching key exchange algorithm (server offers {serverAlgorithms.Join(",")})", DisconnectReason.KeyExchangeFailed);
101+
throw new SshConnectionException($"No matching key exchange algorithm (server offers {string.Join(',', serverAlgorithms)})", DisconnectReason.KeyExchangeFailed);
102102
}
103103

104104
return keyExchangeAlgorithmFactory();

src/Renci.SshNet/Sftp/SftpSession.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,14 @@ public string GetCanonicalPath(string path)
137137
if (fullPath.EndsWith("/.", StringComparison.OrdinalIgnoreCase) ||
138138
fullPath.EndsWith("/..", StringComparison.OrdinalIgnoreCase) ||
139139
fullPath.Equals("/", StringComparison.OrdinalIgnoreCase) ||
140-
#if NET
141140
fullPath.IndexOf('/', StringComparison.OrdinalIgnoreCase) < 0)
142-
#else
143-
fullPath.IndexOf('/') < 0)
144-
#endif
145141
{
146142
return fullPath;
147143
}
148144

149145
var pathParts = fullPath.Split('/');
150146

151-
#if NET
152147
var partialFullPath = string.Join('/', pathParts, 0, pathParts.Length - 1);
153-
#else
154-
var partialFullPath = string.Join("/", pathParts, 0, pathParts.Length - 1);
155-
#endif
156148

157149
if (string.IsNullOrEmpty(partialFullPath))
158150
{

0 commit comments

Comments
 (0)