using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Cryville.Common {
///
/// Provides a set of methods related to string operations.
///
public static class StringUtils {
///
/// Removes the extension in a file name or file path.
///
/// The file name or file path.
/// The file name or file path with the extension removed.
public static string TrimExt(string s) {
return s[..s.LastIndexOf(".")];
}
///
/// Converts the value of a to a human-readable string.
///
/// The time span.
/// The digit count for seconds.
/// A human-readable string representing the time span.
public static string ToString(this TimeSpan timeSpan, int digits) {
var b = new StringBuilder();
bool flag = false;
if (timeSpan.TotalDays >= 1) {
flag = true;
b.Append(timeSpan.Days.ToString() + ":");
}
if (flag || timeSpan.TotalHours >= 1)
b.Append((timeSpan.Hours % 24).ToString() + ":");
b.Append((timeSpan.Minutes % 60).ToString("00") + ":");
b.Append((timeSpan.TotalSeconds % 60).ToString("00." + new string('0', digits)));
return b.ToString();
}
///
/// Escapes special characters in a file name.
///
/// The file name excluding the extension.
/// The escaped file name.
public static string EscapeFileName(string name) {
var result = Regex.Replace(name, @"[\/\\\<\>\:\x22\|\?\*\p{Cc}]", "_").TrimEnd(' ', '.');
if (result.Length == 0) return "_";
return result;
}
///
/// Gets the process path from a command.
///
/// The command.
/// The process path.
public static string GetProcessPathFromCommand(string command) {
command = command.Trim();
if (command[0] == '"') {
return command[1..command.IndexOf('"', 1)];
}
else {
int e = command.IndexOf(' ');
if (e == -1) return command;
else return command[..e];
}
}
}
}