IsLetterCategory() private method

private IsLetterCategory ( UnicodeCategory uc ) : bool
uc UnicodeCategory
return bool
        /// <summary>Converts the specified string to title case (except for words that are entirely in uppercase, which are considered to be acronyms).</summary>
        /// <param name="str">The string to convert to title case. </param>
        /// <returns>The specified string converted to title case.</returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///         <paramref name="str" /> is <see langword="null" />. </exception>
        // Token: 0x060030A3 RID: 12451 RVA: 0x000B9EFC File Offset: 0x000B80FC
        public string ToTitleCase(string str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            if (str.Length == 0)
            {
                return(str);
            }
            StringBuilder stringBuilder = new StringBuilder();
            string        text          = null;

            for (int i = 0; i < str.Length; i++)
            {
                int             num;
                UnicodeCategory unicodeCategory = CharUnicodeInfo.InternalGetUnicodeCategory(str, i, out num);
                if (char.CheckLetter(unicodeCategory))
                {
                    i = this.AddTitlecaseLetter(ref stringBuilder, ref str, i, num) + 1;
                    int  num2 = i;
                    bool flag = unicodeCategory == UnicodeCategory.LowercaseLetter;
                    while (i < str.Length)
                    {
                        unicodeCategory = CharUnicodeInfo.InternalGetUnicodeCategory(str, i, out num);
                        if (TextInfo.IsLetterCategory(unicodeCategory))
                        {
                            if (unicodeCategory == UnicodeCategory.LowercaseLetter)
                            {
                                flag = true;
                            }
                            i += num;
                        }
                        else if (str[i] == '\'')
                        {
                            i++;
                            if (flag)
                            {
                                if (text == null)
                                {
                                    text = this.ToLower(str);
                                }
                                stringBuilder.Append(text, num2, i - num2);
                            }
                            else
                            {
                                stringBuilder.Append(str, num2, i - num2);
                            }
                            num2 = i;
                            flag = true;
                        }
                        else
                        {
                            if (TextInfo.IsWordSeparator(unicodeCategory))
                            {
                                break;
                            }
                            i += num;
                        }
                    }
                    int num3 = i - num2;
                    if (num3 > 0)
                    {
                        if (flag)
                        {
                            if (text == null)
                            {
                                text = this.ToLower(str);
                            }
                            stringBuilder.Append(text, num2, num3);
                        }
                        else
                        {
                            stringBuilder.Append(str, num2, num3);
                        }
                    }
                    if (i < str.Length)
                    {
                        i = TextInfo.AddNonLetter(ref stringBuilder, ref str, i, num);
                    }
                }
                else
                {
                    i = TextInfo.AddNonLetter(ref stringBuilder, ref str, i, num);
                }
            }
            return(stringBuilder.ToString());
        }
Esempio n. 2
0
        /// <summary>将指定字符串转换为标题大写(全部大写将被视为首字母缩写的词不包含在内)。</summary>
        /// <returns>转换为标题大写的指定字符串。</returns>
        /// <param name="str">转换为标题大写的字符串。</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="str" /> is null. </exception>
        public string ToTitleCase(string str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            if (str.Length == 0)
            {
                return(str);
            }
            StringBuilder result = new StringBuilder();
            string        str1   = (string)null;
            int           index1;

            for (int index2 = 0; index2 < str.Length; index2 = index1 + 1)
            {
                int             charLength;
                UnicodeCategory unicodeCategory1 = CharUnicodeInfo.InternalGetUnicodeCategory(str, index2, out charLength);
                if (char.CheckLetter(unicodeCategory1))
                {
                    index1 = this.AddTitlecaseLetter(ref result, ref str, index2, charLength) + 1;
                    int  startIndex = index1;
                    bool flag       = unicodeCategory1 == UnicodeCategory.LowercaseLetter;
                    while (index1 < str.Length)
                    {
                        UnicodeCategory unicodeCategory2 = CharUnicodeInfo.InternalGetUnicodeCategory(str, index1, out charLength);
                        if (TextInfo.IsLetterCategory(unicodeCategory2))
                        {
                            if (unicodeCategory2 == UnicodeCategory.LowercaseLetter)
                            {
                                flag = true;
                            }
                            index1 += charLength;
                        }
                        else if ((int)str[index1] == 39)
                        {
                            ++index1;
                            if (flag)
                            {
                                if (str1 == null)
                                {
                                    str1 = this.ToLower(str);
                                }
                                result.Append(str1, startIndex, index1 - startIndex);
                            }
                            else
                            {
                                result.Append(str, startIndex, index1 - startIndex);
                            }
                            startIndex = index1;
                            flag       = true;
                        }
                        else if (!TextInfo.IsWordSeparator(unicodeCategory2))
                        {
                            index1 += charLength;
                        }
                        else
                        {
                            break;
                        }
                    }
                    int count = index1 - startIndex;
                    if (count > 0)
                    {
                        if (flag)
                        {
                            if (str1 == null)
                            {
                                str1 = this.ToLower(str);
                            }
                            result.Append(str1, startIndex, count);
                        }
                        else
                        {
                            result.Append(str, startIndex, count);
                        }
                    }
                    if (index1 < str.Length)
                    {
                        index1 = TextInfo.AddNonLetter(ref result, ref str, index1, charLength);
                    }
                }
                else
                {
                    index1 = TextInfo.AddNonLetter(ref result, ref str, index2, charLength);
                }
            }
            return(result.ToString());
        }