internal String Normalize(String strInput)
{
if (strInput == null)
throw new ArgumentNullException( "strInput",
Environment.GetResourceString("ArgumentNull_String"));
// Guess our buffer size first
int iLength = GuessLength(strInput);
// Don't break for empty strings (only possible for D & KD and not really possible at that)
if (iLength == 0) return String.Empty;
// Someplace to stick our buffer
char[] cBuffer = null;
int iError = ERROR_INSUFFICIENT_BUFFER;
while (iError == ERROR_INSUFFICIENT_BUFFER)
{
// (re)allocation buffer and normalize string
cBuffer = new char[iLength];
iLength = nativeNormalizationNormalizeString(
normalizationForm, ref iError,
strInput, strInput.Length, cBuffer, cBuffer.Length);
// Could have an error (actually it'd be quite hard to have an error here)
if (iError != ERROR_SUCCESS)
{
switch(iError)
{
// Do appropriate stuff for the individual errors:
case ERROR_INSUFFICIENT_BUFFER:
BCLDebug.Assert(iLength > cBuffer.Length, "Buffer overflow should have iLength > cBuffer.Length");
continue;
case ERROR_NO_UNICODE_TRANSLATION:
// Illegal code point or order found. Ie: FFFE or D800 D800, etc.
throw new ArgumentException(
Environment.GetResourceString("Argument_InvalidCharSequence", iLength ),
"strInput");
case ERROR_NOT_ENOUGH_MEMORY:
throw new OutOfMemoryException(
Environment.GetResourceString("Arg_OutOfMemoryException"));
case ERROR_INVALID_PARAMETER:
// Shouldn't have invalid parameters here unless we have a bug, drop through...
default:
// We shouldn't get here...
throw new InvalidOperationException(
Environment.GetResourceString("UnknownError_Num", iError));
}
}
}
// Copy our buffer into our new string, which will be the appropriate size
String strReturn = new String(cBuffer, 0, iLength);
// Return our output string
return strReturn;
}