mirror of https://github.com/dahall/Vanara.git
Added ability to get CharSet and resize by setting Capacity property.
parent
45ea5993c6
commit
15ffe13254
|
@ -31,9 +31,9 @@ namespace Vanara.InteropServices
|
|||
}
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="SafeCoTaskMemString"/> class.</summary>
|
||||
/// <param name="charLen">The size of the buffer in characters, including the null character terminator.</param>
|
||||
/// <param name="capacity">The size of the buffer in characters, including the null character terminator.</param>
|
||||
/// <param name="charSet">The character set.</param>
|
||||
public SafeCoTaskMemString(int charLen, CharSet charSet = CharSet.Unicode) : base(charLen, charSet)
|
||||
public SafeCoTaskMemString(int capacity, CharSet charSet = CharSet.Unicode) : base(capacity, charSet)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -41,12 +41,14 @@ namespace Vanara.InteropServices
|
|||
/// <param name="ptr">The PTR.</param>
|
||||
/// <param name="charSet">The character set.</param>
|
||||
/// <param name="ownsHandle"><c>true</c> to reliably release the handle during finalization; <c>false</c> to prevent it.</param>
|
||||
/// <param name="allocatedBytes">The number of bytes allocated to <paramref name="ptr"/>.</param>
|
||||
[ExcludeFromCodeCoverage]
|
||||
private SafeCoTaskMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true) : base(ptr, charSet, ownsHandle)
|
||||
private SafeCoTaskMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) :
|
||||
base(ptr, charSet, ownsHandle, allocatedBytes)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Represents a <c>null</c> value. Used primarily for comparrison.</summary>
|
||||
/// <summary>Represents a <c>null</c> value. Used primarily for comparison.</summary>
|
||||
/// <value>A null value.</value>
|
||||
public static SafeCoTaskMemString Null => new SafeCoTaskMemString(IntPtr.Zero, CharSet.Unicode, false);
|
||||
}
|
||||
|
|
|
@ -11,8 +11,6 @@ namespace Vanara.InteropServices
|
|||
/// <seealso cref="Vanara.InteropServices.SafeMemoryHandle{TMem}"/>
|
||||
public abstract class SafeMemString<TMem> : SafeMemoryHandle<TMem> where TMem : IMemoryMethods, new()
|
||||
{
|
||||
private readonly CharSet chSet = CharSet.Unicode;
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="SafeMemString{TMem}"/> class.</summary>
|
||||
/// <param name="s">The string value.</param>
|
||||
/// <param name="charSet">The character set.</param>
|
||||
|
@ -43,26 +41,37 @@ namespace Vanara.InteropServices
|
|||
/// <param name="charSet">The character set.</param>
|
||||
protected SafeMemString(int charLen, CharSet charSet = CharSet.Unicode) : base(charLen * StringHelper.GetCharSize(charSet))
|
||||
{
|
||||
chSet = charSet;
|
||||
CharSet = charSet;
|
||||
}
|
||||
|
||||
/// <summary>Prevents a default instance of the <see cref="SafeMemString{TMem}"/> class from being created.</summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
protected SafeMemString() : base(0) { }
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="SafeMemString{TMem}"/> class.</summary>
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SafeMemString{TMem}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="ptr">The PTR.</param>
|
||||
/// <param name="charSet">The character set.</param>
|
||||
/// <param name="ownsHandle"><c>true</c> to reliably release the handle during finalization; <c>false</c> to prevent it.</param>
|
||||
/// <param name="allocatedBytes">The number of bytes allocated to <paramref name="ptr"/>.</param>
|
||||
[ExcludeFromCodeCoverage]
|
||||
protected SafeMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true) : base(ptr, 0, ownsHandle)
|
||||
protected SafeMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) : base(ptr, allocatedBytes, ownsHandle)
|
||||
{
|
||||
chSet = charSet;
|
||||
CharSet = charSet;
|
||||
}
|
||||
|
||||
/// <summary>Gets the number of allocated characters or 0 if the size is unknown (for example if it is holding a <see cref="SecureString"/>.</summary>
|
||||
/// <value>The number of allocated characters.</value>
|
||||
public int Capacity => Size / StringHelper.GetCharSize(chSet);
|
||||
public int Capacity
|
||||
{
|
||||
get => Size / StringHelper.GetCharSize(CharSet);
|
||||
set => Size = value * StringHelper.GetCharSize(CharSet);
|
||||
}
|
||||
|
||||
/// <summary>Gets the character set of the assigned string.</summary>
|
||||
/// <value>The character set.</value>
|
||||
public CharSet CharSet { get; private set; } = CharSet.Unicode;
|
||||
|
||||
/// <summary>Gets the number of characters in the current <see cref="SafeMemString{TMem}"/> object.</summary>
|
||||
public int Length => ToString().Length;
|
||||
|
@ -73,7 +82,7 @@ namespace Vanara.InteropServices
|
|||
/// An <see cref="IntPtr"/> representing the value of the handle field. If the handle has been marked invalid with
|
||||
/// <see cref="SafeHandle.SetHandleAsInvalid"/>, this method still returns the original handle value, which can be a stale value.
|
||||
/// </returns>
|
||||
public static explicit operator IntPtr(SafeMemString<TMem> s) => s.DangerousGetHandle();
|
||||
public static implicit operator IntPtr(SafeMemString<TMem> s) => s.DangerousGetHandle();
|
||||
|
||||
/// <summary>Returns the string value held by a <see cref="SafeMemString{TMem}"/>.</summary>
|
||||
/// <param name="s">The <see cref="SafeMemString{TMem}"/> instance.</param>
|
||||
|
@ -82,8 +91,14 @@ namespace Vanara.InteropServices
|
|||
/// </returns>
|
||||
public static implicit operator string(SafeMemString<TMem> s) => s?.ToString();
|
||||
|
||||
/// <summary>Performs an explicit conversion from <see cref="SafeMemString{TMem}"/> to <see cref="System.Char"/>.</summary>
|
||||
/// <param name="s">The <see cref="SafeMemString{TMem}"/> instance.</param>
|
||||
/// <returns>The result of the conversion.</returns>
|
||||
/// <exception cref="InvalidCastException">Cannot convert an ANSI string to a Char pointer.</exception>
|
||||
public static unsafe explicit operator char*(SafeMemString<TMem> s) => s.CharSet == CharSet.Unicode ? (char*)(void*)s.handle : throw new InvalidCastException("Cannot convert an ANSI string to a Char pointer.");
|
||||
|
||||
/// <summary>Returns the string value held by this instance.</summary>
|
||||
/// <returns>A <see cref="System.String"/> value held by this instance or <c>null</c> if the handle is invalid.</returns>
|
||||
public override string ToString() => IsInvalid ? null : StringHelper.GetString(handle, chSet, Size);
|
||||
public override string ToString() => IsInvalid ? null : StringHelper.GetString(handle, CharSet, Size);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue