-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathStringExtensions.cs
119 lines (104 loc) · 3.64 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using WebApiClientCore.Internals;
namespace WebApiClientCore
{
/// <summary>
/// string扩展
/// </summary>
static class StringExtensions
{
/// <summary>
/// 格式化字符串
/// </summary>
/// <param name="str"></param>
/// <param name="args"></param>
/// <returns></returns>
public static string Format(this string str, params object?[] args)
{
return string.Format(str, args);
}
/// <summary>
/// 不区分大小写替换字符串
/// </summary>
/// <param name="source"></param>
/// <param name="oldValue">要替换的值</param>
/// <param name="newValue">替换的新值</param>
/// <param name="replaced">是否替换成功</param>
/// <returns></returns>
public static string ReplaceIgnoreCase(this string source, ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, out bool replaced)
{
replaced = false;
if (string.IsNullOrEmpty(source) || oldValue.IsEmpty)
{
return source;
}
var index = 0;
var sourceSpan = source.AsSpan();
var builder = new ValueStringBuilder(stackalloc char[256]);
while ((index = FindIndexIgnoreCase(sourceSpan, oldValue)) > -1)
{
builder.Append(sourceSpan[..index]);
builder.Append(newValue);
sourceSpan = sourceSpan[(index + oldValue.Length)..];
replaced = true;
}
if (replaced == true)
{
builder.Append(sourceSpan);
return builder.ToString();
}
return source;
}
/// <summary>
/// 查找索引
/// </summary>
/// <param name="source"></param>
/// <param name="value"></param>
/// <returns></returns>
private static int FindIndexIgnoreCase(ReadOnlySpan<char> source, ReadOnlySpan<char> value)
{
int ctrSource; // index value into source
int ctrValue; // index value into value
char sourceChar; // Character for case lookup in source
char valueChar; // Character for case lookup in value
var sourceCount = source.Length;
var valueCount = value.Length;
if (valueCount == 0)
{
return 0;
}
if (sourceCount < valueCount)
{
return -1;
}
var lastSourceStart = sourceCount - valueCount;
var firstValueChar = InvariantCaseFold(value[0]);
for (ctrSource = 0; ctrSource <= lastSourceStart; ctrSource++)
{
sourceChar = InvariantCaseFold(source[ctrSource]);
if (sourceChar != firstValueChar)
{
continue;
}
for (ctrValue = 1; ctrValue < valueCount; ctrValue++)
{
sourceChar = InvariantCaseFold(source[ctrSource + ctrValue]);
valueChar = InvariantCaseFold(value[ctrValue]);
if (sourceChar != valueChar)
{
break;
}
}
if (ctrValue == valueCount)
{
return ctrSource;
}
}
return -1;
static char InvariantCaseFold(char c)
{
return (uint)(c - 'a') <= 'z' - 'a' ? (char)(c - 0x20) : c;
}
}
}
}