| |
| |
|
|
| using System.Collections.Generic; |
| using System.Globalization; |
| using System.Management.Automation.Host; |
| using System.Text; |
| using System.Text.RegularExpressions; |
| using System.Threading; |
|
|
| using Dbg = System.Management.Automation.Diagnostics; |
|
|
| namespace System.Management.Automation.Internal |
| { |
| internal static class StringUtil |
| { |
| internal static string Format(string format, object arg0) |
| => string.Format(CultureInfo.CurrentCulture, format, arg0); |
|
|
| internal static string Format(string format, object arg0, object arg1) |
| => string.Format(CultureInfo.CurrentCulture, format, arg0, arg1); |
|
|
| internal static string Format(string format, object arg0, object arg1, object arg2) |
| => string.Format(CultureInfo.CurrentCulture, format, arg0, arg1, arg2); |
|
|
| internal static string Format(string format, params object[] args) |
| => string.Format(CultureInfo.CurrentCulture, format, args); |
|
|
| internal static string TruncateToBufferCellWidth(PSHostRawUserInterface rawUI, string toTruncate, int maxWidthInBufferCells) |
| { |
| Dbg.Assert(rawUI != null, "need a reference"); |
| Dbg.Assert(maxWidthInBufferCells >= 0, "maxWidthInBufferCells must be positive"); |
|
|
| string result; |
| int i = Math.Min(toTruncate.Length, maxWidthInBufferCells); |
|
|
| while (true) |
| { |
| result = toTruncate.Substring(0, i); |
| int cellCount = rawUI.LengthInBufferCells(result); |
| if (cellCount <= maxWidthInBufferCells) |
| { |
| |
|
|
| break; |
| } |
| else |
| { |
| |
| |
| |
| --i; |
| } |
| } |
|
|
| return result; |
| } |
|
|
| |
| private const int IndentCacheMax = 120; |
|
|
| private static readonly string[] s_indentCache = new string[IndentCacheMax]; |
|
|
| internal static string Padding(int countOfSpaces) |
| { |
| if (countOfSpaces >= IndentCacheMax) |
| return new string(' ', countOfSpaces); |
|
|
| var result = s_indentCache[countOfSpaces]; |
|
|
| if (result == null) |
| { |
| Interlocked.CompareExchange(ref s_indentCache[countOfSpaces], new string(' ', countOfSpaces), null); |
| result = s_indentCache[countOfSpaces]; |
| } |
|
|
| return result; |
| } |
|
|
| private const int DashCacheMax = 120; |
|
|
| private static readonly string[] s_dashCache = new string[DashCacheMax]; |
|
|
| internal static string DashPadding(int count) |
| { |
| if (count >= DashCacheMax) |
| return new string('-', count); |
|
|
| var result = s_dashCache[count]; |
|
|
| if (result == null) |
| { |
| Interlocked.CompareExchange(ref s_dashCache[count], new string('-', count), null); |
| result = s_dashCache[count]; |
| } |
|
|
| return result; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static string VtSubstring(this string str, int startOffset) |
| { |
| return VtSubstring(str, startOffset, int.MaxValue, prependStr: null, appendStr: null); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static string VtSubstring(this string str, int startOffset, int length) |
| { |
| return VtSubstring(str, startOffset, length, prependStr: null, appendStr: null); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static string VtSubstring(this string str, int startOffset, string prependStr, string appendStr) |
| { |
| return VtSubstring(str, startOffset, int.MaxValue, prependStr, appendStr); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static string VtSubstring(this string str, int startOffset, int length, string prependStr, string appendStr) |
| { |
| var valueStrDec = new ValueStringDecorated(str); |
| if (valueStrDec.IsDecorated) |
| { |
| |
| bool copyStarted = startOffset == 0; |
| bool hasEscSeqs = false; |
| bool firstNonEscChar = true; |
| StringBuilder sb = new(capacity: str.Length); |
| Dictionary<int, int> vtRanges = valueStrDec.EscapeSequenceRanges; |
|
|
| for (int i = 0, offset = 0; i < str.Length; i++) |
| { |
| |
| if (vtRanges.TryGetValue(i, out int len)) |
| { |
| hasEscSeqs = true; |
| sb.Append(str.AsSpan(i, len)); |
|
|
| i += len - 1; |
| continue; |
| } |
|
|
| |
| if (copyStarted) |
| { |
| if (firstNonEscChar) |
| { |
| |
| sb.Append(prependStr); |
| firstNonEscChar = false; |
| } |
|
|
| |
| sb.Append(str[i]); |
|
|
| |
| offset++; |
| } |
| else if (++offset == startOffset) |
| { |
| |
| copyStarted = true; |
|
|
| |
| offset = 0; |
| continue; |
| } |
|
|
| |
| if (copyStarted && offset == length) |
| { |
| break; |
| } |
| } |
|
|
| if (hasEscSeqs) |
| { |
| string resetStr = PSStyle.Instance.Reset; |
| bool endsWithReset = sb.EndsWith(resetStr); |
| if (endsWithReset) |
| { |
| |
| sb.Insert(sb.Length - resetStr.Length, appendStr); |
| } |
| else |
| { |
| |
| sb.Append(appendStr).Append(resetStr); |
| } |
| } |
| else |
| { |
| sb.Append(appendStr); |
| } |
|
|
| return sb.ToString(); |
| } |
|
|
| |
| if (length == int.MaxValue) |
| { |
| length = str.Length - startOffset; |
| } |
|
|
| if (prependStr is null && appendStr is null) |
| { |
| return str.Substring(startOffset, length); |
| } |
| else |
| { |
| int capacity = length + prependStr?.Length ?? 0 + appendStr?.Length ?? 0; |
| return new StringBuilder(prependStr, capacity) |
| .Append(str, startOffset, length) |
| .Append(appendStr) |
| .ToString(); |
| } |
| } |
|
|
| internal static bool EndsWith(this StringBuilder sb, string value) |
| { |
| if (sb.Length < value.Length) |
| { |
| return false; |
| } |
|
|
| int offset = sb.Length - value.Length; |
| for (int i = 0; i < value.Length; i++) |
| { |
| if (sb[offset + i] != value[i]) |
| { |
| return false; |
| } |
| } |
|
|
| return true; |
| } |
| } |
| } |
|
|