From 0f6fb75d3d5c0060b8631f4165851cfa398db0e3 Mon Sep 17 00:00:00 2001 From: dahall Date: Sat, 22 Jan 2022 15:27:13 -0700 Subject: [PATCH] Added StringHelper methods supporting encoders --- Core/Extensions/StringHelper.cs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/Core/Extensions/StringHelper.cs b/Core/Extensions/StringHelper.cs index d1273c07..9e0c2335 100644 --- a/Core/Extensions/StringHelper.cs +++ b/Core/Extensions/StringHelper.cs @@ -139,10 +139,17 @@ namespace Vanara.Extensions /// if set to true include a null terminator at the end of the string in the resulting byte array. /// The character set. /// A byte array including encoded as per and the optional null terminator. - 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); + + /// Gets the encoded bytes for a string including an optional null terminator. + /// The string value to convert. + /// The character encoding. + /// if set to true include a null terminator at the end of the string in the resulting byte array. + /// A byte array including encoded as per and the optional null terminator. + 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 /// if set to true include a null terminator at the end of the string in the count if does not equal null. /// The character set. /// The number of bytes required to store . Returns 0 if is null. - 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); + + /// Gets the number of bytes required to store the string. + /// The string value. + /// The character encoding. + /// if set to true include a null terminator at the end of the string in the count if does not equal null. + /// The number of bytes required to store . Returns 0 if is null. + 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); /// Gets the size of a character defined by the supplied . /// The character set to size. /// The size of a standard character, in bytes, from . public static int GetCharSize(CharSet charSet = CharSet.Auto) => charSet == CharSet.Auto ? Marshal.SystemDefaultCharSize : (charSet == CharSet.Unicode ? 2 : 1); + /// Gets the size of a character defined by the supplied . + /// The character encoding type. + /// The size of a standard character, in bytes, from . + public static int GetCharSize(System.Text.Encoding enc) => enc.GetByteCount(new[] { '\0' }); + /// /// 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. ///