v2rayN/v2rayN/ServiceLib/Common/Extension.cs

122 lines
3.1 KiB
C#
Raw Normal View History

2025-01-30 09:10:05 +00:00
using System.Diagnostics.CodeAnalysis;
2019-10-11 06:15:20 +00:00
namespace ServiceLib.Common;
2025-07-20 06:16:19 +00:00
public static class Extension
2019-10-11 06:15:20 +00:00
{
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? value)
2019-10-11 06:15:20 +00:00
{
return string.IsNullOrWhiteSpace(value) || string.IsNullOrEmpty(value);
}
2019-10-11 06:15:20 +00:00
public static bool IsNotEmpty([NotNullWhen(false)] this string? value)
{
return !string.IsNullOrWhiteSpace(value);
}
2019-10-11 06:15:20 +00:00
public static string? NullIfEmpty(this string? value)
{
return string.IsNullOrWhiteSpace(value) ? null : value;
}
public static bool BeginWithAny(this string s, IEnumerable<char> chars)
{
if (s.IsNullOrEmpty())
2019-10-11 06:15:20 +00:00
{
return false;
2019-10-11 06:15:20 +00:00
}
return chars.Contains(s.First());
}
2019-10-11 06:15:20 +00:00
private static bool IsWhiteSpace(this string value)
{
return value.All(char.IsWhiteSpace);
}
2019-10-11 06:15:20 +00:00
public static IEnumerable<string> NonWhiteSpaceLines(this TextReader reader)
{
while (reader.ReadLine() is { } line)
2019-10-11 06:15:20 +00:00
{
if (line.IsWhiteSpace())
2019-10-11 06:15:20 +00:00
{
continue;
2019-10-11 06:15:20 +00:00
}
yield return line;
2019-10-11 06:15:20 +00:00
}
}
2019-10-11 06:15:20 +00:00
public static string TrimEx(this string? value)
{
return value == null ? string.Empty : value.Trim();
}
public static string RemovePrefix(this string value, char prefix)
{
return value.StartsWith(prefix) ? value[1..] : value;
}
public static string RemovePrefix(this string value, string prefix)
{
return value.StartsWith(prefix) ? value[prefix.Length..] : value;
}
2023-06-18 01:42:45 +00:00
public static string UpperFirstChar(this string value)
{
if (string.IsNullOrEmpty(value))
2023-06-18 01:42:45 +00:00
{
return string.Empty;
2023-06-18 01:42:45 +00:00
}
2024-01-27 10:29:17 +00:00
return char.ToUpper(value.First()) + value[1..];
}
public static string AppendQuotes(this string value)
{
return string.IsNullOrEmpty(value) ? string.Empty : $"\"{value}\"";
}
public static int ToInt(this string? value, int defaultValue = 0)
{
return int.TryParse(value, out var result) ? result : defaultValue;
2019-10-11 06:15:20 +00:00
}
2025-07-20 06:16:19 +00:00
public static List<string> AppendEmpty(this IEnumerable<string> source)
{
return source.Concat(new[] { string.Empty }).ToList();
}
public static bool IsGroupType(this EConfigType configType)
{
return configType is EConfigType.PolicyGroup or EConfigType.ProxyChain;
}
public static bool IsComplexType(this EConfigType configType)
{
return configType is EConfigType.Custom or EConfigType.PolicyGroup or EConfigType.ProxyChain;
}
/// <summary>
/// Safely adds elements from a collection to the list. Does nothing if the source is null.
/// </summary>
public static void AddRangeSafe<T>(this ICollection<T> destination, IEnumerable<T>? source)
{
ArgumentNullException.ThrowIfNull(destination);
if (source is null)
{
return;
}
if (destination is List<T> list)
{
list.AddRange(source);
return;
}
foreach (var item in source)
{
destination.Add(item);
}
}
2025-01-30 09:10:05 +00:00
}