Skip to main content
Updated to modern C#.
Source Link
Jesse C. Slicer
  • 14.6k
  • 1
  • 40
  • 54

I think your method is pretty readable and clean as it stands. However, if you want to simplify the method itself, I have a decent support structure that would do so and possibly speed it up for particularly long strings:

internal static partial class SafeTag
{
    private static readonly List<Replacement>IEnumerable<Replacement> replacements_Replacements = new List<Replacement>
    {[
        new Replacement("'", "`"),
        new Replacement("\"", "`"),
        new Replacement("&", "and"),
        new Replacement(",", ":"),
        new Replacement("\\", "/"),
    };];

    private static readonly Regex spaces_Spaces = new RegexSpaces("\\s+", RegexOptions.Compiled);

    public static string GetSafeTagName(this string tag)
    {
        varStringBuilder parse = new StringBuilder(tag.ToUpper()); 

        replacementsparse = _Replacements.ForEachAggregate(replacement 
 => parse =         parse,
            (current, replacement) => current.Replace(replacement.Original, replacement.ToReplaceWith));
        return spaces_Spaces.Replace(parse.ToString(), " ");
    }

    private struct Replacement
    {
        private readonly string original;

        private readonly string toReplaceWith;

        internalstruct Replacement(string original, string toReplaceWith)
        {
            this.original = original;
            this.toReplaceWith = toReplaceWith;
        }

        internalpublic string Original
        {
            get
            {
                return this.original;
           get; }
       = }original;

        internalpublic string ToReplaceWith
        {
         get; } = gettoReplaceWith;
            {}
                return this.toReplaceWith;
           [GeneratedRegex("\\s+", }RegexOptions.Compiled)]
        }
private static partial Regex }Spaces();
}

I think your method is pretty readable and clean as it stands. However, if you want to simplify the method itself, I have a decent support structure that would do so and possibly speed it up for particularly long strings:

internal static class SafeTag
{
    private static readonly List<Replacement> replacements = new List<Replacement>
    {
        new Replacement("'", "`"),
        new Replacement("\"", "`"),
        new Replacement("&", "and"),
        new Replacement(",", ":"),
        new Replacement("\\", "/")
    };

    private static readonly Regex spaces = new Regex("\\s+", RegexOptions.Compiled);

    public static string GetSafeTagName(this string tag)
    {
        var parse = new StringBuilder(tag.ToUpper());
        replacements.ForEach(replacement => parse = parse.Replace(replacement.Original, replacement.ToReplaceWith));
        return spaces.Replace(parse.ToString(), " ");
    }

    private struct Replacement
    {
        private readonly string original;

        private readonly string toReplaceWith;

        internal Replacement(string original, string toReplaceWith)
        {
            this.original = original;
            this.toReplaceWith = toReplaceWith;
        }

        internal string Original
        {
            get
            {
                return this.original;
            }
        }

        internal string ToReplaceWith
        {
            get
            {
                return this.toReplaceWith;
            }
        }
    }
}

I think your method is pretty readable and clean as it stands. However, if you want to simplify the method itself, I have a decent support structure that would do so and possibly speed it up for particularly long strings:

internal static partial class SafeTag
{
    private static readonly IEnumerable<Replacement> _Replacements =
    [
        new Replacement("'", "`"),
        new Replacement("\"", "`"),
        new Replacement("&", "and"),
        new Replacement(",", ":"),
        new Replacement("\\", "/"),
    ];

    private static readonly Regex _Spaces = Spaces();

    public static string GetSafeTagName(this string tag)
    {
        StringBuilder parse = new(tag.ToUpper()); 

        parse = _Replacements.Aggregate( 
            parse,
            (current, replacement) => current.Replace(replacement.Original, replacement.ToReplaceWith));
        return _Spaces.Replace(parse.ToString(), " ");
    }

    private readonly struct Replacement(string original, string toReplaceWith)
    {
        public string Original { get; } = original;

        public string ToReplaceWith { get; } = toReplaceWith;
    }

    [GeneratedRegex("\\s+", RegexOptions.Compiled)]
    private static partial Regex Spaces();
}
Source Link
Jesse C. Slicer
  • 14.6k
  • 1
  • 40
  • 54

I think your method is pretty readable and clean as it stands. However, if you want to simplify the method itself, I have a decent support structure that would do so and possibly speed it up for particularly long strings:

internal static class SafeTag
{
    private static readonly List<Replacement> replacements = new List<Replacement>
    {
        new Replacement("'", "`"),
        new Replacement("\"", "`"),
        new Replacement("&", "and"),
        new Replacement(",", ":"),
        new Replacement("\\", "/")
    };

    private static readonly Regex spaces = new Regex("\\s+", RegexOptions.Compiled);

    public static string GetSafeTagName(this string tag)
    {
        var parse = new StringBuilder(tag.ToUpper());
        replacements.ForEach(replacement => parse = parse.Replace(replacement.Original, replacement.ToReplaceWith));
        return spaces.Replace(parse.ToString(), " ");
    }

    private struct Replacement
    {
        private readonly string original;

        private readonly string toReplaceWith;

        internal Replacement(string original, string toReplaceWith)
        {
            this.original = original;
            this.toReplaceWith = toReplaceWith;
        }

        internal string Original
        {
            get
            {
                return this.original;
            }
        }

        internal string ToReplaceWith
        {
            get
            {
                return this.toReplaceWith;
            }
        }
    }
}