Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="src"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static string Convert(string src, KanaOption option)
        {
            if (String.IsNullOrEmpty(src))
            {
                return(src);
            }

            var isRomanToNarrow      = (option & KanaOption.RomanToNarrow) == KanaOption.RomanToNarrow;
            var isRomanToWide        = (option & KanaOption.RomanToWide) == KanaOption.RomanToWide;
            var isNumericToNarrow    = (option & KanaOption.NumericToNarrow) == KanaOption.NumericToNarrow;
            var isNumericToWide      = (option & KanaOption.NumericToWide) == KanaOption.NumericToWide;
            var isAsciiToNarrow      = (option & KanaOption.AsciiToNarrow) == KanaOption.AsciiToNarrow;
            var isAsciiToWide        = (option & KanaOption.AsciiToWide) == KanaOption.AsciiToWide;
            var isSpaceToNarrow      = (option & KanaOption.SpaceToNarrow) == KanaOption.SpaceToNarrow;
            var isSpaceToWide        = (option & KanaOption.SpaceToWide) == KanaOption.SpaceToWide;
            var isKanaToNarrow       = (option & KanaOption.KanaToNarrow) == KanaOption.KanaToNarrow;
            var isKanaToWide         = (option & KanaOption.KanaToWide) == KanaOption.KanaToWide;
            var isHiraganaToHankana  = (option & KanaOption.HiraganaToHankana) == KanaOption.HiraganaToHankana;
            var isHankanaToHiragana  = (option & KanaOption.HankanaToHiragana) == KanaOption.HankanaToHiragana;
            var isKatakanaToHiragana = (option & KanaOption.KatakanaToHiragana) == KanaOption.KatakanaToHiragana;
            var isHiraganaToKatakana = (option & KanaOption.HiraganaToKatakana) == KanaOption.HiraganaToKatakana;

            var sb = new StringBuilder();

            for (var i = 0; i < src.Length; i++)
            {
                var  c    = src[i];
                var  next = i < src.Length - 1 ? src[i + 1] : (char)0;
                char convert;

                // Roman
                if (isRomanToNarrow && RomanWideToNarrow.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                if (isRomanToWide && RomanNarrowToWide.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                // Numeric
                if (isNumericToNarrow && NumericWideToNarrow.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                if (isNumericToWide && NumericNarrowToWide.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                // ASCII
                if (isAsciiToNarrow && AsciiWideToNarrow.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                if (isAsciiToWide && AsciiNarrowToWide.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                // Space
                if (isSpaceToNarrow && (c == ' '))
                {
                    sb.Append(' ');
                    continue;
                }

                if (isSpaceToWide && (c == ' '))
                {
                    sb.Append(' ');
                    continue;
                }

                // WideKatakana/NarrowKatakana
                if (isKanaToNarrow)
                {
                    if (KanaDakuonWideToKanaDakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        sb.Append('゙');
                        continue;
                    }

                    if (KanaHandakuonWideToKanaHandakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        sb.Append('゚');
                        continue;
                    }

                    if (KanaWideToKanaNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                if (isKanaToWide)
                {
                    if ((next == '゙') && KanaDakuonNarrowToKanaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if ((next == '゚') && KanaHandakuonNarrowToKanaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if (KanaNarrowToKanaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                // WideHiragana/NarrowKatanaka
                if (isHiraganaToHankana)
                {
                    if (HiraganaDakuonWideToKanaDakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert).Append('゙');
                        continue;
                    }

                    if (HiraganaHandakuonWideToKanaHandakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert).Append('゚');
                        continue;
                    }

                    if (HiraganaWideToKanaNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                if (isHankanaToHiragana)
                {
                    if ((next == '゙') && KanaDakuonNarrowToHiraganaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if ((next == '゚') && KanaHandakuonNarrowToHiraganaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if (KanaNarrowToHiraganaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                // WideHiragana/WideKatakana
                if (isKatakanaToHiragana)
                {
                    if (KanaDakuonWideToHiraganaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (KanaHandakuonWideToHiraganaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (KanaWideToHiraganaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                if (isHiraganaToKatakana)
                {
                    if (HiraganaDakuonWideToKanaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (HiraganaHandakuonWideToKanaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (HiraganaWideToKanaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                sb.Append(c);
            }

            return(sb.ToString());
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="src"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static string Convert(string src, KanaOption option)
        {
            if (String.IsNullOrEmpty(src))
            {
                return src;
            }

            var isRomanToNarrow = (option & KanaOption.RomanToNarrow) == KanaOption.RomanToNarrow;
            var isRomanToWide = (option & KanaOption.RomanToWide) == KanaOption.RomanToWide;
            var isNumericToNarrow = (option & KanaOption.NumericToNarrow) == KanaOption.NumericToNarrow;
            var isNumericToWide = (option & KanaOption.NumericToWide) == KanaOption.NumericToWide;
            var isAsciiToNarrow = (option & KanaOption.AsciiToNarrow) == KanaOption.AsciiToNarrow;
            var isAsciiToWide = (option & KanaOption.AsciiToWide) == KanaOption.AsciiToWide;
            var isSpaceToNarrow = (option & KanaOption.SpaceToNarrow) == KanaOption.SpaceToNarrow;
            var isSpaceToWide = (option & KanaOption.SpaceToWide) == KanaOption.SpaceToWide;
            var isKanaToNarrow = (option & KanaOption.KanaToNarrow) == KanaOption.KanaToNarrow;
            var isKanaToWide = (option & KanaOption.KanaToWide) == KanaOption.KanaToWide;
            var isHiraganaToHankana = (option & KanaOption.HiraganaToHankana) == KanaOption.HiraganaToHankana;
            var isHankanaToHiragana = (option & KanaOption.HankanaToHiragana) == KanaOption.HankanaToHiragana;
            var isKatakanaToHiragana = (option & KanaOption.KatakanaToHiragana) == KanaOption.KatakanaToHiragana;
            var isHiraganaToKatakana = (option & KanaOption.HiraganaToKatakana) == KanaOption.HiraganaToKatakana;

            var sb = new StringBuilder();
            for (var i = 0; i < src.Length; i++)
            {
                var c = src[i];
                var next = i < src.Length - 1 ? src[i + 1] : (char)0;
                char convert;

                // Roman
                if (isRomanToNarrow && RomanWideToNarrow.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                if (isRomanToWide && RomanNarrowToWide.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                // Numeric
                if (isNumericToNarrow && NumericWideToNarrow.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                if (isNumericToWide && NumericNarrowToWide.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                // ASCII
                if (isAsciiToNarrow && AsciiWideToNarrow.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                if (isAsciiToWide && AsciiNarrowToWide.TryGetValue(c, out convert))
                {
                    sb.Append(convert);
                    continue;
                }

                // Space
                if (isSpaceToNarrow && (c == ' '))
                {
                    sb.Append(' ');
                    continue;
                }

                if (isSpaceToWide && (c == ' '))
                {
                    sb.Append(' ');
                    continue;
                }

                // WideKatakana/NarrowKatakana
                if (isKanaToNarrow)
                {
                    if (KanaDakuonWideToKanaDakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        sb.Append('゙');
                        continue;
                    }

                    if (KanaHandakuonWideToKanaHandakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        sb.Append('゚');
                        continue;
                    }

                    if (KanaWideToKanaNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                if (isKanaToWide)
                {
                    if ((next == '゙') && KanaDakuonNarrowToKanaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if ((next == '゚') && KanaHandakuonNarrowToKanaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if (KanaNarrowToKanaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                // WideHiragana/NarrowKatanaka
                if (isHiraganaToHankana)
                {
                    if (HiraganaDakuonWideToKanaDakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert).Append('゙');
                        continue;
                    }

                    if (HiraganaHandakuonWideToKanaHandakuonNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert).Append('゚');
                        continue;
                    }

                    if (HiraganaWideToKanaNarrow.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                if (isHankanaToHiragana)
                {
                    if ((next == '゙') && KanaDakuonNarrowToHiraganaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if ((next == '゚') && KanaHandakuonNarrowToHiraganaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        i++;
                        continue;
                    }

                    if (KanaNarrowToHiraganaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                // WideHiragana/WideKatakana
                if (isKatakanaToHiragana)
                {
                    if (KanaDakuonWideToHiraganaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (KanaHandakuonWideToHiraganaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (KanaWideToHiraganaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                if (isHiraganaToKatakana)
                {
                    if (HiraganaDakuonWideToKanaDakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (HiraganaHandakuonWideToKanaHandakuonWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }

                    if (HiraganaWideToKanaWide.TryGetValue(c, out convert))
                    {
                        sb.Append(convert);
                        continue;
                    }
                }

                sb.Append(c);
            }

            return sb.ToString();
        }