From 8bc29c49288da392551613371f96d0f5b57ac821 Mon Sep 17 00:00:00 2001 From: Oleksii Holub Date: Wed, 14 Nov 2018 17:51:03 +0200 Subject: [PATCH] Refactor SplitIntoPartitions --- DiscordChatExporter.Core/Models/Extensions.cs | 42 ------------------- .../Services/ExportService.cs | 30 ++++++++++++- 2 files changed, 29 insertions(+), 43 deletions(-) diff --git a/DiscordChatExporter.Core/Models/Extensions.cs b/DiscordChatExporter.Core/Models/Extensions.cs index 70efb2f..90817b9 100644 --- a/DiscordChatExporter.Core/Models/Extensions.cs +++ b/DiscordChatExporter.Core/Models/Extensions.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; namespace DiscordChatExporter.Core.Models { @@ -33,45 +31,5 @@ namespace DiscordChatExporter.Core.Models throw new ArgumentOutOfRangeException(nameof(format)); } - - public static IReadOnlyList SplitIntoPartitions(this ChatLog chatLog, int partitionLimit) - { - // If chat log has fewer messages than the limit - just return chat log in a list - if (chatLog.Messages.Count <= partitionLimit) - return new[] {chatLog}; - - var result = new List(); - - // Loop through messages - var buffer = new List(); - foreach (var message in chatLog.Messages) - { - // Add message to buffer - buffer.Add(message); - - // If reached the limit - split and reset buffer - if (buffer.Count >= partitionLimit) - { - // Add to result - var chatLogPartition = new ChatLog(chatLog.Guild, chatLog.Channel, chatLog.From, chatLog.To, buffer, - chatLog.Mentionables); - result.Add(chatLogPartition); - - // Reset the buffer instead of clearing to avoid mutations on existing references - buffer = new List(); - } - } - - // Add what's remaining in buffer - if (buffer.Any()) - { - // Add to result - var chatLogPartition = new ChatLog(chatLog.Guild, chatLog.Channel, chatLog.From, chatLog.To, buffer, - chatLog.Mentionables); - result.Add(chatLogPartition); - } - - return result; - } } } \ No newline at end of file diff --git a/DiscordChatExporter.Core/Services/ExportService.cs b/DiscordChatExporter.Core/Services/ExportService.cs index c3604e7..4b68fae 100644 --- a/DiscordChatExporter.Core/Services/ExportService.cs +++ b/DiscordChatExporter.Core/Services/ExportService.cs @@ -83,6 +83,34 @@ namespace DiscordChatExporter.Core.Services } } + private IReadOnlyList SplitIntoPartitions(ChatLog chatLog, int partitionLimit) + { + var result = new List(); + + // Loop through all messages with an increment of partition limit + for (var i = 0; i < chatLog.Messages.Count; i += partitionLimit) + { + // Calculate how many messages left in total + var remainingMessageCount = chatLog.Messages.Count - i; + + // Decide how many messages are going into this partition + // Each partition will have the same number of messages except the last one that might have fewer (all remaining messages) + var partitionMessageCount = partitionLimit.ClampMax(remainingMessageCount); + + // Get messages that belong to this partition + var partitionMessages = new List(); + for (var j = i; j < i + partitionMessageCount; j++) + partitionMessages.Add(chatLog.Messages[j]); + + // Create a partition and add to list + var partition = new ChatLog(chatLog.Guild, chatLog.Channel, chatLog.From, chatLog.To, partitionMessages, + chatLog.Mentionables); + result.Add(partition); + } + + return result; + } + public void ExportChatLog(ChatLog chatLog, string filePath, ExportFormat format, int? partitionLimit = null) { @@ -94,7 +122,7 @@ namespace DiscordChatExporter.Core.Services // Otherwise split into partitions and export separately else { - var partitions = chatLog.SplitIntoPartitions(partitionLimit.Value); + var partitions = SplitIntoPartitions(chatLog, partitionLimit.Value); ExportChatLogPartitions(partitions, filePath, format); } }