public static int Foo(string str)
{
int length = str.Length;
while ((uint)(length - 1) < (uint)str.Length && str[length - 1] == ' ')
{
length--;
}
return length;
}
public static int Bar(string str)
{
int length = str.Length;
while (length > 0 && str[length - 1] == ' ')
{
length--;
}
return length;
}
In .NET 9, Foo elided the extra bounds check, but godbolt is showing it again on main https://godbolt.org/z/qnYEzcdne.
Bar also seems like something where the JIT could prove that an extra check isn't needed.
In .NET 9,
Fooelided the extra bounds check, but godbolt is showing it again on main https://godbolt.org/z/qnYEzcdne.Baralso seems like something where the JIT could prove that an extra check isn't needed.