Skip to content

Commit

Permalink
add tests for StringHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Oct 16, 2023
1 parent 3169a73 commit 9248ea7
Show file tree
Hide file tree
Showing 3 changed files with 674 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Serenity.ComponentModel;
namespace Serenity.ComponentModel;

/// <summary>
/// Default implementation for IFilenameFormatSanitizer
Expand All @@ -21,7 +21,7 @@ public class DefaultFilenameFormatSanitizer : IFilenameFormatSanitizer
/// <param name="value">Value to be sanitized</param>
public virtual string SanitizePlaceholder(string _, string? value)
{
value = StringHelper.SanitizeFilename((value ?? "").Replace('\\', '/'));
value = StringHelper.SanitizeFilename((value ?? "").Replace('\\', '/'), removeDiacritics: true);
value = value.Replace("..", "_", StringComparison.Ordinal);
if (string.IsNullOrWhiteSpace(value))
value = "_";
Expand Down
35 changes: 20 additions & 15 deletions src/Serenity.Net.Core/Helpers/StringHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security.Cryptography.X509Certificates;

namespace Serenity;

Expand Down Expand Up @@ -61,6 +62,7 @@ public static bool IsTrimmedEmpty([NotNullWhen(false)] this string? str)

if (str.Length == 0)
return null;

return str;
}

Expand All @@ -80,8 +82,9 @@ public static bool IsTrimmedEmpty([NotNullWhen(false)] this string? str)
/// Trimmed string (result won't be null).</returns>
public static string TrimToEmpty(this string? str)
{
if (string.IsNullOrEmpty(str))
if (string.IsNullOrWhiteSpace(str))
return string.Empty;

return str.Trim();
}

Expand All @@ -101,11 +104,11 @@ public static string TrimToEmpty(this string? str)
/// If two strings are same trimmed, true</returns>
public static bool IsTrimmedSame(this string? string1, string? string2)
{
if ((string1 == null || string1.Length == 0) &&
(string2 == null || string2.Length == 0))
if (string.IsNullOrWhiteSpace(string1) &&
string.IsNullOrWhiteSpace(string2))
return true;
else
return TrimToNull(string1) == TrimToNull(string2);

return TrimToEmpty(string1) == TrimToEmpty(string2);
}

/// <summary>
Expand Down Expand Up @@ -177,7 +180,6 @@ public static string ToDoubleQuoted(this string str)
return sb.ToString();
}


private const string emptySingleQuote = "''";
private const string emptyDoubleQuote = "\"\"";

Expand Down Expand Up @@ -300,7 +302,7 @@ public static string SafeSubstring(this string? value, int startIndex, int maxLe
/// </summary>
/// <param name="filename">The string.</param>
/// <param name="replacement">Replacement string for invalid characters</param>
/// <param name="removeDiacritics">True to remove diacritics</param>
/// <param name="removeDiacritics">True (default) to remove diacritics</param>
/// <exception cref="ArgumentNullException">s is null</exception>
public static string SanitizeFilename(string filename,
string replacement = "_", bool removeDiacritics = true)
Expand All @@ -310,7 +312,7 @@ public static string SanitizeFilename(string filename,

if (removeDiacritics)
{
RemoveDiacritics(filename);
filename = RemoveDiacritics(filename);
filename = filename.Replace("ı", "i");
}

Expand All @@ -323,12 +325,12 @@ public static string SanitizeFilename(string filename,
/// A regex to remove invalid file path characters
/// </summary>
public static readonly Regex InvalidPathCharsRegex =
new($"[{Regex.Escape(new string(Path.GetInvalidPathChars()))}]",
new($"[\\*\\?<>{Regex.Escape(new string(Path.GetInvalidPathChars()))}]",
RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.CultureInvariant);

/// <summary>
/// Sanitizes the path by removing diacritics, ı with i and replacing any
/// invalid file path characters with underscore.
/// Sanitizes the path by removing diacritics (ü with u, ı with i etc.) and
/// replacing any invalid file path characters with underscore.
/// </summary>
/// <param name="filename">The string.</param>
/// <param name="replacement">Replacement string for invalid characters</param>
Expand All @@ -342,7 +344,7 @@ public static string SanitizeFilePath(string filename,

if (removeDiacritics)
{
RemoveDiacritics(filename);
filename = RemoveDiacritics(filename);
filename = filename.Replace("ı", "i");
}

Expand All @@ -354,11 +356,14 @@ public static string SanitizeFilePath(string filename,
/// <summary>
/// Removes the diacritic characters from string by replacing them with ASCII versions.
/// </summary>
/// <param name="s">The string.</param>
/// <param name="str">The string.</param>
/// <returns>String with diacritics replaced.</returns>
public static string RemoveDiacritics(string s)
public static string RemoveDiacritics(string str)
{
var normalizedString = s.Normalize(NormalizationForm.FormKD);
if (str == null)
throw new ArgumentNullException(nameof(str));

var normalizedString = str.Normalize(NormalizationForm.FormKD);
var stringBuilder = new StringBuilder();

for (int i = 0; i < normalizedString.Length; i++)
Expand Down
Loading

0 comments on commit 9248ea7

Please sign in to comment.