Skip to content

Commit 5e3facf

Browse files
Copilotjkotas
andcommitted
Remove GetAsciiCore/GetUnicodeCore indirection, inline directly into callers
Co-authored-by: jkotas <[email protected]>
1 parent 511b2c5 commit 5e3facf

1 file changed

Lines changed: 37 additions & 50 deletions

File tree

  • src/libraries/System.Private.CoreLib/src/System/Globalization

src/libraries/System.Private.CoreLib/src/System/Globalization/IdnMapping.cs

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -75,46 +75,40 @@ public string GetAscii(string unicode, int index, int count)
7575
if (index > unicode.Length - count)
7676
throw new ArgumentOutOfRangeException(nameof(unicode), SR.ArgumentOutOfRange_IndexCountBuffer);
7777

78-
return GetAsciiCore(unicode, unicode.AsSpan(index, count));
79-
}
80-
81-
/// <summary>
82-
/// Encodes a substring of domain name labels that include Unicode characters outside the ASCII character range (U+0000 to U+007F) to a displayable Unicode string.
83-
/// </summary>
84-
/// <param name="unicode">The Unicode domain name to convert.</param>
85-
/// <param name="destination">The buffer to write the ASCII result to.</param>
86-
/// <param name="charsWritten">When this method returns, contains the number of characters that were written to <paramref name="destination"/>.</param>
87-
/// <returns><see langword="true"/> if the conversion was successful and the result was written to <paramref name="destination"/>; otherwise, <see langword="false"/> if <paramref name="destination"/> is too small to contain the result.</returns>
88-
/// <exception cref="ArgumentException"><paramref name="unicode"/> is invalid based on the <see cref="AllowUnassigned"/> and <see cref="UseStd3AsciiRules"/> properties, and the IDNA standard.</exception>
89-
public bool TryGetAscii(ReadOnlySpan<char> unicode, Span<char> destination, out int charsWritten)
90-
{
91-
if (unicode.Length == 0)
78+
if (count == 0)
9279
{
9380
throw new ArgumentException(SR.Argument_IdnBadLabelSize, nameof(unicode));
9481
}
95-
if (unicode[^1] == 0)
82+
if (unicode[index + count - 1] == 0)
9683
{
97-
throw new ArgumentException(SR.Format(SR.Argument_InvalidCharSequence, unicode.Length - 1), nameof(unicode));
84+
throw new ArgumentException(SR.Format(SR.Argument_InvalidCharSequence, index + count - 1), nameof(unicode));
9885
}
9986

10087
if (GlobalizationMode.Invariant)
10188
{
102-
return TryGetAsciiInvariant(unicode, destination, out charsWritten);
89+
return GetAsciiInvariant(unicode.AsSpan(index, count));
10390
}
10491

10592
unsafe
10693
{
107-
fixed (char* pUnicode = &MemoryMarshal.GetReference(unicode))
108-
fixed (char* pDestination = &MemoryMarshal.GetReference(destination))
94+
fixed (char* pUnicode = unicode)
10995
{
11096
return GlobalizationMode.UseNls ?
111-
NlsTryGetAsciiCore(pUnicode, unicode.Length, pDestination, destination.Length, out charsWritten) :
112-
IcuTryGetAsciiCore(pUnicode, unicode.Length, pDestination, destination.Length, out charsWritten);
97+
NlsGetAsciiCore(unicode, pUnicode + index, count) :
98+
IcuGetAsciiCore(unicode, pUnicode + index, count);
11399
}
114100
}
115101
}
116102

117-
private string GetAsciiCore(string? originalUnicode, ReadOnlySpan<char> unicode)
103+
/// <summary>
104+
/// Encodes a substring of domain name labels that include Unicode characters outside the ASCII character range (U+0000 to U+007F) to a displayable Unicode string.
105+
/// </summary>
106+
/// <param name="unicode">The Unicode domain name to convert.</param>
107+
/// <param name="destination">The buffer to write the ASCII result to.</param>
108+
/// <param name="charsWritten">When this method returns, contains the number of characters that were written to <paramref name="destination"/>.</param>
109+
/// <returns><see langword="true"/> if the conversion was successful and the result was written to <paramref name="destination"/>; otherwise, <see langword="false"/> if <paramref name="destination"/> is too small to contain the result.</returns>
110+
/// <exception cref="ArgumentException"><paramref name="unicode"/> is invalid based on the <see cref="AllowUnassigned"/> and <see cref="UseStd3AsciiRules"/> properties, and the IDNA standard.</exception>
111+
public bool TryGetAscii(ReadOnlySpan<char> unicode, Span<char> destination, out int charsWritten)
118112
{
119113
if (unicode.Length == 0)
120114
{
@@ -127,16 +121,17 @@ private string GetAsciiCore(string? originalUnicode, ReadOnlySpan<char> unicode)
127121

128122
if (GlobalizationMode.Invariant)
129123
{
130-
return GetAsciiInvariant(unicode);
124+
return TryGetAsciiInvariant(unicode, destination, out charsWritten);
131125
}
132126

133127
unsafe
134128
{
135129
fixed (char* pUnicode = &MemoryMarshal.GetReference(unicode))
130+
fixed (char* pDestination = &MemoryMarshal.GetReference(destination))
136131
{
137132
return GlobalizationMode.UseNls ?
138-
NlsGetAsciiCore(originalUnicode, pUnicode, unicode.Length) :
139-
IcuGetAsciiCore(originalUnicode, pUnicode, unicode.Length);
133+
NlsTryGetAsciiCore(pUnicode, unicode.Length, pDestination, destination.Length, out charsWritten) :
134+
IcuTryGetAsciiCore(pUnicode, unicode.Length, pDestination, destination.Length, out charsWritten);
140135
}
141136
}
142137
}
@@ -166,7 +161,23 @@ public string GetUnicode(string ascii, int index, int count)
166161
// This is a case (i.e. explicitly null-terminated input) where behavior in .NET and Win32 intentionally differ.
167162
// The .NET APIs should (and did in v4.0 and earlier) throw an ArgumentException on input that includes a terminating null.
168163
// The Win32 APIs fail on an embedded null, but not on a terminating null.
169-
return GetUnicodeCore(ascii, ascii.AsSpan(index, count));
164+
if (count > 0 && ascii[index + count - 1] == (char)0)
165+
throw new ArgumentException(SR.Argument_IdnBadPunycode, nameof(ascii));
166+
167+
if (GlobalizationMode.Invariant)
168+
{
169+
return GetUnicodeInvariant(ascii.AsSpan(index, count));
170+
}
171+
172+
unsafe
173+
{
174+
fixed (char* pAscii = ascii)
175+
{
176+
return GlobalizationMode.UseNls ?
177+
NlsGetUnicodeCore(ascii, pAscii + index, count) :
178+
IcuGetUnicodeCore(ascii, pAscii + index, count);
179+
}
180+
}
170181
}
171182

172183
/// <summary>
@@ -202,30 +213,6 @@ public bool TryGetUnicode(ReadOnlySpan<char> ascii, Span<char> destination, out
202213
}
203214
}
204215

205-
private string GetUnicodeCore(string? originalAscii, ReadOnlySpan<char> ascii)
206-
{
207-
// This is a case (i.e. explicitly null-terminated input) where behavior in .NET and Win32 intentionally differ.
208-
// The .NET APIs should (and did in v4.0 and earlier) throw an ArgumentException on input that includes a terminating null.
209-
// The Win32 APIs fail on an embedded null, but not on a terminating null.
210-
if (ascii.Length > 0 && ascii[^1] == (char)0)
211-
throw new ArgumentException(SR.Argument_IdnBadPunycode, nameof(ascii));
212-
213-
if (GlobalizationMode.Invariant)
214-
{
215-
return GetUnicodeInvariant(ascii);
216-
}
217-
218-
unsafe
219-
{
220-
fixed (char* pAscii = &MemoryMarshal.GetReference(ascii))
221-
{
222-
return GlobalizationMode.UseNls ?
223-
NlsGetUnicodeCore(originalAscii, pAscii, ascii.Length) :
224-
IcuGetUnicodeCore(originalAscii, pAscii, ascii.Length);
225-
}
226-
}
227-
}
228-
229216
public override bool Equals([NotNullWhen(true)] object? obj) =>
230217
obj is IdnMapping that &&
231218
_allowUnassigned == that._allowUnassigned &&

0 commit comments

Comments
 (0)