private static string FormatImpl(string format, IFormatProvider provider, StringFormatTokenBindingHandler binder, params object[] args) { if (format == null) throw new ArgumentNullException("format"); Debug.Assert(binder != null); // // Following is a slightly modified version of the parser from // Henri Weichers that was presented at: // http://haacked.com/archive/2009/01/14/named-formats-redux.aspx // See also the following comment about the modifications: // http://haacked.com/archive/2009/01/14/named-formats-redux.aspx#70485 // StringBuilder result = new StringBuilder(format.Length * 2); StringBuilder token = new StringBuilder(); CharEnumerator e = format.GetEnumerator(); while (e.MoveNext()) { char ch = e.Current; if (ch == '{') { while (true) { if (!e.MoveNext()) throw new FormatException(); ch = e.Current; if (ch == '}') { if (token.Length == 0) throw new FormatException(); result.Append(binder(token.ToString(), args, provider)); token.Length = 0; break; } if (ch == '{') { result.Append(ch); break; } token.Append(ch); } } else if (ch == '}') { if (!e.MoveNext() || e.Current != '}') throw new FormatException(); result.Append('}'); } else { result.Append(ch); } } return result.ToString(); }
public static string Format(string format, IFormatProvider provider, StringFormatTokenBindingHandler binder, params object[] args) { return FormatImpl(format, provider, binder != null ? binder : DefaultTokenBinder, args); }
public static string Format(string format, StringFormatTokenBindingHandler binder, params object[] args) { return Format(format, null, binder, args); }
private static string FormatImpl(string format, IFormatProvider provider, StringFormatTokenBindingHandler binder, params object[] args) { if (format == null) { throw new ArgumentNullException("format"); } Debug.Assert(binder != null); // // Following is a slightly modified version of the parser from // Henri Weichers that was presented at: // http://haacked.com/archive/2009/01/14/named-formats-redux.aspx // See also the following comment about the modifications: // http://haacked.com/archive/2009/01/14/named-formats-redux.aspx#70485 // var result = new StringBuilder(format.Length * 2); var token = new StringBuilder(); var e = format.GetEnumerator(); while (e.MoveNext()) { var ch = e.Current; if (ch == '{') { while (true) { if (!e.MoveNext()) { throw new FormatException(); } ch = e.Current; if (ch == '}') { if (token.Length == 0) { throw new FormatException(); } result.Append(binder(token.ToString(), args, provider)); token.Length = 0; break; } if (ch == '{') { result.Append(ch); break; } token.Append(ch); } } else if (ch == '}') { if (!e.MoveNext() || e.Current != '}') { throw new FormatException(); } result.Append('}'); } else { result.Append(ch); } } return(result.ToString()); }
public static string Format(string format, IFormatProvider provider, StringFormatTokenBindingHandler binder, params object[] args) { return(FormatImpl(format, provider, binder != null ? binder : DefaultTokenBinder, args)); }
public static string Format(string format, StringFormatTokenBindingHandler binder, params object[] args) { return(Format(format, null, binder, args)); }