public static bool HasPrefix(string input, ContinuationProfile profile)
        {
            // Return if only prefix
            if (IsOnlyPrefix(input, profile))
            {
                return(false);
            }

            if (profile.Prefix.Length > 0 && (input.StartsWith(profile.Prefix) && !input.StartsWith(profile.Prefix + Environment.NewLine)))
            {
                return(true);
            }

            if (profile.UseDifferentStyleGap && profile.GapPrefix.Length > 0 && (input.StartsWith(profile.GapPrefix) && !input.StartsWith(profile.GapPrefix + Environment.NewLine)))
            {
                return(true);
            }

            foreach (string prefix in Prefixes)
            {
                if (input.StartsWith(prefix) && !input.StartsWith(prefix + Environment.NewLine))
                {
                    return(true);
                }
            }

            return(false);
        }
        public static bool IsOnlySuffix(string input, ContinuationProfile profile)
        {
            var checkString = input;

            if (string.IsNullOrEmpty(input.Trim()))
            {
                return(false);
            }

            if (profile.Suffix.Length > 0)
            {
                checkString = checkString.Replace(profile.Suffix, "");
            }

            if (profile.UseDifferentStyleGap && profile.GapSuffix.Length > 0)
            {
                checkString = checkString.Replace(profile.GapSuffix, "");
            }

            foreach (string suffix in Suffixes)
            {
                checkString = checkString.Replace(suffix, "");
            }

            checkString = checkString.Trim();

            return(string.IsNullOrEmpty(checkString));
        }
        public static bool HasSuffix(string input, ContinuationProfile profile)
        {
            // Return if only suffix
            if (IsOnlySuffix(input, profile))
            {
                return(false);
            }

            if (profile.Suffix.Length > 0 && (input.EndsWith(profile.Suffix) && !input.EndsWith(Environment.NewLine + profile.Suffix)))
            {
                return(true);
            }

            if (profile.UseDifferentStyleGap && profile.GapSuffix.Length > 0 && (input.EndsWith(profile.GapSuffix) && !input.EndsWith(Environment.NewLine + profile.GapSuffix)))
            {
                return(true);
            }

            foreach (string suffix in Suffixes)
            {
                if ((input.EndsWith(suffix) && !input.EndsWith(Environment.NewLine + suffix)) && input.Length > suffix.Length)
                {
                    return(true);
                }
            }

            return(false);
        }
        public static bool EndsWithNothing(string input, ContinuationProfile profile)
        {
            // Return if empty string
            if (string.IsNullOrEmpty(input))
            {
                return(true);
            }

            return(!HasSuffixUnsafe(input, profile) && !IsEndOfSentence(input) && !input.EndsWith(",") && !input.EndsWith(":") && !input.EndsWith(";") && !input.EndsWith("-"));
        }
        public static string RemoveSuffix(string originalText, ContinuationProfile profile, List <string> additionalSuffixes, bool addComma)
        {
            string text = SanitizeString(originalText);

            // Return if empty string
            if (string.IsNullOrEmpty(text))
            {
                return(originalText);
            }

            // Get last word
            string lastWord    = GetLastWord(text);
            string newLastWord = lastWord;

            foreach (string suffix in Suffixes.Union(additionalSuffixes))
            {
                if (newLastWord.EndsWith(suffix) && !newLastWord.EndsWith(Environment.NewLine + suffix))
                {
                    newLastWord = newLastWord.Substring(0, newLastWord.Length - suffix.Length);
                }
            }
            newLastWord = newLastWord.Trim();

            if (addComma && !newLastWord.EndsWith(","))
            {
                newLastWord = newLastWord + ",";
            }

            string result;

            // If we can find it...
            if (originalText.LastIndexOf(lastWord, StringComparison.Ordinal) >= 0)
            {
                // Replace it
                result = ReplaceLastOccurrence(originalText, lastWord, newLastWord);
            }
            else
            {
                // Just remove whatever suffix we need to remove
                var suffix = lastWord.Replace(newLastWord, "");
                result = ReplaceLastOccurrence(originalText, suffix, "");
            }

            // Return original if empty string
            if (string.IsNullOrEmpty(result))
            {
                return(originalText);
            }

            return(result);
        }
        public static string RemoveAllPrefixes(string originalText, ContinuationProfile profile)
        {
            var text = originalText;

            // Return if empty string
            if (string.IsNullOrEmpty(text))
            {
                return(originalText);
            }

            while (HasPrefix(SanitizeString(text, false), profile))
            {
                text = RemovePrefix(text, profile, false, false /* Not used because of false before */);
            }
            return(text);
        }
        public static bool ShouldAddSuffix(string input, ContinuationProfile profile, bool sanitize)
        {
            string text = sanitize ? SanitizeString(input) : input;

            // Return if empty string
            if (string.IsNullOrEmpty(text))
            {
                return(false);
            }

            if ((EndsWithNothing(text, profile) || text.EndsWith(",") || HasSuffix(text, profile)) && !text.EndsWith("--") && !text.EndsWith(":") && !text.EndsWith(";"))
            {
                return(true);
            }

            return(false);
        }
 public static string RemovePrefix(string originalText, ContinuationProfile profile)
 {
     return(RemovePrefix(originalText, profile, true, false));
 }
        public static string RemovePrefix(string originalText, ContinuationProfile profile, bool shouldRemoveDashesDuringSanitization, bool gap)
        {
            // Decide if we need to remove dashes
            string textWithDash      = SanitizeString(originalText, false);
            string textWithoutDash   = SanitizeString(originalText, true);
            string leadingDialogDash = null;
            bool   removeDashesDuringSanitization = shouldRemoveDashesDuringSanitization;

            // Return if empty string
            if (string.IsNullOrEmpty(textWithDash) && string.IsNullOrEmpty(textWithoutDash))
            {
                return(originalText);
            }

            // If we're using a profile with dashes, count those as dashes instead of dialog dashes.
            if (removeDashesDuringSanitization)
            {
                foreach (string prefix in DashPrefixes)
                {
                    if ((!gap && profile.Prefix == prefix) ||
                        (gap && profile.UseDifferentStyleGap && profile.GapPrefix == prefix) ||
                        (gap && !profile.UseDifferentStyleGap && profile.Prefix == prefix))
                    {
                        removeDashesDuringSanitization = false;
                        leadingDialogDash = prefix;
                        break;
                    }
                }
            }

            // If there is only a dash on the first line, count it as dash instead of dialog dash.
            if (removeDashesDuringSanitization && textWithDash != null)
            {
                var split            = textWithDash.SplitToLines();
                int lastLineWithDash = -1;

                for (var i = 0; i < split.Count; i++)
                {
                    foreach (string prefix in DashPrefixes)
                    {
                        if (split[i].StartsWith(prefix))
                        {
                            lastLineWithDash = i;
                            break;
                        }
                    }
                }

                if (lastLineWithDash == 0)
                {
                    removeDashesDuringSanitization = false;
                }
            }

            string text = removeDashesDuringSanitization ? textWithoutDash : textWithDash;

            // Return if empty string
            if (string.IsNullOrEmpty(text))
            {
                return(originalText);
            }

            // Get first word of the paragraph
            string firstWord    = GetFirstWord(text);
            string newFirstWord = firstWord;

            if (leadingDialogDash != null)
            {
                newFirstWord = newFirstWord.TrimStart(Convert.ToChar(leadingDialogDash)).Trim();
            }

            foreach (string prefix in Prefixes)
            {
                if (newFirstWord.StartsWith(prefix) && !newFirstWord.EndsWith(prefix + Environment.NewLine))
                {
                    newFirstWord = newFirstWord.Substring(prefix.Length);
                }
            }
            newFirstWord = newFirstWord.Trim();

            string result;

            // If we can find it...
            if (originalText.IndexOf(firstWord, StringComparison.Ordinal) >= 0)
            {
                // Replace it
                result = ReplaceFirstOccurrence(originalText, firstWord, newFirstWord);
            }
            else
            {
                // Just remove whatever prefix we need to remove
                var prefix = firstWord.Replace(newFirstWord, "");
                result = ReplaceFirstOccurrence(originalText, prefix, "");
            }

            // Return original if empty string
            if (string.IsNullOrEmpty(result))
            {
                return(originalText);
            }

            return(result);
        }
 public static string RemoveSuffix(string originalText, ContinuationProfile profile, bool addComma)
 {
     return(RemoveSuffix(originalText, profile, new List <string>(), addComma));
 }
 public static string AddPrefixIfNeeded(string originalText, ContinuationProfile profile, bool gap)
 {
     return(AddPrefixIfNeeded(originalText, profile, true, gap));
 }
        public static string AddPrefixIfNeeded(string originalText, ContinuationProfile profile, bool shouldRemoveDashesDuringSanitization, bool gap)
        {
            // Decide if we need to remove dashes
            string textWithDash    = SanitizeString(originalText, false);
            string textWithoutDash = SanitizeString(originalText, true);
            bool   removeDashesDuringSanitization = shouldRemoveDashesDuringSanitization;

            // Return if empty string
            if (string.IsNullOrEmpty(textWithDash) && string.IsNullOrEmpty(textWithoutDash))
            {
                return(originalText);
            }

            // If we're using a profile with dashes, count those as dashes instead of dialog dashes.
            if (removeDashesDuringSanitization)
            {
                foreach (string prefix in DashPrefixes)
                {
                    if ((!gap && profile.Prefix == prefix) ||
                        (gap && profile.UseDifferentStyleGap && profile.GapPrefix == prefix) ||
                        (gap && !profile.UseDifferentStyleGap && profile.Prefix == prefix))
                    {
                        removeDashesDuringSanitization = false;
                        break;
                    }
                }
            }

            // If there is only a dash on the first line, count it as dash instead of dialog dash.
            if (removeDashesDuringSanitization && textWithDash != null)
            {
                var split            = textWithDash.SplitToLines();
                int lastLineWithDash = -1;

                for (var i = 0; i < split.Count; i++)
                {
                    foreach (string prefix in DashPrefixes)
                    {
                        if (split[i].StartsWith(prefix))
                        {
                            lastLineWithDash = i;
                            break;
                        }
                    }
                }

                if (lastLineWithDash == 0)
                {
                    removeDashesDuringSanitization = false;
                }
            }

            string text = removeDashesDuringSanitization ? textWithoutDash : textWithDash;

            // Return if empty string
            if (string.IsNullOrEmpty(text))
            {
                return(originalText);
            }

            // Return if only suffix/prefix
            if (IsOnlySuffix(text, profile) || IsOnlyPrefix(text, profile))
            {
                return(originalText);
            }

            // Get first word of the paragraph
            string firstWord    = GetFirstWord(text);
            string newFirstWord = firstWord;

            if (gap && profile.UseDifferentStyleGap)
            {
                // Make new first word
                string gapAddStart = profile.GapPrefix + (profile.GapPrefixAddSpace ? " " : "");

                if (gapAddStart.Length == 0 || !newFirstWord.StartsWith(gapAddStart))
                {
                    newFirstWord = gapAddStart + newFirstWord;
                }
            }
            else
            {
                // Make new first word
                string addStart = profile.Prefix + (profile.PrefixAddSpace ? " " : "");

                if (addStart.Length == 0 || !newFirstWord.StartsWith(addStart))
                {
                    newFirstWord = addStart + newFirstWord;
                }
            }

            // Check if it's not surrounded by HTML tags, then we'll place it outside the tags
            // Only if it's not a tag across the whole subtitle.
            var wordIndex = originalText.IndexOf(firstWord, StringComparison.Ordinal);

            if (wordIndex >= 3)
            {
                int currentIndex = wordIndex - 1;
                if (currentIndex >= 0 && originalText[currentIndex] == '>' && !IsFullLineTag(originalText, currentIndex))
                {
                    while (currentIndex - 1 >= 0 && !(originalText[currentIndex - 1] != '>' && originalText[currentIndex] == '<'))
                    {
                        currentIndex--;
                    }
                    var prefix = newFirstWord.Replace(firstWord, "");
                    return(originalText.Insert(currentIndex, prefix));
                }
            }

            // Replace it
            return(ReplaceFirstOccurrence(originalText, firstWord, newFirstWord));
        }
 public static string AddSuffixIfNeeded(string originalText, ContinuationProfile profile, bool gap)
 {
     return(AddSuffixIfNeeded(originalText, profile, gap, false));
 }
        public static string AddSuffixIfNeeded(string originalText, ContinuationProfile profile, bool gap, bool addComma)
        {
            string text = SanitizeString(originalText);

            // Return if empty string
            if (string.IsNullOrEmpty(text))
            {
                return(originalText);
            }

            // Return if only suffix/prefix
            if (IsOnlySuffix(text, profile) || IsOnlyPrefix(text, profile))
            {
                return(originalText);
            }

            // Get last word
            string lastWord    = GetLastWord(text);
            string newLastWord = lastWord;

            if (gap && profile.UseDifferentStyleGap)
            {
                // Make new last word
                string gapAddEnd = (profile.GapSuffixAddSpace ? " " : "") + profile.GapSuffix;

                if (gapAddEnd.Length == 0 || !newLastWord.EndsWith(gapAddEnd))
                {
                    newLastWord = newLastWord.TrimEnd(',') + ((lastWord.EndsWith(",") || addComma) && !profile.GapSuffixReplaceComma ? "," : "") + gapAddEnd;
                }
            }
            else
            {
                // Make new last word
                string addEnd = (profile.SuffixAddSpace ? " " : "") + profile.Suffix;

                if (addEnd.Length == 0 || !newLastWord.EndsWith(addEnd))
                {
                    newLastWord = newLastWord.TrimEnd(',') + ((lastWord.EndsWith(",") || addComma) && !profile.SuffixReplaceComma ? "," : "") + addEnd;
                }
            }

            // Check if it's not surrounded by HTML tags, then we'll place it outside the tags (remove comma if present)
            // Only if it's not a tag across the whole subtitle.
            var wordIndex = originalText.LastIndexOf(lastWord.TrimEnd(','), StringComparison.Ordinal);

            if (wordIndex >= 0 && wordIndex < originalText.Length - 3)
            {
                int currentIndex = wordIndex + lastWord.TrimEnd(',').Length;
                if (((currentIndex < originalText.Length && originalText[currentIndex] == '<') ||
                     (currentIndex + 1 < originalText.Length && originalText[currentIndex + 1] == ',' && originalText[currentIndex] == '<')) &&
                    !IsFullLineTag(originalText, currentIndex))
                {
                    if (currentIndex < originalText.Length && originalText[currentIndex] == ',')
                    {
                        originalText = originalText.Remove(currentIndex, 1);
                    }

                    while (currentIndex + 1 < originalText.Length && !(originalText[currentIndex] == '>' && originalText[currentIndex + 1] != '<'))
                    {
                        currentIndex++;
                    }

                    var suffix = newLastWord.Replace(lastWord.TrimEnd(','), "");

                    if (currentIndex + 1 < originalText.Length && originalText[currentIndex + 1] == ',')
                    {
                        originalText = originalText.Remove(currentIndex + 1, 1);
                    }

                    return(originalText.Insert(currentIndex + 1, suffix));
                }
            }

            // Replace it
            return(ReplaceLastOccurrence(originalText, lastWord, newLastWord));
        }
 public static bool ShouldAddSuffix(string input, ContinuationProfile profile)
 {
     return(ShouldAddSuffix(input, profile, true));
 }
 public static string RemoveSuffix(string originalText, ContinuationProfile profile)
 {
     return(RemoveSuffix(originalText, profile, new List <string>(), false));
 }
        public static Tuple <string, string> MergeHelper(string input, string nextInput, ContinuationProfile profile, string language)
        {
            var thisText         = SanitizeString(input);
            var nextText         = SanitizeString(nextInput);
            var nextTextWithDash = SanitizeString(nextInput, false);

            // Remove any prefix and suffix when:
            // - Title 1 ends with a suffix AND title 2 starts with a prefix
            // - Title 2 is a continuing sentence
            if (HasSuffix(thisText, profile) && HasPrefix(nextTextWithDash, profile) ||
                !IsNewSentence(nextText) && !string.IsNullOrEmpty(nextText))
            {
                var newNextText = RemoveAllPrefixes(nextInput, profile);
                var newText     = RemoveSuffix(input, profile, StartsWithConjunction(newNextText, language));
                return(new Tuple <string, string>(newText, newNextText));
            }

            return(new Tuple <string, string>(input, nextInput));
        }