I've found some interesting function inside String.cs (disassembled):
private static unsafe int wcslen(char* ptr)
{
char* end = ptr;
while (((uint)end & 3) != 0 && *end != 0)
end++;
if (*end != 0)
{
while ((end[0] & end[1]) != 0 || (end[0] != 0 && end[1] != 0))
{
end += 2;
}
}
for (; *end != 0; end++)
;
int count = (int)(end - ptr);
return count;
}
usage:
class Program
{
private static void Main(string[] args)
{
var a = "asample";
unsafe
{
fixed (char* str_char = a)
{
var count = wcslen(str_char);
}
}
}
private static unsafe int wcslen(char* ptr)
{
// impl
}
}
Just curiosity. Not recommended in real code. The most interesting thing:
Here is comment inside that wcslen function:
// The following code is (somewhat surprisingly!) significantly faster than a naive loop,
// at least on x86 and the current jit.
:)