small improvements

This commit is contained in:
Timerix22 2021-11-13 22:50:30 +03:00
parent 861346456c
commit 5e8f9d8485
2 changed files with 35 additions and 5 deletions

View File

@ -7,12 +7,8 @@ namespace DTLib
// вывод лога в консоль и файл
public class DefaultLogger
{
public DefaultLogger() { }
public DefaultLogger(string logfile) => Logfile = logfile;
public DefaultLogger(string dir, string programName) => SetLogfile(dir, programName);
public void SetLogfile(string dir, string programName)
=> Logfile = $"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_');
public DefaultLogger(string dir, string programName) => Logfile = $"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_');
public string Logfile { get; set; }

View File

@ -39,6 +39,9 @@ namespace DTLib
return true;
}
public static bool StartsWith(this string s, char c) => s[0] == c;
public static bool EndsWith(this string s, char c) => s[s.Length-1] == c;
// Math.Truncate принимает как decimal, так и doublе,
// из-за чего вызов метода так: Math.Truncate(10/3) выдаст ошибку "неоднозначный вызов"
public static int Truncate<T>(this T number) => Math.Truncate(number.ToDouble()).ToInt();
@ -190,6 +193,37 @@ namespace DTLib
return o;
}
// правильно реагирует на кавычки
public static List<string> SplitToList(this string s, char c, char quot)
{
List<string> output = new();
var list = s.SplitToList(c);
bool q_open = false;
for (int i = 0; i < list.Count; i++)
{
var _s = list[i];
if (q_open)
{
if (_s.EndsWith(quot))
{
q_open = false;
_s=_s.Remove(_s.Length - 1);
}
output[output.Count - 1] += c + _s;
}
else
{
if (_s.StartsWith(quot))
{
q_open = true;
_s = _s.Remove(0, 1);
}
output.Add(_s);
}
}
return output;
}
// разбивает на части указанной длины
public static List<string> Split(this string s, int length)
{