Skip to content

Commit

Permalink
Merge pull request dotnet#402 from stephentoub/char_allocs
Browse files Browse the repository at this point in the history
Remove unnecessary char-related allocations
stephentoub committed Jan 13, 2015
2 parents 7bd5f37 + b1cfa0d commit 03fbac5
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/System.IO.FileSystem/src/System/IO/Directory.cs
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ internal static String EnsureTrailingDirectorySeparator(string fullPath)
String fullPathWithTrailingDirectorySeparator;

if (!PathHelpers.EndsInDirectorySeparator(fullPath))
fullPathWithTrailingDirectorySeparator = fullPath + Path.DirectorySeparatorChar;
fullPathWithTrailingDirectorySeparator = fullPath + PathHelpers.DirectorySeparatorCharAsString;
else
fullPathWithTrailingDirectorySeparator = fullPath;

4 changes: 2 additions & 2 deletions src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs
Original file line number Diff line number Diff line change
@@ -408,13 +408,13 @@ public void MoveTo(String destDirName)

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

String fullSourcePath;
if (FullPath.Length > 0 && FullPath[FullPath.Length - 1] == Path.DirectorySeparatorChar)
fullSourcePath = FullPath;
else
fullSourcePath = FullPath + Path.DirectorySeparatorChar;
fullSourcePath = FullPath + PathHelpers.DirectorySeparatorCharAsString;

if (String.Compare(fullSourcePath, fullDestDirName, StringComparison.OrdinalIgnoreCase) == 0)
throw new IOException(SR.IO_SourceDestMustBeDifferent);
12 changes: 9 additions & 3 deletions src/System.IO.FileSystem/src/System/IO/PathHelpers.cs
Original file line number Diff line number Diff line change
@@ -19,6 +19,13 @@ internal static class PathHelpers
internal static readonly char[] TrimEndChars = { (char)0x9, (char)0xA, (char)0xB, (char)0xC, (char)0xD, (char)0x20, (char)0x85, (char)0xA0 };
internal static readonly char[] TrimStartChars = { ' ' };

// Array of the separator chars
internal static readonly char[] DirectorySeparatorChars = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };

// String-representation of the directory-separator character, used when appending the character to another
// string so as to avoid the boxing of the character when calling String.Concat(..., object).
internal static readonly string DirectorySeparatorCharAsString = Path.DirectorySeparatorChar.ToString();

// Gets the length of the root DirectoryInfo or whatever DirectoryInfo markers
// are specified for the first part of the DirectoryInfo name.
//
@@ -37,7 +44,7 @@ internal static int GetRootLength(String path)
{
i = 2;
int n = 2;
while (i < length && ((path[i] != Path.DirectorySeparatorChar && path[i] != Path.AltDirectorySeparatorChar) || --n > 0)) i++;
while (i < length && (!IsDirectorySeparator(path[i]) || --n > 0)) i++;
}
}
else if (length >= 2 && path[1] == Path.VolumeSeparatorChar)
@@ -62,8 +69,7 @@ internal static void CheckSearchPattern(String searchPattern)
if (index + 2 == searchPattern.Length) // Terminal ".." . Files names cannot end in ".."
throw new ArgumentException(SR.Arg_InvalidSearchPattern);

if ((searchPattern[index + 2] == Path.DirectorySeparatorChar)
|| (searchPattern[index + 2] == Path.AltDirectorySeparatorChar))
if (IsDirectorySeparator(searchPattern[index + 2]))
throw new ArgumentException(SR.Arg_InvalidSearchPattern);

searchPattern = searchPattern.Substring(index + 2);
6 changes: 3 additions & 3 deletions src/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ internal static int FillAttributeInfo(String path, ref Interop.WIN32_FILE_ATTRIB
findData = new Interop.WIN32_FIND_DATA();

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

// For floppy drives, normally the OS will pop up a dialog saying
// 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
Interop.WIN32_FIND_DATA data = new Interop.WIN32_FIND_DATA();

// Open a Find handle
using (SafeFindHandle hnd = Interop.mincore.FindFirstFile(fullPath + Path.DirectorySeparatorChar + "*", ref data))
using (SafeFindHandle hnd = Interop.mincore.FindFirstFile(fullPath + PathHelpers.DirectorySeparatorCharAsString + "*", ref data))
{
if (hnd.IsInvalid)
throw Win32Marshal.GetExceptionForLastWin32Error(fullPath);
@@ -546,7 +546,7 @@ private static void RemoveDirectoryHelper(String fullPath, bool recursive, bool
if (data.dwReserved0 == Interop.IO_REPARSE_TAG_MOUNT_POINT)
{
// Use full path plus a trailing '\'
String mountPoint = Path.Combine(fullPath, data.cFileName + Path.DirectorySeparatorChar);
String mountPoint = Path.Combine(fullPath, data.cFileName + PathHelpers.DirectorySeparatorCharAsString);
r = Interop.mincore.DeleteVolumeMountPoint(mountPoint);
if (!r)
{
Original file line number Diff line number Diff line change
@@ -502,7 +502,7 @@ private static String GetFullSearchString(String fullPath, String searchPattern)
char lastChar = tempStr[tempStr.Length - 1];
if (PathHelpers.IsDirectorySeparator(lastChar) || lastChar == Path.VolumeSeparatorChar)
{
tempStr = tempStr + '*';
tempStr = tempStr + "*";
}

return tempStr;

0 comments on commit 03fbac5

Please sign in to comment.