using System; using System.Collections.Generic; using System.Text; namespace Semmle.Util { public static class StringBuilderExtensions { /// /// Appends a [comma] separated list to a StringBuilder. /// /// The type of the list. /// The StringBuilder to append to. /// The separator string (e.g. ",") /// The list of items. /// The original StringBuilder (fluent interface). public static StringBuilder AppendList(this StringBuilder builder, string separator, IEnumerable items) { return builder.BuildList(separator, items, (x, sb) => { sb.Append(x); }); } /// /// Builds a string using a separator and an action for each item in the list. /// /// The type of the items. /// The string builder. /// The separator string (e.g. ", ") /// The list of items. /// The action on each item. /// The original StringBuilder (fluent interface). public static StringBuilder BuildList(this StringBuilder builder, string separator, IEnumerable items, Action action) { bool first = true; foreach (var item in items) { if (first) first = false; else builder.Append(separator); action(item, builder); } return builder; } } }