Changed ASCII encoder to UTF8 encoder in string helper classes for broader character support.

pull/363/head
David Hall 2023-01-02 14:38:38 -07:00
parent 8e9280e5aa
commit 3596f9999b
2 changed files with 5 additions and 5 deletions

View File

@ -69,7 +69,7 @@ namespace Vanara.Extensions
allocatedBytes = 0;
if (s == null) return IntPtr.Zero;
var chSz = GetCharSize(charSet);
var encoding = chSz == 2 ? Encoding.Unicode : Encoding.ASCII;
var encoding = chSz == 2 ? Encoding.Unicode : Encoding.UTF8;
var hMem = AllocSecureString(s, charSet);
var str = chSz == 2 ? Marshal.PtrToStringUni(hMem) : Marshal.PtrToStringAnsi(hMem);
Marshal.FreeCoTaskMem(hMem);
@ -143,7 +143,7 @@ namespace Vanara.Extensions
/// <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) =>
GetBytes(value, GetCharSize(charSet) == 1 ? Encoding.ASCII : Encoding.Unicode, nullTerm);
GetBytes(value, GetCharSize(charSet) == 1 ? Encoding.UTF8 : 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>
@ -166,7 +166,7 @@ namespace Vanara.Extensions
/// <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) =>
GetByteCount(value, GetCharSize(charSet) == 1 ? Encoding.ASCII : Encoding.Unicode, nullTerm);
GetByteCount(value, GetCharSize(charSet) == 1 ? Encoding.UTF8 : Encoding.Unicode, nullTerm);
/// <summary>Gets the number of bytes required to store the string.</summary>
/// <param name="value">The string value.</param>

View File

@ -88,13 +88,13 @@ public abstract class SafeMemString<TMem> : SafeMemoryHandle<TMem>, IConvertible
var cs = StringHelper.GetCharSize(CharSet);
return index * cs >= Capacity || index < 0
? throw new IndexOutOfRangeException()
: CharSet == CharSet.Ansi ? System.Text.Encoding.ASCII.GetChars(GetBytes(index * cs, cs))[0] : System.Text.Encoding.Unicode.GetChars(GetBytes(index * cs, cs))[0];
: CharSet == CharSet.Ansi ? System.Text.Encoding.UTF8.GetChars(GetBytes(index * cs, cs))[0] : System.Text.Encoding.Unicode.GetChars(GetBytes(index * cs, cs))[0];
}
set
{
var cs = StringHelper.GetCharSize(CharSet);
if (index * cs >= Capacity || index < 0) throw new IndexOutOfRangeException();
var bytes = CharSet == CharSet.Ansi ? System.Text.Encoding.ASCII.GetBytes(new[] { value }) : System.Text.Encoding.Unicode.GetBytes(new[] { value });
var bytes = CharSet == CharSet.Ansi ? System.Text.Encoding.UTF8.GetBytes(new[] { value }) : System.Text.Encoding.Unicode.GetBytes(new[] { value });
handle.Write(bytes, index * cs, Size);
}
}