Exemplo n.º 1
0
        /// <summary>
        /// Scans the string looking for unmappable characters in the ASCII set, and replaces
        /// them with '?'.
        /// </summary>
        /// <param name="inputString">A unicode string with unknown characters.</param>
        /// <param name="charCount">The length of the string to sanitize.</param>
        /// <param name="allocedMemory">On output, a value that needs to be freed. Only used
        /// if there are any untranslaable characters.</param>
        /// <returns>A string that has all legal ASCII characters.</returns>
        private unsafe char *SanitizeString(char *inputString, int charCount, out IntPtr allocedMemory)
        {
            allocedMemory = IntPtr.Zero;

            bool  needToDuplicate = false;
            char *returnString    = inputString;

            for (int i = 0; i < charCount; ++i)
            {
                if (inputString[i] > 127)
                {
                    needToDuplicate = true;
                    break;
                }
            }

            if (needToDuplicate)
            {
                allocedMemory = LibraryHelpers.MarshalAllocHGlobal(charCount);
                returnString  = (char *)allocedMemory;

                char *dest = returnString;

                for (int i = 0; i < charCount; ++i)
                {
                    dest[i] = inputString[i] > 127 ? '?' : inputString[i];
                }
            }

            return(returnString);
        }