Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private readonly string[] _commonTags = { "<i>", "<b>", "<u>", "<font " };
- private string FixDigitOrLetterAdjacentToTag(string text)
- {
- // foo<i>bar => foo <i>bar | foo<i><b>bar => foo <i><b>bar
- foreach (var commonTag in _commonTags)
- {
- var firstTagIndex = text.IndexOf(commonTag, StringComparison.Ordinal);
- if (firstTagIndex < 1)
- {
- continue;
- }
- // add chars you want to match e.g: ], )
- if (char.IsLetterOrDigit(text[firstTagIndex - 1]) && IsLetterOrDigitAfterTag(text, firstTagIndex + commonTag.Length))
- {
- text = text.Insert(firstTagIndex, " ");
- }
- }
- return text;
- bool IsLetterOrDigitAfterTag(string s, int index)
- {
- if (index == s.Length)
- {
- return false;
- }
- var ch = s[index];
- if (char.IsLetterOrDigit(ch))
- {
- return true;
- }
- foreach (var tag in _commonTags)
- {
- // we are expecting the tag + 1 char/letter
- if (index + tag.Length + 1 >= s.Length)
- {
- continue;
- }
- var tagIndex = text.IndexOf(tag, index, StringComparison.Ordinal);
- if (tagIndex != index)
- {
- continue;
- }
- return IsLetterOrDigitAfterTag(text, tagIndex + tag.Length);
- }
- return false;
- }
- }
- // https://github.com/SubtitleEdit/subtitleedit/issues/5110
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement