If the output matches the input string, we can just use the input string as the result.
{
sealed partial class IdnMapping
{
- private unsafe string GetAsciiCore(char* unicode, int count)
+ private unsafe string GetAsciiCore(string unicodeString, char* unicode, int count)
{
Debug.Assert(!GlobalizationMode.Invariant);
+ Debug.Assert(unicodeString != null && unicodeString.Length >= count);
uint flags = Flags;
CheckInvalidIdnCharacters(unicode, count, flags, nameof(unicode));
actualLength = Interop.Globalization.ToAscii(flags, unicode, count, outputStack, estimatedLength);
if (actualLength > 0 && actualLength <= estimatedLength)
{
- return new string(outputStack, 0, actualLength);
+ return GetStringForOutput(unicodeString, unicode, count, outputStack, actualLength);
}
}
else
{
throw new ArgumentException(SR.Argument_IdnIllegalName, nameof(unicode));
}
- return new string(pOutputHeap, 0, actualLength);
+ return GetStringForOutput(unicodeString, unicode, count, pOutputHeap, actualLength);
}
}
- private unsafe string GetUnicodeCore(char* ascii, int count)
+ private unsafe string GetUnicodeCore(string asciiString, char* ascii, int count)
{
Debug.Assert(!GlobalizationMode.Invariant);
+ Debug.Assert(asciiString != null && asciiString.Length >= count);
uint flags = Flags;
CheckInvalidIdnCharacters(ascii, count, flags, nameof(ascii));
if (count < StackAllocThreshold)
{
char* output = stackalloc char[count];
- return GetUnicodeCore(ascii, count, flags, output, count, reattempt: true);
+ return GetUnicodeCore(asciiString, ascii, count, flags, output, count, reattempt: true);
}
else
{
char[] output = new char[count];
fixed (char* pOutput = &output[0])
{
- return GetUnicodeCore(ascii, count, flags, pOutput, count, reattempt: true);
+ return GetUnicodeCore(asciiString, ascii, count, flags, pOutput, count, reattempt: true);
}
}
}
- private unsafe string GetUnicodeCore(char* ascii, int count, uint flags, char* output, int outputLength, bool reattempt)
+ private unsafe string GetUnicodeCore(string asciiString, char* ascii, int count, uint flags, char* output, int outputLength, bool reattempt)
{
Debug.Assert(!GlobalizationMode.Invariant);
+ Debug.Assert(asciiString != null && asciiString.Length >= count);
int realLen = Interop.Globalization.ToUnicode(flags, ascii, count, output, outputLength);
}
else if (realLen <= outputLength)
{
- return new string(output, 0, realLen);
+ return GetStringForOutput(asciiString, ascii, count, output, realLen);
}
else if (reattempt)
{
char[] newOutput = new char[realLen];
fixed (char* pNewOutput = newOutput)
{
- return GetUnicodeCore(ascii, count, flags, pNewOutput, realLen, reattempt: false);
+ return GetUnicodeCore(asciiString, ascii, count, flags, pNewOutput, realLen, reattempt: false);
}
}
{
public sealed partial class IdnMapping
{
- private unsafe string GetAsciiCore(char* unicode, int count)
+ private unsafe string GetAsciiCore(string unicodeString, char* unicode, int count)
{
Debug.Assert(!GlobalizationMode.Invariant);
+ Debug.Assert(unicodeString != null && unicodeString.Length >= count);
uint flags = Flags;
if (length < StackAllocThreshold)
{
char* output = stackalloc char[length];
- return GetAsciiCore(unicode, count, flags, output, length);
+ return GetAsciiCore(unicodeString, unicode, count, flags, output, length);
}
else
{
char[] output = new char[length];
fixed (char* pOutput = &output[0])
{
- return GetAsciiCore(unicode, count, flags, pOutput, length);
+ return GetAsciiCore(unicodeString, unicode, count, flags, pOutput, length);
}
}
}
- private unsafe string GetAsciiCore(char* unicode, int count, uint flags, char* output, int outputLength)
+ private unsafe string GetAsciiCore(string unicodeString, char* unicode, int count, uint flags, char* output, int outputLength)
{
Debug.Assert(!GlobalizationMode.Invariant);
+ Debug.Assert(unicodeString != null && unicodeString.Length >= count);
int length = Interop.Normaliz.IdnToAscii(flags, unicode, count, output, outputLength);
if (length == 0)
ThrowForZeroLength(unicode: true);
}
Debug.Assert(length == outputLength);
- return new string(output, 0, length);
+ return GetStringForOutput(unicodeString, unicode, count, output, length);
}
- private unsafe string GetUnicodeCore(char* ascii, int count)
+ private unsafe string GetUnicodeCore(string asciiString, char* ascii, int count)
{
Debug.Assert(!GlobalizationMode.Invariant);
+ Debug.Assert(asciiString != null && asciiString.Length >= count);
uint flags = Flags;
if (length < StackAllocThreshold)
{
char* output = stackalloc char[length];
- return GetUnicodeCore(ascii, count, flags, output, length);
+ return GetUnicodeCore(asciiString, ascii, count, flags, output, length);
}
else
{
char[] output = new char[length];
fixed (char* pOutput = &output[0])
{
- return GetUnicodeCore(ascii, count, flags, pOutput, length);
+ return GetUnicodeCore(asciiString, ascii, count, flags, pOutput, length);
}
}
}
- private unsafe string GetUnicodeCore(char* ascii, int count, uint flags, char* output, int outputLength)
+ private unsafe string GetUnicodeCore(string asciiString, char* ascii, int count, uint flags, char* output, int outputLength)
{
Debug.Assert(!GlobalizationMode.Invariant);
+ Debug.Assert(asciiString != null && asciiString.Length >= count);
int length = Interop.Normaliz.IdnToUnicode(flags, ascii, count, output, outputLength);
if (length == 0)
ThrowForZeroLength(unicode: false);
}
Debug.Assert(length == outputLength);
- return new string(output, 0, length);
+ return GetStringForOutput(asciiString, ascii, count, output, length);
}
// -----------------------------
// RFC 3492 - Punycode: A Bootstring encoding of Unicode for Internationalized Domain Names in Applications (IDNA)
using System.Diagnostics;
+using System.Runtime.CompilerServices;
using System.Text;
namespace System.Globalization
{
fixed (char* pUnicode = unicode)
{
- return GetAsciiCore(pUnicode + index, count);
+ return GetAsciiCore(unicode, pUnicode + index, count);
}
}
}
{
fixed (char* pAscii = ascii)
{
- return GetUnicodeCore(pAscii + index, count);
+ return GetUnicodeCore(ascii, pAscii + index, count);
}
}
}
return (_allowUnassigned ? 100 : 200) + (_useStd3AsciiRules ? 1000 : 2000);
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static unsafe string GetStringForOutput(string originalString, char* input, int inputLength, char* output, int outputLength)
+ {
+ return originalString.Length == inputLength && new ReadOnlySpan<char>(input, inputLength).SequenceEqual(new ReadOnlySpan<char>(output, outputLength)) ?
+ originalString :
+ new string(output, 0, outputLength);
+ }
+
//
// Invariant implementation
//