Пример #1
0
        public static string ApplyRTLfix1(string line, int maxCharacters, bool ignoreNumbers)
        {
            // pattern = ignoreNumbers ? (all non ascii characters + numbers) : (all non ASCII characters)
            // tip: the character before the 0 is /  and the character after the 9 is :   (that's why the second pattern is [0x0-/  and  :-0xff]
            string pattern      = ignoreNumbers ? @"(\s|[^\x00-\xff])+" : @"(\s|[^\x00-\/:-\xff])+";
            var    regexPattern = new Regex(pattern);

            if (maxCharacters <= 0)
            {
                // Apply arabic fixer to all non ascii characters
                line = regexPattern.Replace(line, (m) => ReverseText(RTLFixer.Fix(m.Value)));
            }
            else
            {
                // Split into lines of maximum length
                var regex = new Regex(".{0," + maxCharacters + "}(\\s+|$)", RegexOptions.Multiline);
                line = line.Replace("\r\n", "\n");
                line = regex.Replace(line, "$0\n");

                line = line.Replace("\n\n", "\n");
                line = line.TrimEnd('\n');
                //if (line.EndsWith("\n\n"))
                //  line = line.Substring(0, line.Length - 2);

                // Apply the RTL fix for each line
                var lines = line.Split('\n');
                for (int i = 0, imax = lines.Length; i < imax; ++i)
                {
                    lines[i] = regexPattern.Replace(lines[i], (m) => ReverseText(RTLFixer.Fix(m.Value)));
                }
                line = string.Join("\n", lines);
            }

            return(line);
        }
Пример #2
0
        void AppendToCharSet(HashSet <char> sb, string text, bool isRTL)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            text = RemoveTagsPrefix(text, "[i2p_");
            text = RemoveTagsPrefix(text, "[i2s_");

            if (isRTL)
            {
                text = RTLFixer.Fix(text);
            }

            foreach (char c in text)
            {
                if (!mCharSetTool_CaseSensitive)
                {
                    sb.Add(char.ToLowerInvariant(c));
                    sb.Add(char.ToUpperInvariant(c));
                }
                else
                {
                    sb.Add(c);
                }
            }
        }
Пример #3
0
        public static string ApplyRTLfix(string line, int maxCharacters, bool ignoreNumbers)
        {
            if (string.IsNullOrEmpty(line))
            {
                return(line);
            }

            // Fix !, ? and . signs not set correctly
            char firstC = line[0];

            if (firstC == '!' || firstC == '.' || firstC == '?')
            {
                line = line.Substring(1) + firstC;
            }

            int tagStart = -1, tagEnd = 0;
            // Find any non-RTL character at the beggining because those need to be converted to RTL direction (place them at the end)
            //int ifirst = 0;
            //while (ifirst<line.Length && line[ifirst] < 255)
            //	ifirst++;

            // unless there is tag there
            //if (FindNextTag(line, 0, out tagStart, out tagEnd))
            //	ifirst = ifirst < tagStart ? ifirst : tagStart;

            //string linePrefix = ifirst>0 ? line.Substring(0, ifirst) : string.Empty;
            //line = line.Substring(ifirst);

            //int ilast = line.Length-1;
            //string linePostfix = "";
            //while (ilast > 0 && line[ilast] < 255)
            //{
            //	char c = line[ilast];
            //	switch (c)
            //	{
            //		case '(': c = ')'; break;
            //		case ')': c = '('; break;
            //		case '[': c = ']'; break;
            //		case ']': c = '['; break;
            //		case '{': c = '}'; break;
            //		case '}': c = '{'; break;
            //	}
            //	linePostfix += c;
            //	ilast--;
            //}
            //line = line.Substring(0, ilast);



            // Find all Tags (and Numbers if ignoreNumbers is true)
            int tagBase = 40000;

            tagEnd = 0;
            var tags = new List <string>();

            while (FindNextTag(line, tagEnd, out tagStart, out tagEnd))
            {
                string tag = "@@" + (char)(tagBase + tags.Count) + "@@";
                tags.Add(line.Substring(tagStart, tagEnd - tagStart + 1));

                line   = line.Substring(0, tagStart) + tag + line.Substring(tagEnd + 1);
                tagEnd = tagStart + 5;
            }

            // Split into lines and fix each line

            if (maxCharacters <= 0)
            {
                line = RTLFixer.Fix(line, true, !ignoreNumbers);
                //line = linePrefix + linePostfix + line;
            }
            else
            {
                // Split into lines of maximum length
                var regex = new Regex(".{0," + maxCharacters + "}(\\s+|$)", RegexOptions.Multiline);
                line = line.Replace("\r\n", "\n");
                line = regex.Replace(line, "$0\n");

                line = line.Replace("\n\n", "\n");
                line = line.TrimEnd('\n');
                //if (line.EndsWith("\n\n"))
                //  line = line.Substring(0, line.Length - 2);

                // Apply the RTL fix for each line
                var lines = line.Split('\n');
                for (int i = 0, imax = lines.Length; i < imax; ++i)
                {
                    lines[i] = RTLFixer.Fix(lines[i], true, !ignoreNumbers);
                }
                //lines[lines.Length-1] = linePrefix + linePostfix + lines[lines.Length - 1];

                line = string.Join("\n", lines);
            }


            // Restore all tags

            for (int i = 0; i < tags.Count; i++)
            {
                var len = line.Length;
                for (int j = 0; j < len; ++j)
                {
                    if (line[j] == '@' && line[j + 1] == '@' && line[j + 2] >= tagBase && line[j + 3] == '@' && line[j + 4] == '@')
                    {
                        int idx = line[j + 2] - tagBase;
                        if (idx % 2 == 0)
                        {
                            idx++;
                        }
                        else
                        {
                            idx--;
                        }
                        if (idx >= tags.Count)
                        {
                            idx = tags.Count - 1;
                        }

                        line = line.Substring(0, j) + tags[idx] + line.Substring(j + 5);

                        break;
                    }
                }
            }
            return(line);
        }
 private static string FixRTLString(string s)
 {
     return(Reverse(RTLFixer.Fix(s)));
 }
Пример #5
0
        public static string ApplyRTLfix(string line, int maxCharacters, bool ignoreNumbers)
        {
            if (string.IsNullOrEmpty(line))
            {
                return(line);
            }

            // Fix !, ? and . signs not set correctly
            char firstC = line[0];

            if (firstC == '!' || firstC == '.' || firstC == '?')
            {
                line = line.Substring(1) + firstC;
            }

            int tagStart = -1, tagEnd = 0;

            // Find all Tags (and Numbers if ignoreNumbers is true)
            int tagBase = 40000;

            tagEnd = 0;
            var tags = new List <string>();

            while (I2Utils.FindNextTag(line, tagEnd, out tagStart, out tagEnd))
            {
                string tag = "@@" + (char)(tagBase + tags.Count) + "@@";
                tags.Add(line.Substring(tagStart, tagEnd - tagStart + 1));

                line   = line.Substring(0, tagStart) + tag + line.Substring(tagEnd + 1);
                tagEnd = tagStart + 5;
            }

            // Split into lines and fix each line
            line = line.Replace("\r\n", "\n");
            line = I2Utils.SplitLine(line, maxCharacters);
            line = RTLFixer.Fix(line, true, !ignoreNumbers);


            // Restore all tags

            for (int i = 0; i < tags.Count; i++)
            {
                var len = line.Length;

                for (int j = 0; j < len; ++j)
                {
                    if (line[j] == '@' && line[j + 1] == '@' && line[j + 2] >= tagBase && line[j + 3] == '@' && line[j + 4] == '@')
                    {
                        int idx = line[j + 2] - tagBase;
                        if (idx % 2 == 0)
                        {
                            idx++;
                        }
                        else
                        {
                            idx--;
                        }
                        if (idx >= tags.Count)
                        {
                            idx = tags.Count - 1;
                        }

                        line = line.Substring(0, j) + tags[idx] + line.Substring(j + 5);

                        break;
                    }
                }
            }

            return(line);
        }