From ab604877be9cf674191b2f39cc22cd9c4ad54dd4 Mon Sep 17 00:00:00 2001 From: adroslice Date: Fri, 22 Nov 2019 19:18:07 +0100 Subject: [PATCH] Added controlled fail conditions for for Number --- BFR/Operations/Number.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/BFR/Operations/Number.cs b/BFR/Operations/Number.cs index d51f34b..e24b98b 100644 --- a/BFR/Operations/Number.cs +++ b/BFR/Operations/Number.cs @@ -1,10 +1,12 @@ -using System.Xml.Serialization; +using System.Linq; +using System.Xml.Serialization; using System.Collections.Generic; using Avalonia; using BFR.Helpers; using BFR.DataModels; +using System; namespace BFR.Operations { @@ -36,6 +38,16 @@ namespace BFR.Operations ? (StartIndex + Increment * (files.Count - 1)).ToString().Length : Padding; + // Fail conditions: Out of bounds insert + var shortestLength = files.Min(x => FullName ? x.FullName.Length : x.Name.Length); + if (Mode == NumberMode.Insert && InsertPosition > shortestLength) throw new OperationException($"First N can't be greater than the shortest file name length ({shortestLength})."); + + // Regex Failure + if (Mode == NumberMode.Replace && UseRegex) + try { "".Replace(Pattern, "", true, false); } + catch (Exception) { throw new OperationException("Invalid Regex Pattern."); } + + // Apply operation foreach (var file in files) { var newName = FullName ? file.FullName : file.Name;