Exemple #1
0
        public static string ToUpper(string src, string locale)
        {
            if (src == null)
            {
                return(string.Empty);
            }

            int    length = src.Length + 10;
            IntPtr resPtr = Marshal.AllocCoTaskMem(length * 2);

            try
            {
                ErrorCode err;
                int       outLength = NativeMethods.u_strToUpper(resPtr, length, src, src.Length, locale, out err);
                if (err > 0 && err != ErrorCode.BUFFER_OVERFLOW_ERROR)
                {
                    throw new Exception("UnicodeString.ToUpper() failed with code " + err);
                }
                if (outLength > length)
                {
                    err = ErrorCode.NoErrors;                     // ignore possible U_BUFFER_OVERFLOW_ERROR
                    Marshal.FreeCoTaskMem(resPtr);
                    length = outLength + 1;
                    resPtr = Marshal.AllocCoTaskMem(length * 2);
                    NativeMethods.u_strToUpper(resPtr, length, src, src.Length, locale, out err);
                }
                if (err > 0)
                {
                    throw new Exception("UnicodeString.ToUpper() failed with code " + err);
                }

                string result = Marshal.PtrToStringUni(resPtr);
                // Strip any garbage left over at the end of the string.
                if (err == ErrorCode.STRING_NOT_TERMINATED_WARNING && result != null)
                {
                    return(result.Substring(0, outLength));
                }
                return(result);
            }
            finally
            {
                Marshal.FreeCoTaskMem(resPtr);
            }
        }