AutoPatientInfoRecorder/src/AntdUI/Lib/Vanara/Core/StringHelper.cs

(开头部分) 15KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

// THIS FILE IS PART OF Vanara PROJECT
// THE Vanara PROJECT IS AN OPENSOURCE LIBRARY LICENSED UNDER THE MIT License.
// COPYRIGHT (C) dahall. ALL RIGHTS RESERVED.
// GITHUB: https://github.com/dahall/Vanara

using System;
using System.Runtime.InteropServices;
using System.Security;

namespace Vanara.Extensions
{
    /// <summary>A safe class that represents an object that is pinned in memory.</summary>
    /// <seealso cref="System.IDisposable"/>
    public static class StringHelper
    {
        /// <summary>Allocates a block of memory allocated from the unmanaged COM task allocator sufficient to hold the number of specified characters.</summary>
        /// <param name="count">The number of characters, inclusive of the null terminator.</param>
        /// <param name="memAllocator">The method used to allocate the memory.</param>
        /// <param name="charSet">The character set.</param>
        /// <returns>The address of the block of memory allocated.</returns>
        public static IntPtr AllocChars(uint count, Func<int, IntPtr> memAllocator, CharSet charSet = CharSet.Auto)
        {
            if (count == 0) return IntPtr.Zero;
            var sz = GetCharSize(charSet);
            var ptr = memAllocator((int)count * sz);
            if (count > 0)
            {
                if (sz == 1)
                    Marshal.WriteByte(ptr, 0);
                else
                    Marshal.WriteInt16(ptr, 0);
            }
            return ptr;
        }

        /// <summary>Allocates a block of memory allocated from the unmanaged COM task allocator sufficient to hold the number of specified characters.</summary>
        /// <param name="count">The number of characters, inclusive of the null terminator.</param>
        /// <param name="charSet">The character set.</param>
        /// <returns>The address of the block of memory allocated.</returns>
        public static IntPtr AllocChars(uint count, CharSet charSet = CharSet.Auto) => AllocChars(count, Marshal.AllocCoTaskMem, charSet);

        /// <summary>Copies the contents of a managed <see cref="SecureString"/> object to a block of memory allocated from the unmanaged COM task allocator.</summary>
        /// <param name="s">The managed object to copy.</param>
        /// <param name="charSet">The character set.</param>
        /// <returns>The address, in unmanaged memory, where the <paramref name="s"/> parameter was copied to, or 0 if a null object was supplied.</returns>
        public static IntPtr AllocSecureString(SecureString s, CharSet charSet = CharSet.Auto)
        {
            if (s == null) return IntPtr.Zero;
            if (GetCharSize(charSet) == 2)
                return Marshal.SecureStringToCoTaskMemUnicode(s);
            return Marshal.SecureStringToCoTaskMemAnsi(s);
        }

        /// <summary>Copies the contents of a managed <see cref="SecureString"/> object to a block of memory allocated from a supplied allocation method.</summary>
        /// <param name="s">The managed object to copy.</param>
        /// <param name="charSet">The character set.</param>
        /// <param name="memAllocator">The method used to allocate the memory.</param>
        /// <returns>The address, in unmanaged memory, where the <paramref name="s"/> parameter was copied to, or 0 if a null object was supplied.</returns>
        public static IntPtr AllocSecureString(SecureString s, CharSet charSet, Func<int, IntPtr> memAllocator) => AllocSecureString(s, charSet, memAllocator, out _);

后面还有 194 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。