コード例 #1
0
ファイル: Wiki2Html.cs プロジェクト: Ashod/WikiDesk
        private static string ConvertUnaryCode(Regex regex, MatchedRegexHandler handler, string wikicode)
        {
            Match match = regex.Match(wikicode);
            while (match.Success)
            {
                string text = handler(match);

                StringBuilder sb = new StringBuilder(wikicode.Length);
                sb.Append(wikicode.Substring(0, match.Index));
                sb.Append(text);
                sb.Append(wikicode.Substring(match.Index + match.Length));
                wikicode = sb.ToString();

                //TODO: Optimize.
                match = regex.Match(wikicode, match.Index);
            }

            return wikicode;
        }
コード例 #2
0
ファイル: Wiki2Html.cs プロジェクト: Ashod/WikiDesk
        private static string ConvertBinaryCode(
            string wikicode,
            MismatchedCodeHandler misHandler,
            Regex regex,
            MatchedRegexHandler hitHandler)
        {
            if (string.IsNullOrEmpty(wikicode))
            {
                return wikicode;
            }

            Match match = regex.Match(wikicode);
            if (!match.Success)
            {
                return misHandler(wikicode);
            }

            int lastIndex = 0;
            StringBuilder sb = new StringBuilder(wikicode.Length * 2);

            while (match.Success && (lastIndex < wikicode.Length))
            {
                // Copy the skipped part.
                string code = wikicode.Substring(lastIndex, match.Index - lastIndex);
                if (!string.IsNullOrEmpty(code))
                {
                    sb.Append(misHandler(code));
                }

                // Handle the match. Either copy a replacement or the matched part as-is.
                code = hitHandler(match) ?? match.Value;
                sb.Append(code);

                lastIndex = match.Index + match.Length;
                match = match.NextMatch();
            }

            // Copy the remaining bit.
            if (lastIndex < wikicode.Length)
            {
                string code = wikicode.Substring(lastIndex);
                if (!string.IsNullOrEmpty(code))
                {
                    sb.Append(misHandler(code));
                }
            }

            return sb.ToString();
        }