/// <summary> /// Normalize the given path. /// </summary> /// <remarks> /// Normalizes via Win32 GetFullPathName(). /// </remarks> /// <param name="path">Path to normalize</param> /// <exception cref="PathTooLongException">Thrown if we have a string that is too large to fit into a UNICODE_STRING.</exception> /// <exception cref="IOException">Thrown if the path is empty.</exception> /// <returns>Normalized path</returns> internal static string Normalize(string path) { // Get the full path StringBuffer fullPath = new StringBuffer(path.Length); try { GetFullPathName(path, ref fullPath); if (fullPath.Contains('~')) { return(TryExpandShortFileName(ref fullPath, originalPath: path)); } else { if (fullPath.Length == path.Length && fullPath.StartsWith(path)) { // If we have the exact same string we were passed in, don't bother to allocate another string from the StringBuffer. return(path); } return(fullPath.ToString()); } } finally { // Clear the buffer fullPath.Free(); } }
public void ContainsParamsTests(string content, char[] values, bool expected) { using (var buffer = new StringBuffer(content)) { buffer.Contains(values).Should().Be(expected); } }
/*===============================CurrentDirectory=============================== **Action: Provides a getter and setter for the current directory. The original ** current DirectoryInfo is the one from which the process was started. **Returns: The current DirectoryInfo (from the getter). Void from the setter. **Arguments: The current DirectoryInfo to which to switch to the setter. **Exceptions: ** ==============================================================================*/ public static String GetCurrentDirectory() { // Start with a buffer the size of MAX_PATH using (StringBuffer buffer = new StringBuffer(260)) { uint result = 0; while ((result = Win32Native.GetCurrentDirectoryW(buffer.CharCapacity, buffer.GetHandle())) > buffer.CharCapacity) { // Reported size is greater than the buffer size. Increase the capacity. // The size returned includes the null only if more space is needed (this case). buffer.EnsureCharCapacity(result); } if (result == 0) { __Error.WinIOError(); } buffer.Length = result; #if !PLATFORM_UNIX if (buffer.Contains('~')) { return(Path.GetFullPath(buffer.ToString())); } #endif return(buffer.ToString()); } }
internal static string EscapeSpecialCharacters(string unescapedValue) { if (string.IsNullOrEmpty(unescapedValue)) { return(string.Empty); } var original = new StringBuffer(unescapedValue); var escaped = new StringBuffer(); if (original.Contains(' ')) { escaped.PushToEnd(SpChar_LiteralStringBlock); escaped.TransferToEnd(original); escaped.PushToEnd(SpChar_LiteralStringBlock); } else { while (!original.IsEmpty) { switch (original.PeekStart()) { case SpChar_KeyDelimiter: case SpChar_ValueDelimiter: case SpChar_BeginComplexValue: case SpChar_FinishComplexValue: case SpChar_ListItemSplitter: case SpChar_CommentBlock: case SpChar_EscapeChar: escaped.PushToEnd(SpChar_EscapeChar); break; default: break; } escaped.PushToEnd(original.PopFromStart()); } } return(escaped.ToString()); }
/*===============================CurrentDirectory=============================== **Action: Provides a getter and setter for the current directory. The original ** current DirectoryInfo is the one from which the process was started. **Returns: The current DirectoryInfo (from the getter). Void from the setter. **Arguments: The current DirectoryInfo to which to switch to the setter. **Exceptions: ** ==============================================================================*/ public static String GetCurrentDirectory() { // Start with a buffer the size of MAX_PATH StringBuffer buffer = new StringBuffer(260); try { uint result = 0; while ((result = Win32Native.GetCurrentDirectoryW((uint)buffer.Capacity, buffer.UnderlyingArray)) > buffer.Capacity) { // Reported size is greater than the buffer size. Increase the capacity. // The size returned includes the null only if more space is needed (this case). buffer.EnsureCapacity(checked ((int)result)); } if (result == 0) { __Error.WinIOError(); } buffer.Length = (int)result; #if PLATFORM_WINDOWS if (buffer.Contains('~')) { return(Path.GetFullPath(buffer.ToString())); } #endif // PLATFORM_WINDOWS return(buffer.ToString()); } finally { buffer.Free(); } }
public void ContainsTests(string content, char value, bool expected) { using var buffer = new StringBuffer(content); buffer.Contains(value).Should().Be(expected); }