Advertisement
ivandrofly

Subtitle Edit - Remove empty tags - Algorithm

Nov 17th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. public static string RemoveEmptyTags(string text)
  2.         {
  3.             int lowerBound = 0;
  4.             // <tag></tag> => "".
  5.             const string EmptyTags = "><";
  6.             int idx = text.IndexOf(EmptyTags);
  7.             while (idx >= 0)
  8.             {
  9.                 int j = idx - 1;
  10.                 int k = idx + 2;
  11.                 // Find tag start index.
  12.                 for (; j >= lowerBound; j--)
  13.                 {
  14.                     if (text[j] == '>' || text[j] == '<') break;
  15.                 }
  16.                 // Find tag end index.
  17.                 for (; k < text.Length; k++)
  18.                 {
  19.                     if (text[k] == '<' || text[k] == '>') break;
  20.                 }
  21.                 // Invalid tag.
  22.                 if ((j < 0 || text[j] != '<') && k >= text.Length)
  23.                 {
  24.                     break;
  25.                 }
  26.                 // For tags like: ><Foobar<b></b>.>< => ><Foobar.>< or ><.<b></b>Foobar>< => ><.Foobar><
  27.                 if ((j >= 0 && text[j] == '>') || (k < text.Length && text[k] == '<'))
  28.                 {
  29.                     lowerBound = idx + 2;
  30.                     idx = text.IndexOf(EmptyTags, lowerBound);
  31.                     continue;
  32.                 }
  33.                 if (Configuration.Settings.Tools.OcrFixUseHardcodedRules)
  34.                 {
  35.                     // Foo><tag>bar. =>  Foobar
  36.                     if (j < 0 && k < text.Length && text[k] == '>')
  37.                     {
  38.                         j = idx;
  39.                     }
  40.                     // Foo<tag><bar. => Foobar.
  41.                     else if (j >= 0 && text[j] == '<' && k >= text.Length)
  42.                     {
  43.                         k = idx + 1; // Note: +1 will be add when removing.
  44.                     }
  45.                 }
  46.                 else
  47.                 {
  48.                     // Do nothing for text like: ><Foobar.><
  49.                     if (j < lowerBound || k >= text.Length)
  50.                     {
  51.                         lowerBound = idx + 2;
  52.                         idx = text.IndexOf(EmptyTags, lowerBound);
  53.                         continue;
  54.                     }
  55.                 }
  56.  
  57.                 // Remove no space inserted.
  58.                 text = text.Remove(j, k - j + 1);
  59.                 idx = text.IndexOf(EmptyTags, j);
  60.             }
  61.             return text;
  62.         }
  63.  
  64.  
  65. // Note: Algorithms written for Subtitle Edit.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement