コード例 #1
0
        private static List <byte> getUnicodeToIranSystem(string text, IranSystemNumbers iranSystemNumbers)
        {
            var unicodeString = string.Format(" {0} ", text);
            var textBytes     = _encoding1256.GetBytes(unicodeString);

            byte pre     = 0;
            var  length  = textBytes.Length;
            var  results = new List <byte>(length);

            for (var i = 0; i < length; i++)
            {
                byte cur;
                var  currentChar = textBytes[i];

                if (isNumber(currentChar) && iranSystemNumbers == IranSystemNumbers.DontConvert)
                {
                    cur = currentChar;
                    results.Add(cur);
                    pre = cur;
                }
                else if (isLattinLetter(currentChar))
                {
                    cur = getLattinLetter(currentChar);
                    results.Add(cur);
                    pre = cur;
                }
                else if (i != 0 && i != length - 1)
                {
                    cur = getUnicodeToIranSystemChar(textBytes[i - 1], currentChar, textBytes[i + 1]);
                    if (cur == AlefChasban) // برای بررسی استثنای لا
                    {
                        if (pre == LaamAvval)
                        {
                            results.RemoveAt(results.Count - 1);
                            results.Add(LaaArabic);
                        }
                        else
                        {
                            results.Add(cur);
                        }
                    }
                    else
                    {
                        results.Add(cur);
                    }

                    pre = cur;
                }
            }

            return(results);
        }
コード例 #2
0
        /// <summary>
        /// تبديل رشته‌ي يونيكد به رشته‌ي ايران سيستمي
        /// </summary>
        /// <param name="text">رشته‌ي يونيكد</param>
        /// <param name="iranSystemNumbers">تبديل اعداد</param>
        /// <returns>رشته‌ي ايران سيستمي</returns>
        public static string ToIranSystem(
            this string text,
            IranSystemNumbers iranSystemNumbers = IranSystemNumbers.DontConvert)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            text = reverseNumbersAndLetters(text);
            var iranSystemBytes = getUnicodeToIranSystem(text, iranSystemNumbers);
            var iranSystemText  = _encoding1256.GetString(iranSystemBytes.ToArray()).Trim();

            return(new string(iranSystemText.Reverse().ToArray()));
        }