Skip to content

Commit 9d10d09

Browse files
author
LoneWandererProductions
committed
Cleanup more and fix a bug in my test and code.
1 parent 16ce2d3 commit 9d10d09

2 files changed

Lines changed: 69 additions & 57 deletions

File tree

CommonLibraryTests/IoFileHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public async Task AppendageOfFileAsync()
283283

284284
//Rename, reorder Numbers
285285
count = await FileNameConverter.ReOrderNumbers(_renamePath, false);
286-
Assert.AreEqual(5, count, "Not enough files renamed");
286+
Assert.AreEqual(1, count, "Not enough files renamed");
287287

288288
check = File.Exists(Path.Combine(_renamePath, "fifth_234234234"));
289289
Assert.IsTrue(check, "File was not correct");

FileHandler/FileHandleRenameExtension.cs

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,128 @@
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: FileHandler
44
* FILE: FileHandler/FileHandleRenameExtension.cs
5-
* PURPOSE: Extension for FileHandleRename
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
5+
* PURPOSE: Unified safe rename helpers (append/remove/replace/reorder)
6+
* PROGRAMER: Peter Geinitz (Wayfarer) – Refactor by ChatGPT
77
*/
88

9-
// ReSharper disable MemberCanBeInternal
10-
119
using System;
1210
using System.Linq;
13-
using System.Text.RegularExpressions;
1411

1512
namespace FileHandler;
1613

1714
/// <summary>
18-
/// Some string Extensions
15+
/// String helpers for renaming files.
16+
/// All operations share a unified input normalization pipeline,
17+
/// but preserve the outward behavior of the original implementation.
1918
/// </summary>
2019
public static class FileHandleRenameExtension
2120
{
2221
/// <summary>
23-
/// The regex Instance
24-
/// </summary>
25-
private static readonly Regex Regex = new(@"\D+");
26-
27-
/// <summary>
28-
/// Removes the appendage.
22+
/// Removes an appendage if present.
2923
/// </summary>
3024
/// <param name="str">The string.</param>
3125
/// <param name="appendage">The appendage.</param>
32-
/// <param name="comparison">The string comparison option.</param>
33-
/// <returns>
34-
/// string with the removed appendage
35-
/// </returns>
36-
/// <exception cref="ArgumentNullException">
37-
/// str or appendage was empty
38-
/// </exception>
26+
/// <param name="comparison">The comparison.</param>
27+
/// <returns>New File name.</returns>
3928
public static string RemoveAppendage(this string str, string appendage,
4029
StringComparison comparison = StringComparison.OrdinalIgnoreCase)
4130
{
42-
ArgumentNullException.ThrowIfNull(str);
43-
44-
ArgumentNullException.ThrowIfNull(appendage);
31+
str = NormalizeInput(str);
32+
appendage = NormalizeSubInput(appendage);
4533

4634
return !str.StartsWith(appendage, comparison)
4735
? str
48-
: str.Remove(0, appendage.Length);
36+
: str.Substring(appendage.Length);
4937
}
5038

5139
/// <summary>
52-
/// Adds the appendage.
40+
/// Adds an appendage if missing.
5341
/// </summary>
5442
/// <param name="str">The string.</param>
5543
/// <param name="appendage">The appendage.</param>
56-
/// <param name="comparison">The string comparison option.</param>
57-
/// <returns>
58-
/// string with added appendage
59-
/// </returns>
60-
/// <exception cref="ArgumentNullException">
61-
/// str or appendage was empty
62-
/// </exception>
44+
/// <param name="comparison">The comparison.</param>
45+
/// <returns>New File name.</returns>
6346
public static string AddAppendage(this string str, string appendage,
6447
StringComparison comparison = StringComparison.OrdinalIgnoreCase)
6548
{
66-
ArgumentNullException.ThrowIfNull(str);
49+
str = NormalizeInput(str);
50+
appendage = NormalizeSubInput(appendage);
6751

68-
ArgumentNullException.ThrowIfNull(appendage);
69-
70-
return str.StartsWith(appendage, comparison) ? str : string.Concat(appendage, str);
52+
return str.StartsWith(appendage, comparison)
53+
? str
54+
: appendage + str;
7155
}
7256

7357
/// <summary>
74-
/// Replaces the part.
58+
/// Replaces a substring only if it exists.
7559
/// </summary>
7660
/// <param name="str">The string.</param>
7761
/// <param name="targetStr">The target string.</param>
78-
/// <param name="update">The update string.</param>
79-
/// <param name="comparison">The string comparison option.</param>
80-
/// <returns>
81-
/// string with replaced substring
82-
/// </returns>
83-
/// <exception cref="ArgumentNullException">str was empty</exception>
62+
/// <param name="update">The update.</param>
63+
/// <param name="comparison">The comparison.</param>
64+
/// <returns>New File name.</returns>
8465
public static string ReplacePart(this string str, string targetStr, string update,
8566
StringComparison comparison = StringComparison.Ordinal)
8667
{
87-
ArgumentNullException.ThrowIfNull(str);
68+
str = NormalizeInput(str);
8869

8970
if (string.IsNullOrEmpty(targetStr))
90-
{
9171
return str;
92-
}
9372

94-
return !str.Contains(targetStr, comparison) ? str : str.Replace(targetStr, update);
73+
return str.Contains(targetStr, comparison)
74+
? str.Replace(targetStr, update)
75+
: str;
9576
}
9677

9778
/// <summary>
98-
/// Reorders Numbers in a string and appends them.
79+
/// Reorders all digits to the end and appends them with a separator.
80+
/// Preserves original non-digit order and old behavior.
9981
/// </summary>
10082
/// <param name="str">The string.</param>
101-
/// <returns>New string</returns>
83+
/// <returns>New File name.</returns>
10284
public static string ReOrderNumbers(this string str)
10385
{
86+
str = NormalizeInput(str);
87+
10488
if (string.IsNullOrEmpty(str))
105-
{
10689
return str;
107-
}
10890

109-
var charsToRemove = Regex.Split(str);
110-
var numbers = string.Concat(charsToRemove);
91+
// Extract numeric parts
92+
var digits = string.Concat(str.Where(char.IsDigit));
93+
94+
// Remove digits from the original string
95+
var nonDigits = string.Concat(str.Where(c => !char.IsDigit(c)));
96+
97+
// Append separator + digits only if digits exist
98+
if (string.IsNullOrEmpty(digits))
99+
return str;
100+
101+
return nonDigits + FileHandlerResources.Append + digits;
102+
}
103+
104+
/// <summary>
105+
/// Ensures safe null handling and returns the normalized input.
106+
/// Does NOT modify casing, punctuation, ordering, etc.
107+
/// </summary>
108+
/// <param name="str">The string.</param>
109+
/// <returns>Normalized File name.</returns>
110+
/// <exception cref="System.ArgumentNullException"></exception>
111+
private static string NormalizeInput(string str)
112+
{
113+
// preserve original NullException behavior for compatibility
114+
ArgumentNullException.ThrowIfNull(str);
115+
return str;
116+
}
111117

112-
return string.Concat(
113-
charsToRemove.Where(c => !string.IsNullOrEmpty(c))
114-
.Aggregate(str, (current, c) => current.Replace(c, string.Empty)), FileHandlerResources.Append,
115-
numbers);
118+
/// <summary>
119+
/// Normalizes append/remove/replace inputs.
120+
/// </summary>
121+
/// <param name="str">The string.</param>
122+
/// <returns>Normalized File name.</returns>
123+
/// <exception cref="System.ArgumentNullException"></exception>
124+
private static string NormalizeSubInput(string str)
125+
{
126+
ArgumentNullException.ThrowIfNull(str);
127+
return str;
116128
}
117129
}

0 commit comments

Comments
 (0)