Is uppercase string always of the same length as the original one?
c#, unicode
Solution
I can give a partial answer. For all strings of length 2 (of which there are about 4 billion), and for the German culture (`de-DE`) your assertions hold:
static unsafe void TestUnicodeLength2()
{
Parallel.For(char.MinValue, char.MaxValue + 1, charVal =>
{
var firstChar = checked((char)charVal);
var buffer = new string(firstChar, 2);
fixed (char* bufferPtr = buffer)
{
var currentCulture = CultureInfo.CurrentCulture;
for (int i = char.MinValue; i <= char.MaxValue; i++)
{
bufferPtr[1] = checked((char)i);
var toLower = buffer.ToLower(currentCulture);
if (toLower.Length != buffer.Length)
{
Console.WriteLine(buffer + " => " + toLower);
Debugger.Break();
}
var toUpper = buffer.ToUpper(currentCulture);
if (toUpper.Length != buffer.Length)
{
Console.WriteLine(buffer + " => " + toUpper);
Debugger.Break();
}
}
}
});
}
This runs for about 2 minutes.
I think this is rather strong evidence that the assertions always hold because by testing all possible combinations of two chars we automatically test all code points in existence and all strange combinations that no one would ever think about.
Update: I later ran a similar test for random strings (each 256 chars in length) for 256 billion characters in total length. The assertions still hold.
Problem
Is the length of an unicode uppercase string always the same as the length of an original string, no matter what culture is used? Is the length of an unicode lowercase string always the same as the length of an original string, no matter what culture is used? In other words, is the following true in C#? ``` text.ToUpper(CultureInfo.CurrentCulture).Length == text.Length text.ToLower(CultureInfo.CurrentCulture).Length == text.Length ``` Note that I'm not interested about the number of bytes: the question about that is already answered.