Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some calls to wstrcpy #32342

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class String
{
private const int StackallocIntBufferSizeLimit = 128;

private static unsafe void FillStringChecked(string dest, int destPos, string src)
private static void FillStringChecked(string dest, int destPos, string src)
{
Debug.Assert(dest != null);
Debug.Assert(src != null);
Expand All @@ -24,11 +24,10 @@ private static unsafe void FillStringChecked(string dest, int destPos, string sr
throw new IndexOutOfRangeException();
}

fixed (char* pDest = &dest._firstChar)
fixed (char* pSrc = &src._firstChar)
{
wstrcpy(pDest + destPos, pSrc, src.Length);
}
Buffer.Memmove(
destination: ref Unsafe.Add(ref dest._firstChar, destPos),
source: ref src._firstChar,
elementCount: (uint)src.Length);
}

public static string Concat(object? arg0) => arg0?.ToString() ?? string.Empty;
Expand Down Expand Up @@ -1672,11 +1671,10 @@ private unsafe string InternalSubString(int startIndex, int length)

string result = FastAllocateString(length);

fixed (char* dest = &result._firstChar)
fixed (char* src = &_firstChar)
{
wstrcpy(dest, src + startIndex, length);
}
Buffer.Memmove(
elementCount: (uint)result.Length, // derefing Length now allows JIT to prove 'result' not null below
destination: ref result._firstChar,
source: ref Unsafe.Add(ref _firstChar, startIndex));

return result;
}
Expand Down
58 changes: 38 additions & 20 deletions src/libraries/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ private string Ctor(char[]? value)
return Empty;

string result = FastAllocateString(value.Length);
unsafe
{
fixed (char* dest = &result._firstChar, source = value)
wstrcpy(dest, source, value.Length);
}

Buffer.Memmove(
elementCount: (uint)result.Length, // derefing Length now allows JIT to prove 'result' not null below
destination: ref result._firstChar,
source: ref MemoryMarshal.GetArrayDataReference(value));

return result;
}

Expand Down Expand Up @@ -91,11 +92,12 @@ private string Ctor(char[] value, int startIndex, int length)
return Empty;

string result = FastAllocateString(length);
unsafe
{
fixed (char* dest = &result._firstChar, source = value)
wstrcpy(dest, source + startIndex, length);
}

Buffer.Memmove(
elementCount: (uint)result.Length, // derefing Length now allows JIT to prove 'result' not null below
destination: ref result._firstChar,
source: ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(value), startIndex));

return result;
}

Expand All @@ -117,8 +119,12 @@ private unsafe string Ctor(char* ptr)
return Empty;

string result = FastAllocateString(count);
fixed (char* dest = &result._firstChar)
wstrcpy(dest, ptr, count);

Buffer.Memmove(
elementCount: (uint)result.Length, // derefing Length now allows JIT to prove 'result' not null below
destination: ref result._firstChar,
source: ref *ptr);

return result;
}

Expand Down Expand Up @@ -151,8 +157,12 @@ private unsafe string Ctor(char* ptr, int startIndex, int length)
throw new ArgumentOutOfRangeException(nameof(ptr), SR.ArgumentOutOfRange_PartialWCHAR);

string result = FastAllocateString(length);
fixed (char* dest = &result._firstChar)
wstrcpy(dest, pStart, length);

Buffer.Memmove(
elementCount: (uint)result.Length, // derefing Length now allows JIT to prove 'result' not null below
destination: ref result._firstChar,
source: ref *pStart);

return result;
}

Expand Down Expand Up @@ -398,20 +408,24 @@ public unsafe void CopyTo(int sourceIndex, char[] destination, int destinationIn
}

// Returns the entire string as an array of characters.
public unsafe char[] ToCharArray()
public char[] ToCharArray()
{
if (Length == 0)
return Array.Empty<char>();

char[] chars = new char[Length];
fixed (char* src = &_firstChar, dest = &chars[0])
wstrcpy(dest, src, Length);

Buffer.Memmove(
destination: ref MemoryMarshal.GetArrayDataReference(chars),
source: ref _firstChar,
elementCount: (uint)Length);

return chars;
}

// Returns a substring of this string as an array of characters.
//
public unsafe char[] ToCharArray(int startIndex, int length)
public char[] ToCharArray(int startIndex, int length)
{
// Range check everything.
if (startIndex < 0 || startIndex > Length || startIndex > Length - length)
Expand All @@ -425,8 +439,12 @@ public unsafe char[] ToCharArray(int startIndex, int length)
}

char[] chars = new char[length];
fixed (char* src = &_firstChar, dest = &chars[0])
wstrcpy(dest, src + startIndex, length);

Buffer.Memmove(
destination: ref MemoryMarshal.GetArrayDataReference(chars),
source: ref Unsafe.Add(ref _firstChar, startIndex),
elementCount: (uint)length);

return chars;
}

Expand Down