Skip to content

Commit 03fbac5

Browse files
committed
Merge pull request dotnet#402 from stephentoub/char_allocs
Remove unnecessary char-related allocations
2 parents 7bd5f37 + b1cfa0d commit 03fbac5

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

src/System.IO.FileSystem/src/System/IO/Directory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal static String EnsureTrailingDirectorySeparator(string fullPath)
5959
String fullPathWithTrailingDirectorySeparator;
6060

6161
if (!PathHelpers.EndsInDirectorySeparator(fullPath))
62-
fullPathWithTrailingDirectorySeparator = fullPath + Path.DirectorySeparatorChar;
62+
fullPathWithTrailingDirectorySeparator = fullPath + PathHelpers.DirectorySeparatorCharAsString;
6363
else
6464
fullPathWithTrailingDirectorySeparator = fullPath;
6565

src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,13 @@ public void MoveTo(String destDirName)
408408

409409
String fullDestDirName = PathHelpers.GetFullPathInternal(destDirName);
410410
if (fullDestDirName[fullDestDirName.Length - 1] != Path.DirectorySeparatorChar)
411-
fullDestDirName = fullDestDirName + Path.DirectorySeparatorChar;
411+
fullDestDirName = fullDestDirName + PathHelpers.DirectorySeparatorCharAsString;
412412

413413
String fullSourcePath;
414414
if (FullPath.Length > 0 && FullPath[FullPath.Length - 1] == Path.DirectorySeparatorChar)
415415
fullSourcePath = FullPath;
416416
else
417-
fullSourcePath = FullPath + Path.DirectorySeparatorChar;
417+
fullSourcePath = FullPath + PathHelpers.DirectorySeparatorCharAsString;
418418

419419
if (String.Compare(fullSourcePath, fullDestDirName, StringComparison.OrdinalIgnoreCase) == 0)
420420
throw new IOException(SR.IO_SourceDestMustBeDifferent);

src/System.IO.FileSystem/src/System/IO/PathHelpers.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ internal static class PathHelpers
1919
internal static readonly char[] TrimEndChars = { (char)0x9, (char)0xA, (char)0xB, (char)0xC, (char)0xD, (char)0x20, (char)0x85, (char)0xA0 };
2020
internal static readonly char[] TrimStartChars = { ' ' };
2121

22+
// Array of the separator chars
23+
internal static readonly char[] DirectorySeparatorChars = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
24+
25+
// String-representation of the directory-separator character, used when appending the character to another
26+
// string so as to avoid the boxing of the character when calling String.Concat(..., object).
27+
internal static readonly string DirectorySeparatorCharAsString = Path.DirectorySeparatorChar.ToString();
28+
2229
// Gets the length of the root DirectoryInfo or whatever DirectoryInfo markers
2330
// are specified for the first part of the DirectoryInfo name.
2431
//
@@ -37,7 +44,7 @@ internal static int GetRootLength(String path)
3744
{
3845
i = 2;
3946
int n = 2;
40-
while (i < length && ((path[i] != Path.DirectorySeparatorChar && path[i] != Path.AltDirectorySeparatorChar) || --n > 0)) i++;
47+
while (i < length && (!IsDirectorySeparator(path[i]) || --n > 0)) i++;
4148
}
4249
}
4350
else if (length >= 2 && path[1] == Path.VolumeSeparatorChar)
@@ -62,8 +69,7 @@ internal static void CheckSearchPattern(String searchPattern)
6269
if (index + 2 == searchPattern.Length) // Terminal ".." . Files names cannot end in ".."
6370
throw new ArgumentException(SR.Arg_InvalidSearchPattern);
6471

65-
if ((searchPattern[index + 2] == Path.DirectorySeparatorChar)
66-
|| (searchPattern[index + 2] == Path.AltDirectorySeparatorChar))
72+
if (IsDirectorySeparator(searchPattern[index + 2]))
6773
throw new ArgumentException(SR.Arg_InvalidSearchPattern);
6874

6975
searchPattern = searchPattern.Substring(index + 2);

src/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ internal static int FillAttributeInfo(String path, ref Interop.WIN32_FILE_ATTRIB
216216
findData = new Interop.WIN32_FIND_DATA();
217217

218218
// Remove trialing slash since this can cause grief to FindFirstFile. You will get an invalid argument error
219-
String tempPath = path.TrimEnd(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
219+
String tempPath = path.TrimEnd(PathHelpers.DirectorySeparatorChars);
220220

221221
// For floppy drives, normally the OS will pop up a dialog saying
222222
// there is no disk in drive A:, please insert one. We don't want that.
@@ -507,7 +507,7 @@ private static void RemoveDirectoryHelper(String fullPath, bool recursive, bool
507507
Interop.WIN32_FIND_DATA data = new Interop.WIN32_FIND_DATA();
508508

509509
// Open a Find handle
510-
using (SafeFindHandle hnd = Interop.mincore.FindFirstFile(fullPath + Path.DirectorySeparatorChar + "*", ref data))
510+
using (SafeFindHandle hnd = Interop.mincore.FindFirstFile(fullPath + PathHelpers.DirectorySeparatorCharAsString + "*", ref data))
511511
{
512512
if (hnd.IsInvalid)
513513
throw Win32Marshal.GetExceptionForLastWin32Error(fullPath);
@@ -546,7 +546,7 @@ private static void RemoveDirectoryHelper(String fullPath, bool recursive, bool
546546
if (data.dwReserved0 == Interop.IO_REPARSE_TAG_MOUNT_POINT)
547547
{
548548
// Use full path plus a trailing '\'
549-
String mountPoint = Path.Combine(fullPath, data.cFileName + Path.DirectorySeparatorChar);
549+
String mountPoint = Path.Combine(fullPath, data.cFileName + PathHelpers.DirectorySeparatorCharAsString);
550550
r = Interop.mincore.DeleteVolumeMountPoint(mountPoint);
551551
if (!r)
552552
{

src/System.IO.FileSystem/src/System/IO/Win32FileSystemEnumerable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ private static String GetFullSearchString(String fullPath, String searchPattern)
502502
char lastChar = tempStr[tempStr.Length - 1];
503503
if (PathHelpers.IsDirectorySeparator(lastChar) || lastChar == Path.VolumeSeparatorChar)
504504
{
505-
tempStr = tempStr + '*';
505+
tempStr = tempStr + "*";
506506
}
507507

508508
return tempStr;

0 commit comments

Comments
 (0)