2015-08-26 22:42:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace Nikse.SubtitleEdit.Core
|
|
|
|
|
{
|
2016-04-05 04:01:19 +02:00
|
|
|
|
public class NoBreakAfterItem : IComparable<NoBreakAfterItem>
|
2015-08-26 22:42:40 +02:00
|
|
|
|
{
|
2019-01-28 19:35:15 +01:00
|
|
|
|
public Regex Regex { get; }
|
2015-08-26 22:42:40 +02:00
|
|
|
|
public readonly string Text;
|
|
|
|
|
|
|
|
|
|
public NoBreakAfterItem(Regex regex, string text)
|
|
|
|
|
{
|
|
|
|
|
Regex = regex;
|
|
|
|
|
Text = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NoBreakAfterItem(string text)
|
|
|
|
|
{
|
|
|
|
|
Text = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMatch(string line)
|
|
|
|
|
{
|
2016-04-05 04:01:19 +02:00
|
|
|
|
// Make sure that both *line and *Text are not null.
|
|
|
|
|
if (string.IsNullOrEmpty(line) || string.IsNullOrEmpty(Text))
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2016-04-05 04:01:19 +02:00
|
|
|
|
return false;
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 22:42:40 +02:00
|
|
|
|
if (Regex != null)
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2015-08-26 22:42:40 +02:00
|
|
|
|
return Regex.IsMatch(line);
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 13:53:51 +01:00
|
|
|
|
if (line.EndsWith(Text, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
var indexBeforeText = line.Length - Text.Length - 1;
|
|
|
|
|
return indexBeforeText < 0 || line[indexBeforeText] == ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return Text;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 04:01:19 +02:00
|
|
|
|
public int CompareTo(NoBreakAfterItem obj)
|
2015-08-26 22:42:40 +02:00
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2015-08-26 22:42:40 +02:00
|
|
|
|
return -1;
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 04:01:19 +02:00
|
|
|
|
if (obj.Text == null && Text == null)
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2015-08-26 22:42:40 +02:00
|
|
|
|
return 0;
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
2016-04-05 04:01:19 +02:00
|
|
|
|
else if (obj.Text == null)
|
2019-01-19 14:40:37 +01:00
|
|
|
|
{
|
2015-08-26 22:42:40 +02:00
|
|
|
|
return -1;
|
2019-01-19 14:40:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 04:01:19 +02:00
|
|
|
|
return string.Compare(Text, obj.Text, StringComparison.Ordinal);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-24 11:51:04 +01:00
|
|
|
|
}
|