1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Runtime . InteropServices ;
4
+ using System . Text ;
4
5
using System . Text . RegularExpressions ;
5
6
6
7
namespace OmniSharp . Extensions . LanguageServer . Protocol
@@ -98,11 +99,12 @@ private static string ReferenceResolution(string scheme, string path)
98
99
{
99
100
if ( string . IsNullOrWhiteSpace ( path ) )
100
101
{
101
- path = StrSlash ;
102
+ return StrSlash ;
102
103
}
103
- else if ( path [ 0 ] != Slash )
104
+
105
+ if ( path [ 0 ] != Slash )
104
106
{
105
- path = Slash + path ;
107
+ return Slash + path ;
106
108
}
107
109
}
108
110
@@ -135,7 +137,7 @@ private static string ReferenceResolution(string scheme, string path)
135
137
136
138
private static string EncodeUriComponentFast ( string uriComponent , bool allowSlash )
137
139
{
138
- string res = null ;
140
+ StringBuilder res = null ;
139
141
var nativeEncodePos = - 1 ;
140
142
141
143
for ( var pos = 0 ; pos < uriComponent . Length ; pos ++ )
@@ -162,23 +164,25 @@ private static string EncodeUriComponentFast(string uriComponent, bool allowSlas
162
164
// check if we are delaying native encode
163
165
if ( nativeEncodePos != - 1 )
164
166
{
165
- res += Uri . EscapeDataString ( uriComponent . Substring ( nativeEncodePos , pos - nativeEncodePos ) ) ;
167
+ res ??= new StringBuilder ( ) ;
168
+ res . Append ( Uri . EscapeDataString ( uriComponent . Substring ( nativeEncodePos , pos - nativeEncodePos ) ) ) ;
166
169
nativeEncodePos = - 1 ;
167
170
}
168
171
169
172
// check if we write into a new string (by default we try to return the param)
170
173
if ( res != null )
171
174
{
172
- res += uriComponent [ pos ] ;
175
+ res ??= new StringBuilder ( ) ;
176
+ res . Append ( uriComponent [ pos ] ) ;
173
177
}
174
178
}
175
179
else
176
180
{
177
181
// encoding needed, we need to allocate a new string
178
182
if ( res == null )
179
-
180
183
{
181
- res = uriComponent . Substring ( 0 , pos ) ;
184
+ res ??= new StringBuilder ( ) ;
185
+ res . Append ( uriComponent . Substring ( 0 , pos ) ) ;
182
186
}
183
187
184
188
// check with default table first
@@ -187,12 +191,12 @@ private static string EncodeUriComponentFast(string uriComponent, bool allowSlas
187
191
// check if we are delaying native encode
188
192
if ( nativeEncodePos != - 1 )
189
193
{
190
- res += Uri . EscapeDataString ( uriComponent . Substring ( nativeEncodePos , pos - nativeEncodePos ) ) ;
194
+ res . Append ( Uri . EscapeDataString ( uriComponent . Substring ( nativeEncodePos , pos - nativeEncodePos ) ) ) ;
191
195
nativeEncodePos = - 1 ;
192
196
}
193
197
194
198
// append escaped variant to result
195
- res += escaped ;
199
+ res . Append ( escaped ) ;
196
200
}
197
201
else if ( nativeEncodePos == - 1 )
198
202
{
@@ -204,37 +208,35 @@ private static string EncodeUriComponentFast(string uriComponent, bool allowSlas
204
208
205
209
if ( nativeEncodePos != - 1 )
206
210
{
207
- res += Uri . EscapeDataString ( uriComponent . Substring ( nativeEncodePos ) ) ;
211
+ res ??= new StringBuilder ( ) ;
212
+ res . Append ( Uri . EscapeDataString ( uriComponent . Substring ( nativeEncodePos ) ) ) ;
208
213
}
209
214
210
- return ! string . IsNullOrWhiteSpace ( res ) ? res : uriComponent ;
215
+ return res != null ? res . ToString ( ) : uriComponent ;
211
216
}
212
217
213
218
private static string EncodeUriComponentMinimal ( string path )
214
219
{
215
- string res = null ;
220
+ StringBuilder res = null ;
216
221
for ( var pos = 0 ; pos < path . Length ; pos ++ )
217
222
{
218
223
var code = path [ pos ] ;
219
224
if ( code == CharCode . Hash || code == CharCode . QuestionMark )
220
225
{
221
226
if ( res == null )
222
227
{
223
- res = path . Substring ( 0 , pos ) ;
228
+ res = new StringBuilder ( path . Substring ( 0 , pos ) ) ;
224
229
}
225
230
226
- res += EncodeTable [ code ] ;
231
+ res . Append ( EncodeTable [ code ] ) ;
227
232
}
228
233
else
229
234
{
230
- if ( res != null )
231
- {
232
- res += path [ pos ] ;
233
- }
235
+ res ? . Append ( path [ pos ] ) ;
234
236
}
235
237
}
236
238
237
- return res ?? path ;
239
+ return res != null ? res . ToString ( ) : path ;
238
240
}
239
241
240
242
/// <summary>
@@ -290,18 +292,18 @@ string Encoder(string p, bool allowSlash)
290
292
return ! skipEncoding ? EncodeUriComponentFast ( p , allowSlash ) : EncodeUriComponentMinimal ( p ) ;
291
293
}
292
294
293
- var res = "" ;
295
+ var res = new StringBuilder ( ) ;
294
296
var ( scheme , authority , path , query , fragment ) = uri ;
295
297
if ( ! string . IsNullOrWhiteSpace ( scheme ) )
296
298
{
297
- res += scheme ;
298
- res += ":" ;
299
+ res . Append ( scheme ) ;
300
+ res . Append ( ":" ) ;
299
301
}
300
302
301
303
if ( ! string . IsNullOrWhiteSpace ( authority ) || scheme == "file" )
302
304
{
303
- res += Slash ;
304
- res += Slash ;
305
+ res . Append ( Slash ) ;
306
+ res . Append ( Slash ) ;
305
307
}
306
308
307
309
if ( ! string . IsNullOrWhiteSpace ( authority ) )
@@ -315,70 +317,82 @@ string Encoder(string p, bool allowSlash)
315
317
idx = userinfo . IndexOf ( ":" ) ;
316
318
if ( idx == - 1 )
317
319
{
318
- res += Encoder ( userinfo , false ) ;
320
+ res . Append ( Encoder ( userinfo , false ) ) ;
319
321
}
320
322
else
321
323
{
322
324
// <user>:<pass>@<auth>
323
- res += Encoder ( userinfo . Substring ( 0 , idx ) , false ) ;
324
- res += ":" ;
325
- res += Encoder ( userinfo . Substring ( idx + 1 ) , false ) ;
325
+ res . Append ( Encoder ( userinfo . Substring ( 0 , idx ) , false ) ) ;
326
+ res . Append ( ":" ) ;
327
+ res . Append ( Encoder ( userinfo . Substring ( idx + 1 ) , false ) ) ;
326
328
}
327
329
328
- res += "@" ;
330
+ res . Append ( "@" ) ;
329
331
}
330
332
331
333
authority = authority . ToLowerInvariant ( ) ;
332
334
idx = authority . IndexOf ( ":" , StringComparison . Ordinal ) ;
333
335
if ( idx == - 1 )
334
336
{
335
- res += Encoder ( authority , false ) ;
337
+ res . Append ( Encoder ( authority , false ) ) ;
336
338
}
337
339
else
338
340
{
339
341
// <auth>:<port>
340
- res += Encoder ( authority . Substring ( 0 , idx ) , false ) ;
341
- res += authority . Substring ( idx ) ;
342
+ res . Append ( Encoder ( authority . Substring ( 0 , idx ) , false ) ) ;
343
+ res . Append ( authority . Substring ( idx ) ) ;
342
344
}
343
345
}
344
346
345
347
if ( ! string . IsNullOrWhiteSpace ( path ) )
346
348
{
349
+ var appended = false ;
347
350
// lower-case windows drive letters in /C:/fff or C:/fff
348
351
if ( path . Length >= 3 && path [ 0 ] == CharCode . Slash && path [ 2 ] == CharCode . Colon )
349
352
{
350
353
var code = path [ 1 ] ;
351
354
if ( code >= CharCode . A && code <= CharCode . Z )
352
355
{
353
- path = $ "/{ Convert . ToChar ( code + 32 ) } :{ path . Substring ( 3 ) } "; // "/c:".length == 3
356
+ appended = true ;
357
+ res . Append ( "/" ) ;
358
+ res . Append ( Convert . ToChar ( code + 32 ) ) ;
359
+ res . Append ( ":" ) ;
360
+ res . Append ( Encoder ( path . Substring ( 3 ) , true ) ) ;
361
+ // path = $"/{Convert.ToChar(code + 32)}:{path.Substring(3)}"; // "/c:".length == 3
354
362
}
355
363
}
356
364
else if ( path . Length >= 2 && path [ 1 ] == CharCode . Colon )
357
365
{
358
366
var code = path [ 0 ] ;
359
367
if ( code >= CharCode . A && code <= CharCode . Z )
360
368
{
361
- path = $ "{ Convert . ToChar ( code + 32 ) } :{ path . Substring ( 2 ) } "; // "/c:".length == 3
369
+ appended = true ;
370
+ res . Append ( Convert . ToChar ( code + 32 ) ) ;
371
+ res . Append ( ":" ) ;
372
+ res . Append ( Encoder ( path . Substring ( 2 ) , true ) ) ;
373
+ // path = $"{Convert.ToChar(code + 32)}:{path.Substring(2)}"; // "/c:".length == 3
362
374
}
363
375
}
364
376
365
- // encode the rest of the path
366
- res += Encoder ( path , true ) ;
377
+ if ( ! appended )
378
+ {
379
+ res . Append ( Encoder ( path , true ) ) ;
380
+ }
367
381
}
368
382
369
383
if ( ! string . IsNullOrWhiteSpace ( query ) )
370
384
{
371
- res += "?" ;
372
- res += Encoder ( query , false ) ;
385
+ res . Append ( "?" ) ;
386
+ res . Append ( Encoder ( query , false ) ) ;
373
387
}
374
388
375
389
if ( ! string . IsNullOrWhiteSpace ( fragment ) )
376
390
{
377
- res += "#" ;
378
- res += ! skipEncoding ? EncodeUriComponentFast ( fragment , false ) : fragment ;
391
+ res . Append ( "#" ) ;
392
+ res . Append ( ! skipEncoding ? EncodeUriComponentFast ( fragment , false ) : fragment ) ;
379
393
}
380
394
381
- return res ;
395
+ return res . ToString ( ) ;
382
396
}
383
397
384
398
private static string DecodeUriComponentGraceful ( string str )
0 commit comments