More work on #8257

This commit is contained in:
Nikolaj Olsson 2024-04-28 20:36:47 +02:00
parent 6a3ddce7dd
commit 59ab0fca5b
2 changed files with 20 additions and 13 deletions

View File

@ -3,11 +3,11 @@ using Nikse.SubtitleEdit.Core.SubtitleFormats;
using Nikse.SubtitleEdit.Logic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nikse.SubtitleEdit.Core.Enums;
using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox;
namespace Nikse.SubtitleEdit.Forms
@ -94,13 +94,14 @@ namespace Nikse.SubtitleEdit.Forms
private void listViewParts_DragDrop(object sender, DragEventArgs e)
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string fileName in files)
foreach (var fileName in files)
{
if (!_fileNamesToJoin.Any(file => file.Equals(fileName, StringComparison.OrdinalIgnoreCase)))
{
_fileNamesToJoin.Add(fileName);
}
}
SortAndLoad();
}
@ -266,21 +267,16 @@ namespace Nikse.SubtitleEdit.Forms
if (!radioButtonJoinAddTime.Checked)
{
for (int outer = 0; outer < subtitles.Count; outer++)
for (var outer = 0; outer < subtitles.Count; outer++)
{
for (int inner = 1; inner < subtitles.Count; inner++)
for (var inner = 1; inner < subtitles.Count; inner++)
{
var a = subtitles[inner - 1];
var b = subtitles[inner];
if (a.Paragraphs.Count > 0 && b.Paragraphs.Count > 0 && a.Paragraphs[0].StartTime.TotalMilliseconds > b.Paragraphs[0].StartTime.TotalMilliseconds)
{
var t1 = _fileNamesToJoin[inner - 1];
_fileNamesToJoin[inner - 1] = _fileNamesToJoin[inner];
_fileNamesToJoin[inner] = t1;
var t2 = subtitles[inner - 1];
subtitles[inner - 1] = subtitles[inner];
subtitles[inner] = t2;
(_fileNamesToJoin[inner - 1], _fileNamesToJoin[inner]) = (_fileNamesToJoin[inner], _fileNamesToJoin[inner - 1]);
(subtitles[inner - 1], subtitles[inner]) = (subtitles[inner], subtitles[inner - 1]);
}
}
}
@ -331,6 +327,11 @@ namespace Nikse.SubtitleEdit.Forms
}
}
if (radioButtonJoinPlain.Checked)
{
JoinedSubtitle.Sort(SubtitleSortCriteria.StartTime);
}
JoinedSubtitle.Renumber();
labelTotalLines.Text = string.Format(LanguageSettings.Current.JoinSubtitles.TotalNumberOfLinesX, JoinedSubtitle.Paragraphs.Count);
}

View File

@ -1,6 +1,8 @@
using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
@ -13,6 +15,7 @@ namespace Nikse.SubtitleEdit.Logic
public bool IsDisplayFileSize { get; set; }
public bool Descending { get; set; }
private readonly Regex _numbers = new Regex(@"\d+", RegexOptions.Compiled);
private readonly Regex _invariantNumber = new Regex(@"\d+\.{1,2}", RegexOptions.Compiled);
public int Compare(object o1, object o2)
@ -53,7 +56,10 @@ namespace Nikse.SubtitleEdit.Logic
return i1 > i2 ? 1 : i1 == i2 ? 0 : -1;
}
return string.Compare(lvi2.SubItems[ColumnNumber].Text, lvi1.SubItems[ColumnNumber].Text, StringComparison.Ordinal);
// use natural sort order
var str2 = _numbers.Replace(lvi2.SubItems[ColumnNumber].Text, m => m.Value.PadLeft(10, '0')).RemoveChar(' ');
var str1 = _numbers.Replace(lvi1.SubItems[ColumnNumber].Text, m => m.Value.PadLeft(10, '0')).RemoveChar(' ');
return string.Compare(str2, str1, StringComparison.Ordinal);
}
}
}