Repository navigation
Expand file tree
/
Copy pathStringHelper.cs
More file actions
132 lines (115 loc) · 3.42 KB
/
Copy pathStringHelper.cs
File metadata and controls
132 lines (115 loc) · 3.42 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Runtime.CompilerServices;
using System.Text;
namespace Lua.Internal;
static class StringHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<char> Slice(string s, int i, int j)
{
i = RelativePosition(i, s.Length);
j = RelativePosition(j, s.Length);
if (i < 1)
i = 1;
if (j > s.Length)
j = s.Length;
return i > j ? "" : s.AsSpan()[(i - 1)..j];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int RelativePosition(int index, int length) =>
index < 0 ? length + index + 1 : index;
public static string Quote(ReadOnlySpan<char> str)
{
ValueStringBuilder builder = new(str.Length + 2);
builder.Append('"');
for (var i = 0; i < str.Length; i++)
{
var c = str[i];
if (c is '"' or '\\' or '\n')
{
builder.Append('\\');
builder.Append(c);
}
else if (c < 32 || c == 127)
{
// A following digit must not become part of this decimal escape.
var pad = i + 1 < str.Length && IsNumber(str[i + 1]);
builder.Append('\\');
if (c >= 100 || pad)
builder.Append((char)('0' + c / 100));
if (c >= 10 || pad)
builder.Append((char)('0' + c / 10 % 10));
builder.Append((char)('0' + c % 10));
}
else
{
builder.Append(c);
}
}
builder.Append('"');
return builder.ToString();
}
/// <summary>
/// toupper in the C locale: only 'a'-'z' change, every other char (including bytes 0x80-0xFF) is kept.
/// </summary>
public static string ToUpperAscii(string s)
{
var i = 0;
while (i < s.Length && !(s[i] >= 'a' && s[i] <= 'z'))
{
i++;
}
if (i == s.Length)
{
return s;
}
return string.Create(
s.Length,
s,
static (span, source) =>
{
for (var k = 0; k < span.Length; k++)
{
var c = source[k];
span[k] = c >= 'a' && c <= 'z' ? (char)(c - 32) : c;
}
}
);
}
/// <summary>
/// tolower in the C locale: only 'A'-'Z' change, every other char (including bytes 0x80-0xFF) is kept.
/// </summary>
public static string ToLowerAscii(string s)
{
var i = 0;
while (i < s.Length && !(s[i] >= 'A' && s[i] <= 'Z'))
{
i++;
}
if (i == s.Length)
{
return s;
}
return string.Create(
s.Length,
s,
static (span, source) =>
{
for (var k = 0; k < span.Length; k++)
{
var c = source[k];
span[k] = c >= 'A' && c <= 'Z' ? (char)(c + 32) : c;
}
}
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNumber(char c)
{
return '0' <= c && c <= '9';
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDigit(char c)
{
return IsNumber(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
}
}