Skip to content

Commit

Permalink
strlen to managed code and vectorize (dotnet/coreclr#21729)
Browse files Browse the repository at this point in the history
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
  • Loading branch information
benaadams authored and dotnet-bot committed Jan 2, 2019
1 parent e5a3b6e commit ce1df57
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions netcore/System.Private.CoreLib/shared/System/String.cs
Expand Up @@ -163,11 +163,7 @@ private unsafe string Ctor(sbyte* value)
if (pb == null)
return Empty;

int numBytes = new ReadOnlySpan<byte>((byte*)value, int.MaxValue).IndexOf<byte>(0);

// Check for overflow
if (numBytes < 0)
throw new ArgumentException(SR.Arg_MustBeNullTerminatedString);
int numBytes = strlen((byte*)value);

return CreateStringForSByteConstructor(pb, numBytes);
}
Expand Down Expand Up @@ -655,6 +651,25 @@ internal static unsafe int wcslen(char* ptr)
return count;
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe int strlen(byte* ptr)
{
// IndexOf processes memory in aligned chunks, and thus it won't crash even if it accesses memory beyond the null terminator.
int length = SpanHelpers.IndexOf(ref *ptr, (byte)'\0', int.MaxValue);
if (length < 0)
{
ThrowMustBeNullTerminatedString();
}

return length;
}

private static void ThrowMustBeNullTerminatedString()
{
throw new ArgumentException(SR.Arg_MustBeNullTerminatedString);
}

//
// IConvertible implementation
//
Expand Down

0 comments on commit ce1df57

Please sign in to comment.