source: binary-improvements2/SpaceWizards.HttpListener/src/System/StringExtensions.cs@ 437

Last change on this file since 437 was 377, checked in by alloc, 2 years ago

Made SpaceWizards.HttpListener compilable against .NET 4.8 with preprocessor define UNITY_NETFRAMEWORK

File size: 1.5 KB
Line 
1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3
4#pragma warning disable CS8602
5
6using System.Diagnostics;
7
8namespace System
9{
10 internal static class StringExtensions
11 {
12 internal static string SubstringTrim(this string value, int startIndex, int length)
13 {
14 Debug.Assert(value != null, "string must be non-null");
15 Debug.Assert(startIndex >= 0, "startIndex must be non-negative");
16 Debug.Assert(length >= 0, "length must be non-negative");
17 Debug.Assert(startIndex <= value.Length - length, "startIndex + length must be <= value.Length");
18
19 if (length == 0)
20 {
21 return string.Empty;
22 }
23
24 int endIndex = startIndex + length - 1;
25
26 while (startIndex <= endIndex && char.IsWhiteSpace(value[startIndex]))
27 {
28 startIndex++;
29 }
30
31 while (endIndex >= startIndex && char.IsWhiteSpace(value[endIndex]))
32 {
33 endIndex--;
34 }
35
36 int newLength = endIndex - startIndex + 1;
37 Debug.Assert(newLength >= 0 && newLength <= value.Length, "Expected resulting length to be within value's length");
38
39 return
40 newLength == 0 ? string.Empty :
41 newLength == value.Length ? value :
42 value.Substring(startIndex, newLength);
43 }
44 }
45}
Note: See TracBrowser for help on using the repository browser.