2022-11-20 03:06:41 +01:00
|
|
|
using System.Reflection;
|
2022-11-26 15:51:29 +01:00
|
|
|
using System.ServiceModel.Syndication;
|
|
|
|
using System.Text;
|
|
|
|
using System.Xml;
|
2022-11-20 03:06:41 +01:00
|
|
|
|
2022-11-26 13:23:58 +01:00
|
|
|
namespace zotanpw.Backend;
|
2022-11-20 03:06:41 +01:00
|
|
|
|
|
|
|
public static class Utils {
|
|
|
|
public static readonly string Version =
|
|
|
|
((AssemblyInformationalVersionAttribute)Assembly.GetEntryAssembly()!.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)[0])
|
|
|
|
.InformationalVersion[6..];
|
|
|
|
|
|
|
|
public static readonly string LinkVersion = Version[..8];
|
|
|
|
|
|
|
|
// Works up to 79, doubt i'll have >=80 minute read time blog posts
|
|
|
|
public static string a_an(int number) {
|
|
|
|
if (number >= 80)
|
2022-12-01 16:40:00 +01:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(number));
|
2022-11-20 03:06:41 +01:00
|
|
|
|
|
|
|
return number switch {
|
|
|
|
8 => "an",
|
|
|
|
11 => "an",
|
|
|
|
18 => "an",
|
|
|
|
_ => "a"
|
|
|
|
};
|
|
|
|
}
|
2022-11-26 15:51:29 +01:00
|
|
|
|
|
|
|
public static string ToXmlString(this SyndicationFeedFormatter feed) {
|
|
|
|
using var sw = new StringWriterWithEncoding(Encoding.UTF8);
|
|
|
|
using (var xw = XmlWriter.Create(sw, new XmlWriterSettings { Encoding = Encoding.UTF8 }))
|
|
|
|
feed.WriteTo(xw);
|
|
|
|
|
|
|
|
return sw.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private sealed class StringWriterWithEncoding : StringWriter {
|
|
|
|
public StringWriterWithEncoding(Encoding encoding) => Encoding = encoding;
|
|
|
|
|
|
|
|
public override Encoding Encoding { get; }
|
|
|
|
}
|
2023-02-03 21:45:07 +01:00
|
|
|
|
|
|
|
public static string Truncate(this string input, int maxLen, string truncateMarker = "...") {
|
|
|
|
var markerLength = truncateMarker.Length;
|
|
|
|
if (maxLen < 1 + markerLength)
|
|
|
|
maxLen = 1 + markerLength;
|
|
|
|
return input.Length <= maxLen ? input : input[..(maxLen - markerLength)] + truncateMarker;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string GetUntil(this string input, string stopAt) {
|
|
|
|
if (!string.IsNullOrWhiteSpace(input)) {
|
|
|
|
if (!input.Contains(stopAt))
|
|
|
|
return input;
|
|
|
|
|
|
|
|
var charLocation = input.IndexOf(stopAt, StringComparison.Ordinal);
|
|
|
|
return input[..++charLocation];
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
}
|
2022-11-26 15:51:29 +01:00
|
|
|
}
|