Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Forked from terenced/string-format-extension.cs
Last active December 17, 2015 10:19

Revisions

  1. ramonsmits revised this gist May 16, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions string-format-extension.cs
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ namespace StringExtensions
    /// </remarks>
    public static class JamesFormatter
    {
    static readonly Regex NamedFormatRegex = new Regex(@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
    static readonly Regex NamedFormatRegex = new Regex(@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<align>,\-?\d+)?(?<format>:[^}]+)?(?<end>\})+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

    public static string FormatWith(this string format, object source)
    {
    @@ -29,6 +29,7 @@ public static string FormatWith(this string format, IFormatProvider provider, ob
    {
    Group startGroup = m.Groups["start"];
    Group propertyGroup = m.Groups["property"];
    Group alignGroup = m.Groups["align"];
    Group formatGroup = m.Groups["format"];
    Group endGroup = m.Groups["end"];

    @@ -37,7 +38,7 @@ public static string FormatWith(this string format, IFormatProvider provider, ob
    int openings = startGroup.Captures.Count;
    int closings = endGroup.Captures.Count;

    return openings > closings || openings % 2 == 0 ? m.Value : new string('{', openings) + (values.Count - 1) + formatGroup.Value + new string('}', closings);
    return openings > closings || openings % 2 == 0 ? m.Value : new string('{', openings) + (values.Count - 1) + alignGroup.Value + formatGroup.Value + new string('}', closings);
    });

    return string.Format(provider, rewrittenFormat, values.ToArray());
  2. ramonsmits revised this gist May 16, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions string-format-extension.cs
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@

    namespace StringExtensions
    {

    /// <remarks>
    /// http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
    /// http://james.newtonking.com/archive/2008/03/29/formatwith-2-0-string-formatting-with-named-variables.aspx
    @@ -35,7 +34,10 @@ public static string FormatWith(this string format, IFormatProvider provider, ob

    values.Add((propertyGroup.Value == "0") ? source : Eval(source, propertyGroup.Value));

    return new string('{', startGroup.Captures.Count) + (values.Count - 1) + formatGroup.Value + new string('}', endGroup.Captures.Count);
    int openings = startGroup.Captures.Count;
    int closings = endGroup.Captures.Count;

    return openings > closings || openings % 2 == 0 ? m.Value : new string('{', openings) + (values.Count - 1) + formatGroup.Value + new string('}', closings);
    });

    return string.Format(provider, rewrittenFormat, values.ToArray());
  3. ramonsmits revised this gist May 16, 2013. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion string-format-extension.cs
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,16 @@
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;

    namespace StringExtensions
    {

    /// <remarks>
    /// http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
    /// http://james.newtonking.com/archive/2008/03/29/formatwith-2-0-string-formatting-with-named-variables.aspx
    /// http://haacked.com/archive/2009/01/14/named-formats-redux.aspx
    /// </remarks>
    public static class JamesFormatter
    {
    @@ -30,12 +33,24 @@ public static string FormatWith(this string format, IFormatProvider provider, ob
    Group formatGroup = m.Groups["format"];
    Group endGroup = m.Groups["end"];

    values.Add((propertyGroup.Value == "0") ? source : DataBinder.Eval(source, propertyGroup.Value));
    values.Add((propertyGroup.Value == "0") ? source : Eval(source, propertyGroup.Value));

    return new string('{', startGroup.Captures.Count) + (values.Count - 1) + formatGroup.Value + new string('}', endGroup.Captures.Count);
    });

    return string.Format(provider, rewrittenFormat, values.ToArray());
    }

    private static object Eval(object source, string expression)
    {
    try
    {
    return DataBinder.Eval(source, expression);
    }
    catch (HttpException e)
    {
    throw new FormatException(null, e);
    }
    }
    }
    }
  4. ramonsmits revised this gist May 16, 2013. 1 changed file with 1 addition and 13 deletions.
    14 changes: 1 addition & 13 deletions string-format-extension.cs
    Original file line number Diff line number Diff line change
    @@ -9,22 +9,10 @@ namespace StringExtensions
    /// http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
    /// http://james.newtonking.com/archive/2008/03/29/formatwith-2-0-string-formatting-with-named-variables.aspx
    /// </remarks>
    public static class StringFormatExtension
    public static class JamesFormatter
    {
    static readonly Regex NamedFormatRegex = new Regex(@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

    public static string FormatWith(this string format, params object[] args)
    {
    if (format == null) throw new ArgumentNullException("format");
    return string.Format(format, args);
    }

    public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
    {
    if (format == null) throw new ArgumentNullException("format");
    return string.Format(provider, format, args);
    }

    public static string FormatWith(this string format, object source)
    {
    return FormatWith(format, null, source);
  5. ramonsmits revised this gist May 16, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion string-format-extension.cs
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@

    namespace StringExtensions
    {
    /// <remarks>
    /// <remarks>
    /// http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
    /// http://james.newtonking.com/archive/2008/03/29/formatwith-2-0-string-formatting-with-named-variables.aspx
    /// </remarks>
  6. ramonsmits revised this gist May 16, 2013. 1 changed file with 47 additions and 54 deletions.
    101 changes: 47 additions & 54 deletions string-format-extension.cs
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,53 @@
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Web.UI;

    namespace Helpers
    namespace StringExtensions
    {
    public static class StringFormatExtension
    {
    // http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
    public static string FormatWith(this string format, params object[] args)
    {
    if (format == null)
    throw new ArgumentNullException("format");

    return string.Format(format, args);
    }

    public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
    {
    if (format == null)
    throw new ArgumentNullException("format");

    return string.Format(provider, format, args);
    }

    // http://james.newtonking.com/archive/2008/03/29/formatwith-2-0-string-formatting-with-named-variables.aspx
    public static string FormatWith(this string format, object source)
    {
    return FormatWith(format, null, source);
    }

    public static string FormatWith(this string format, IFormatProvider provider, object source)
    {
    if (format == null)
    throw new ArgumentNullException("format");

    var r = new Regex(@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+",
    RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

    var values = new List<object>();
    string rewrittenFormat = r.Replace(format, delegate(Match m)
    {
    Group startGroup = m.Groups["start"];
    Group propertyGroup = m.Groups["property"];
    Group formatGroup = m.Groups["format"];
    Group endGroup = m.Groups["end"];

    values.Add((propertyGroup.Value == "0")
    ? source
    : DataBinder.Eval(source, propertyGroup.Value));

    return new string('{', startGroup.Captures.Count) + (values.Count - 1) + formatGroup.Value
    + new string('}', endGroup.Captures.Count);
    });

    return string.Format(provider, rewrittenFormat, values.ToArray());
    }

    }
    /// <remarks>
    /// http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
    /// http://james.newtonking.com/archive/2008/03/29/formatwith-2-0-string-formatting-with-named-variables.aspx
    /// </remarks>
    public static class StringFormatExtension
    {
    static readonly Regex NamedFormatRegex = new Regex(@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

    public static string FormatWith(this string format, params object[] args)
    {
    if (format == null) throw new ArgumentNullException("format");
    return string.Format(format, args);
    }

    public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
    {
    if (format == null) throw new ArgumentNullException("format");
    return string.Format(provider, format, args);
    }

    public static string FormatWith(this string format, object source)
    {
    return FormatWith(format, null, source);
    }

    public static string FormatWith(this string format, IFormatProvider provider, object source)
    {
    if (format == null) throw new ArgumentNullException("format");

    var values = new List<object>();
    string rewrittenFormat = NamedFormatRegex.Replace(format, delegate(Match m)
    {
    Group startGroup = m.Groups["start"];
    Group propertyGroup = m.Groups["property"];
    Group formatGroup = m.Groups["format"];
    Group endGroup = m.Groups["end"];

    values.Add((propertyGroup.Value == "0") ? source : DataBinder.Eval(source, propertyGroup.Value));

    return new string('{', startGroup.Captures.Count) + (values.Count - 1) + formatGroup.Value + new string('}', endGroup.Captures.Count);
    });

    return string.Format(provider, rewrittenFormat, values.ToArray());
    }
    }
    }
  7. @terenced terenced renamed this gist Mar 27, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @terenced terenced created this gist Mar 27, 2013.
    60 changes: 60 additions & 0 deletions string-format-extension
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    namespace Helpers
    {
    public static class StringFormatExtension
    {
    // http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx
    public static string FormatWith(this string format, params object[] args)
    {
    if (format == null)
    throw new ArgumentNullException("format");

    return string.Format(format, args);
    }

    public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
    {
    if (format == null)
    throw new ArgumentNullException("format");

    return string.Format(provider, format, args);
    }

    // http://james.newtonking.com/archive/2008/03/29/formatwith-2-0-string-formatting-with-named-variables.aspx
    public static string FormatWith(this string format, object source)
    {
    return FormatWith(format, null, source);
    }

    public static string FormatWith(this string format, IFormatProvider provider, object source)
    {
    if (format == null)
    throw new ArgumentNullException("format");

    var r = new Regex(@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+",
    RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

    var values = new List<object>();
    string rewrittenFormat = r.Replace(format, delegate(Match m)
    {
    Group startGroup = m.Groups["start"];
    Group propertyGroup = m.Groups["property"];
    Group formatGroup = m.Groups["format"];
    Group endGroup = m.Groups["end"];

    values.Add((propertyGroup.Value == "0")
    ? source
    : DataBinder.Eval(source, propertyGroup.Value));

    return new string('{', startGroup.Captures.Count) + (values.Count - 1) + formatGroup.Value
    + new string('}', endGroup.Captures.Count);
    });

    return string.Format(provider, rewrittenFormat, values.ToArray());
    }

    }
    }