Created
July 20, 2016 03:51
-
-
Save simpleprogrammer-shared/372ed51cc310d20ead580ac702be5942 to your computer and use it in GitHub Desktop.
Why Comments Are Stupid, a Real Example 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DirectoryFileSplitter | |
{ | |
private readonly string validatedFullPath; | |
private int length; | |
private int rootLength; | |
private bool pivotFound; | |
public string Directory { get; set; } | |
public string File { get; set; } | |
public DirectoryFileSplitter(string validatedFullPath) | |
{ | |
this.validatedFullPath = validatedFullPath; | |
length = validatedFullPath.Length; | |
rootLength = GetRootLength(validatedFullPath); | |
} | |
public void Split() | |
{ | |
if (validatedFullPath != null) | |
{ | |
IgnoreTrailingSlash(); | |
FindPivotIndexBetweenEndOfStringAndRoot(); | |
// no pivot, return just the trimmed directory | |
if(!pivotFound) | |
Directory = validatedFullPath.Substring(0, length); | |
} | |
return; | |
} | |
private void FindPivotIndexBetweenEndOfStringAndRoot() | |
{ | |
for (int pivot = length - 1; pivot >= rootLength; pivot--) | |
{ | |
if (IsDirectorySeparator(validatedFullPath[pivot])) | |
{ | |
Directory = validatedFullPath.Substring(0, pivot); | |
File = validatedFullPath.Substring(pivot + 1, length - pivot - 1); | |
pivotFound = true; | |
} | |
} | |
} | |
private void IgnoreTrailingSlash() | |
{ | |
if (length > rootLength && EndsInDirectorySeparator(validatedFullPath)) | |
length--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment