Added StringHelper methods supporting encoders

pull/279/head
dahall 2022-01-22 15:27:13 -07:00
parent 0b3ac8d87a
commit 0f6fb75d3d
1 changed files with 25 additions and 10 deletions

View File

@ -139,10 +139,17 @@ namespace Vanara.Extensions
/// <param name="nullTerm">if set to <c>true</c> include a null terminator at the end of the string in the resulting byte array.</param>
/// <param name="charSet">The character set.</param>
/// <returns>A byte array including <paramref name="value"/> encoded as per <paramref name="charSet"/> and the optional null terminator.</returns>
public static byte[] GetBytes(this string value, bool nullTerm = true, CharSet charSet = CharSet.Auto)
public static byte[] GetBytes(this string value, bool nullTerm = true, CharSet charSet = CharSet.Auto) =>
GetBytes(value, GetCharSize(charSet) == 1 ? System.Text.Encoding.ASCII : System.Text.Encoding.Unicode, nullTerm);
/// <summary>Gets the encoded bytes for a string including an optional null terminator.</summary>
/// <param name="value">The string value to convert.</param>
/// <param name="enc">The character encoding.</param>
/// <param name="nullTerm">if set to <c>true</c> include a null terminator at the end of the string in the resulting byte array.</param>
/// <returns>A byte array including <paramref name="value"/> encoded as per <paramref name="enc"/> and the optional null terminator.</returns>
public static byte[] GetBytes(this string value, System.Text.Encoding enc, bool nullTerm = true)
{
var chSz = GetCharSize(charSet);
var enc = chSz == 1 ? System.Text.Encoding.ASCII : System.Text.Encoding.Unicode;
var chSz = GetCharSize(enc);
var ret = new byte[enc.GetByteCount(value) + (nullTerm ? chSz : 0)];
enc.GetBytes(value, 0, value.Length, ret, 0);
if (nullTerm)
@ -155,19 +162,27 @@ namespace Vanara.Extensions
/// <param name="nullTerm">if set to <c>true</c> include a null terminator at the end of the string in the count if <paramref name="value"/> does not equal <c>null</c>.</param>
/// <param name="charSet">The character set.</param>
/// <returns>The number of bytes required to store <paramref name="value"/>. Returns 0 if <paramref name="value"/> is <c>null</c>.</returns>
public static int GetByteCount(this string value, bool nullTerm = true, CharSet charSet = CharSet.Auto)
{
if (value == null) return 0;
var chSz = GetCharSize(charSet);
var enc = chSz == 1 ? System.Text.Encoding.ASCII : System.Text.Encoding.Unicode;
return enc.GetByteCount(value) + (nullTerm ? chSz : 0);
}
public static int GetByteCount(this string value, bool nullTerm = true, CharSet charSet = CharSet.Auto) =>
GetByteCount(value, GetCharSize(charSet) == 1 ? System.Text.Encoding.ASCII : System.Text.Encoding.Unicode);
/// <summary>Gets the number of bytes required to store the string.</summary>
/// <param name="value">The string value.</param>
/// <param name="enc">The character encoding.</param>
/// <param name="nullTerm">if set to <c>true</c> include a null terminator at the end of the string in the count if <paramref name="value"/> does not equal <c>null</c>.</param>
/// <returns>The number of bytes required to store <paramref name="value"/>. Returns 0 if <paramref name="value"/> is <c>null</c>.</returns>
public static int GetByteCount(this string value, System.Text.Encoding enc, bool nullTerm = true) =>
value is null ? 0 : enc.GetByteCount(value) + (nullTerm ? GetCharSize(enc) : 0);
/// <summary>Gets the size of a character defined by the supplied <see cref="CharSet"/>.</summary>
/// <param name="charSet">The character set to size.</param>
/// <returns>The size of a standard character, in bytes, from <paramref name="charSet"/>.</returns>
public static int GetCharSize(CharSet charSet = CharSet.Auto) => charSet == CharSet.Auto ? Marshal.SystemDefaultCharSize : (charSet == CharSet.Unicode ? 2 : 1);
/// <summary>Gets the size of a character defined by the supplied <see cref="System.Text.Encoding"/>.</summary>
/// <param name="enc">The character encoding type.</param>
/// <returns>The size of a standard character, in bytes, from <paramref name="enc"/>.</returns>
public static int GetCharSize(System.Text.Encoding enc) => enc.GetByteCount(new[] { '\0' });
/// <summary>
/// Allocates a managed String and copies all characters up to the first null character or the end of the allocated memory pool from a string stored in unmanaged memory into it.
/// </summary>