-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
StringExtensions.cs
80 lines (67 loc) · 2.65 KB
/
StringExtensions.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
namespace Microsoft.IO
{
public static class StringExtensions
{
public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg);
public static bool Contains(this string s, char value)
{
return s.IndexOf(value) != -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool EqualsOrdinal(this ReadOnlySpan<char> span, ReadOnlySpan<char> value)
{
if (span.Length != value.Length)
return false;
if (value.Length == 0) // span.Length == value.Length == 0
return true;
return span.SequenceEqual(value);
}
public static unsafe string Create<TState>(int length, TState state, SpanAction<char, TState> action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
if (length <= 0)
{
if (length == 0)
return string.Empty;
throw new ArgumentOutOfRangeException(nameof(length));
}
string result = new string('\0', length);
fixed (char* r = result)
{
action(new Span<char>(r, length), state);
}
return result;
}
internal static unsafe string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1)
{
var result = new string('\0', checked(str0.Length + str1.Length));
fixed (char* resultPtr = result)
{
var resultSpan = new Span<char>(resultPtr, result.Length);
str0.CopyTo(resultSpan);
str1.CopyTo(resultSpan.Slice(str0.Length));
}
return result;
}
internal static unsafe string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2)
{
var result = new string('\0', checked(str0.Length + str1.Length + str2.Length));
fixed (char* resultPtr = result)
{
var resultSpan = new Span<char>(resultPtr, result.Length);
str0.CopyTo(resultSpan);
resultSpan = resultSpan.Slice(str0.Length);
str1.CopyTo(resultSpan);
resultSpan = resultSpan.Slice(str1.Length);
str2.CopyTo(resultSpan);
}
return result;
}
}
}