-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
53 lines (39 loc) · 1.65 KB
/
StringExtensions.cs
File metadata and controls
53 lines (39 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
// Polyfills for string APIs not available on .NET Framework.
// These are test-only and not optimized for production use.
#if !NET8_0_OR_GREATER
using System.Text.RegularExpressions;
namespace System;
internal static class TestDownlevelStringExtensions
{
extension(string s)
{
public bool Contains(string value, StringComparison comparisonType)
=> s.IndexOf(value, comparisonType) >= 0;
public bool Contains(char value)
=> s.IndexOf(value) >= 0;
public bool StartsWith(char value)
=> s.Length > 0 && s[0] == value;
public bool EndsWith(char value)
=> s.Length > 0 && s[s.Length - 1] == value;
public string[] Split(char separator, StringSplitOptions options)
=> s.Split([separator], options);
public string ReplaceLineEndings()
=> Regex.Replace(s, @"\r\n|\r|\n", "\n");
public string ReplaceLineEndings(string replacementText)
=> Regex.Replace(s, @"\r\n|\r|\n", replacementText);
}
extension(string)
{
public static string Create<TState>(int length, TState state, TestStringCreateSpanAction<TState> action)
{
var array = new char[length];
action(array, state);
return new string(array);
}
}
internal delegate void TestStringCreateSpanAction<in TArg>(Span<char> span, TArg arg);
}
#endif