mirror of https://github.com/dahall/Vanara.git
Added StringHelper methods supporting encoders
parent
0b3ac8d87a
commit
0f6fb75d3d
|
@ -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="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>
|
/// <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>
|
/// <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 chSz = GetCharSize(enc);
|
||||||
var enc = chSz == 1 ? System.Text.Encoding.ASCII : System.Text.Encoding.Unicode;
|
|
||||||
var ret = new byte[enc.GetByteCount(value) + (nullTerm ? chSz : 0)];
|
var ret = new byte[enc.GetByteCount(value) + (nullTerm ? chSz : 0)];
|
||||||
enc.GetBytes(value, 0, value.Length, ret, 0);
|
enc.GetBytes(value, 0, value.Length, ret, 0);
|
||||||
if (nullTerm)
|
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="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>
|
/// <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>
|
/// <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)
|
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);
|
||||||
if (value == null) return 0;
|
|
||||||
var chSz = GetCharSize(charSet);
|
/// <summary>Gets the number of bytes required to store the string.</summary>
|
||||||
var enc = chSz == 1 ? System.Text.Encoding.ASCII : System.Text.Encoding.Unicode;
|
/// <param name="value">The string value.</param>
|
||||||
return enc.GetByteCount(value) + (nullTerm ? chSz : 0);
|
/// <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>
|
/// <summary>Gets the size of a character defined by the supplied <see cref="CharSet"/>.</summary>
|
||||||
/// <param name="charSet">The character set to size.</param>
|
/// <param name="charSet">The character set to size.</param>
|
||||||
/// <returns>The size of a standard character, in bytes, from <paramref name="charSet"/>.</returns>
|
/// <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);
|
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>
|
/// <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.
|
/// 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>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue