private static string ConvertBinaryCode( string wikicode, MismatchedCodeHandler misHandler, char startMarker, char endMarker, MatchedCodeHandler hitHandler) { if (string.IsNullOrEmpty(wikicode)) { return string.Empty; } // Find the open tag. int startOffset = 0; int lastIndex; string contents = StringUtils.ExtractBlock(wikicode, startMarker, endMarker, ref startOffset, out lastIndex); if (string.IsNullOrEmpty(contents)) { return wikicode; } StringBuilder sb = new StringBuilder(wikicode.Length * 16); int curWikiOffset = 0; while (lastIndex < wikicode.Length) { string wikiChunk = wikicode.Substring(curWikiOffset, startOffset - curWikiOffset); sb.Append(misHandler(wikiChunk)); sb.Append(hitHandler(contents)); ++lastIndex; startOffset = lastIndex; curWikiOffset = lastIndex; contents = StringUtils.ExtractBlock(wikicode, startMarker, endMarker, ref startOffset, out lastIndex); if (string.IsNullOrEmpty(contents)) { if (curWikiOffset >= 0 && curWikiOffset < wikicode.Length) { sb.Append(misHandler(wikicode.Substring(curWikiOffset))); } break; } } return sb.ToString(); }
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(); }