Skip to content

Commit 4e15ca7

Browse files
committed
New ext syntax + removed dead helper
1 parent 14d7707 commit 4e15ca7

1 file changed

Lines changed: 44 additions & 57 deletions

File tree

src/FirebirdSql.Data.FirebirdClient/Common/Extensions.cs

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,6 @@ public IEnumerable<IEnumerable<T>> Split(int size)
7272
}
7373
}
7474

75-
extension(string s)
76-
{
77-
public IEnumerable<char[]> EnumerateRunesToChars()
78-
{
79-
if (s == null)
80-
throw new ArgumentNullException(nameof(s));
81-
82-
return s.EnumerateRunes().Select(r =>
83-
{
84-
var result = new char[r.Utf16SequenceLength];
85-
r.EncodeToUtf16(result);
86-
return result;
87-
});
88-
}
89-
}
90-
9175
extension(Encoding)
9276
{
9377
public static Encoding GetANSIEncoding()
@@ -103,59 +87,62 @@ public static Encoding GetANSIEncoding()
10387
}
10488
}
10589

106-
public static int CountRunes(this ReadOnlySpan<char> text)
90+
extension(ReadOnlySpan<char> text)
10791
{
108-
var length = text.Length;
109-
if(length == 0)
110-
return 0;
92+
public int CountRunes()
93+
{
94+
var length = text.Length;
95+
if(length == 0)
96+
return 0;
11197

112-
var i = text.IndexOfAnyInRange('\uD800', '\uDBFF');
113-
if(i < 0)
114-
return length;
98+
var i = text.IndexOfAnyInRange('\uD800', '\uDBFF');
99+
if(i < 0)
100+
return length;
115101

116-
var count = i;
117-
while(i < length)
118-
{
119-
if(char.IsHighSurrogate(text[i]) && i + 1 < length && char.IsLowSurrogate(text[i + 1]))
102+
var count = i;
103+
while(i < length)
120104
{
121-
i += 2;
105+
if(char.IsHighSurrogate(text[i]) && i + 1 < length && char.IsLowSurrogate(text[i + 1]))
106+
{
107+
i += 2;
108+
}
109+
else
110+
{
111+
i++;
112+
}
113+
count++;
122114
}
123-
else
124-
{
125-
i++;
126-
}
127-
count++;
115+
return count;
128116
}
129-
return count;
130-
}
131117

132-
public static ReadOnlySpan<char> TruncateStringToRuneCount(this ReadOnlySpan<char> text, int maxRuneCount)
133-
{
134-
if(maxRuneCount <= 0 || text.IsEmpty)
135-
return ReadOnlySpan<char>.Empty;
118+
public ReadOnlySpan<char> TruncateStringToRuneCount(int maxRuneCount)
119+
{
120+
if(maxRuneCount <= 0 || text.IsEmpty)
121+
return ReadOnlySpan<char>.Empty;
136122

137-
var length = text.Length;
138-
if(maxRuneCount >= length)
139-
return text;
123+
var length = text.Length;
124+
if(maxRuneCount >= length)
125+
return text;
140126

141-
var prefix = text[..maxRuneCount];
142-
var i = prefix.IndexOfAnyInRange('\uD800', '\uDBFF');
143-
if(i < 0)
144-
return prefix;
127+
var prefix = text[..maxRuneCount];
128+
var i = prefix.IndexOfAnyInRange('\uD800', '\uDBFF');
129+
if(i < 0)
130+
return prefix;
145131

146-
var remaining = maxRuneCount - i;
147-
while(i < length && remaining > 0)
148-
{
149-
if(char.IsHighSurrogate(text[i]) && i + 1 < length && char.IsLowSurrogate(text[i + 1]))
150-
{
151-
i += 2;
152-
}
153-
else
132+
var remaining = maxRuneCount - i;
133+
while(i < length && remaining > 0)
154134
{
155-
i++;
135+
if(char.IsHighSurrogate(text[i]) && i + 1 < length && char.IsLowSurrogate(text[i + 1]))
136+
{
137+
i += 2;
138+
}
139+
else
140+
{
141+
i++;
142+
}
143+
remaining--;
156144
}
157-
remaining--;
145+
return text[..i];
158146
}
159-
return text[..i];
160147
}
161148
}

0 commit comments

Comments
 (0)