iterableToShortString static method

String iterableToShortString(
  1. Iterable iterable, [
  2. String leftDelimiter = '(',
  3. String rightDelimiter = ')'
])

Convert an Iterable to a string like Iterable.toString.

Allows using other delimiters than '(' and ')'.

Handles circular references where converting one of the elements to a string ends up converting iterable to a string again.

Implementation

static String iterableToShortString(
  Iterable iterable, [
  String leftDelimiter = '(',
  String rightDelimiter = ')',
]) {
  if (isToStringVisiting(iterable)) {
    if (leftDelimiter == "(" && rightDelimiter == ")") {
      // Avoid creating a new string in the "common" case.
      return "(...)";
    }
    return "$leftDelimiter...$rightDelimiter";
  }
  List<String> parts = <String>[];
  toStringVisiting.add(iterable);
  try {
    _iterablePartsToStrings(iterable, parts);
  } finally {
    assert(identical(toStringVisiting.last, iterable));
    toStringVisiting.removeLast();
  }
  return (StringBuffer(leftDelimiter)
        ..writeAll(parts, ", ")
        ..write(rightDelimiter))
      .toString();
}