mirror of
https://github.com/BeyondDimension/SteamTools.git
synced 2026-10-26 03:59:34 +08:00
V1.2 新构架
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
namespace System.Text
|
||||
{
|
||||
public static partial class Base64Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Base64编码(ByteArray → String)
|
||||
/// </summary>
|
||||
/// <param name="inArray"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64Encode(this byte[] inArray, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
ReadOnlySpan<byte> inArray_ = inArray;
|
||||
return Base64Encode(inArray_, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64编码(ByteArray → String)
|
||||
/// </summary>
|
||||
/// <param name="inArray"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string? Base64Encode_Nullable(this byte[]? inArray, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
if (inArray == default) return default;
|
||||
return Base64Encode(inArray, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64编码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64Encode(this string s, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
var inArray = Encoding.UTF8.GetBytes(s);
|
||||
return Base64Encode(inArray, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64编码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string? Base64Encode_Nullable(this string? s, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
if (s == default) return default;
|
||||
return Base64Encode(s, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码(String → ByteArray)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[]? Base64DecodeToByteArray_Nullable(this string? s, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
if (s == default) return default;
|
||||
return Base64DecodeToByteArray(s, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64Decode(this string s, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
var bytes = Base64DecodeToByteArray(s, options);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string? Base64Decode_Nullable(this string? s, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
if (s == default) return default;
|
||||
return Base64Decode(s, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Base64 = gfoidl.Base64.Base64;
|
||||
|
||||
// 使用 https://github.com/gfoidl/Base64 实现
|
||||
// 与 2019年版本 比较
|
||||
// Base64URL 不兼容
|
||||
// Base64URLConfuse 不支持
|
||||
|
||||
namespace System.Text
|
||||
{
|
||||
partial class Base64Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Base64编码(ReadOnlySpan<Byte> → String)
|
||||
/// </summary>
|
||||
/// <param name="inArray"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64Encode(this ReadOnlySpan<byte> inArray, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
switch (options)
|
||||
{
|
||||
case Base64FormattingOptions.InsertLineBreaks:
|
||||
return Convert.ToBase64String(inArray, Base64FormattingOptions.InsertLineBreaks); ;
|
||||
case Base64FormattingOptions.None:
|
||||
return Base64.Default.Encode(inArray);
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(options));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码(ReadOnlySpan<Char> → ByteArray)
|
||||
/// </summary>
|
||||
/// <param name="encoded"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] Base64DecodeToByteArray(this ReadOnlySpan<char> encoded) => Base64.Default.Decode(encoded);
|
||||
|
||||
/// <summary>
|
||||
/// Base64解码(String → ByteArray)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] Base64DecodeToByteArray(this string s, Base64FormattingOptions options = Base64FormattingOptions.None)
|
||||
{
|
||||
switch (options)
|
||||
{
|
||||
case Base64FormattingOptions.InsertLineBreaks:
|
||||
return Convert.FromBase64String(s);
|
||||
|
||||
case Base64FormattingOptions.None:
|
||||
var encoded = s.AsSpan();
|
||||
return Base64DecodeToByteArray(encoded);
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(options));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
partial class Base64UrlExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Base64Url编码(ReadOnlySpan<Byte> → String)
|
||||
/// </summary>
|
||||
/// <param name="inArray"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64UrlEncode(this ReadOnlySpan<byte> inArray) => Base64.Url.Encode(inArray);
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url解码(ReadOnlySpan<Char> → ByteArray)
|
||||
/// </summary>
|
||||
/// <param name="encoded"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] Base64UrlDecodeToByteArray(this ReadOnlySpan<char> encoded) => Base64.Url.Decode(encoded);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
namespace System.Text
|
||||
{
|
||||
public static partial class Base64UrlExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Base64Url编码(ByteArray → String)
|
||||
/// </summary>
|
||||
/// <param name="inArray"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64UrlEncode(this byte[] inArray)
|
||||
{
|
||||
ReadOnlySpan<byte> inArray_ = inArray;
|
||||
return Base64UrlEncode(inArray_);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url编码(ByteArray → String)
|
||||
/// </summary>
|
||||
/// <param name="inArray"></param>
|
||||
/// <returns></returns>
|
||||
public static string? Base64UrlEncode_Nullable(this byte[]? inArray)
|
||||
{
|
||||
if (inArray == default) return default;
|
||||
return Base64UrlEncode(inArray);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url编码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64UrlEncode(this string s)
|
||||
{
|
||||
var inArray = Encoding.UTF8.GetBytes(s);
|
||||
return Base64UrlEncode(inArray);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url编码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static string? Base64UrlEncode_Nullable(this string? s)
|
||||
{
|
||||
if (s == default) return default;
|
||||
return Base64UrlEncode(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url解码(String → ByteArray)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] Base64UrlDecodeToByteArray(this string s)
|
||||
{
|
||||
var encoded = s.AsSpan();
|
||||
return Base64UrlDecodeToByteArray(encoded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url解码(String → ByteArray)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[]? Base64UrlDecodeToByteArray_Nullable(this string? s)
|
||||
{
|
||||
if (s == default) return default;
|
||||
return Base64UrlDecodeToByteArray(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url解码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static string Base64UrlDecode(this string s)
|
||||
{
|
||||
var bytes = Base64UrlDecodeToByteArray(s);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64Url解码(String → String)
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static string? Base64UrlDecode_Nullable(this string? s)
|
||||
{
|
||||
if (s == default) return default;
|
||||
return Base64UrlDecode(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace System.Text
|
||||
{
|
||||
public interface IStringBuilder
|
||||
{
|
||||
IStringBuilder Append(string? value);
|
||||
|
||||
IStringBuilder AppendLine();
|
||||
|
||||
IStringBuilder AppendLine(string? value);
|
||||
|
||||
IStringBuilder AppendFormat(string format, params object?[] args);
|
||||
|
||||
IStringBuilder AppendFormatLine(string format, params object?[] args);
|
||||
|
||||
IStringBuilder AppendDateTimeNowLine(DateTime? now = null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace System.Text
|
||||
{
|
||||
public abstract class StringBuilderBase : IStringBuilder
|
||||
{
|
||||
public abstract IStringBuilder Append(string? value);
|
||||
|
||||
public virtual IStringBuilder AppendLine()
|
||||
{
|
||||
Append(Environment.NewLine);
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual IStringBuilder AppendLine(string? value)
|
||||
{
|
||||
Append(value);
|
||||
AppendLine();
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual IStringBuilder AppendFormat(string format, params object?[] args)
|
||||
{
|
||||
Append(string.Format(format, args));
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual IStringBuilder AppendFormatLine(string format, params object?[] args)
|
||||
{
|
||||
StringBuilderExtensions.AppendFormatLine((f, a) => AppendFormat(f, a), () => AppendLine(), format, args);
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual IStringBuilder AppendDateTimeNowLine(DateTime? now = null)
|
||||
{
|
||||
StringBuilderExtensions.AppendDateTimeNowLine((format, args) => AppendFormatLine(format, args), now);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace System.Text
|
||||
{
|
||||
public static class StringBuilderExtensions
|
||||
{
|
||||
internal static void AppendFormatLine(
|
||||
Action<string, object?[]> appendFormat,
|
||||
Action appendLine,
|
||||
string format,
|
||||
params object?[] args)
|
||||
{
|
||||
appendFormat(format, args);
|
||||
appendLine();
|
||||
}
|
||||
|
||||
internal static void AppendDateTimeNowLine(Action<string, object?[]> appendFormatLine, DateTime? now = null)
|
||||
{
|
||||
now ??= DateTime.Now;
|
||||
AppendFormatLine("当前时间:{0}", now.Value.ToString(DateTimeFormat.Complete));
|
||||
void AppendFormatLine(string format, params object[] args) => appendFormatLine(format, args);
|
||||
}
|
||||
|
||||
public static StringBuilder AppendFormatLine(this StringBuilder sb, string format, params object?[] args)
|
||||
{
|
||||
AppendFormatLine((f, a) => sb.AppendFormat(f, a), () => sb.AppendLine(), format, args);
|
||||
return sb;
|
||||
}
|
||||
|
||||
public static StringBuilder AppendDateTimeNowLine(this StringBuilder sb, DateTime? now = null)
|
||||
{
|
||||
AppendDateTimeNowLine((format, args) => sb.AppendFormatLine(format, args), now);
|
||||
return sb;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user