createSegmentsString static method

String createSegmentsString(
  1. String path
)

Split the given path into a list of segments

Implementation

static String createSegmentsString(String path) {
  final sanitized = path
      // Replace escaped backslashes with forward slashes
      .replaceAll(r'\\', '/')
      // Replace unescaped backslashes with forward slashes
      .replaceAll(r'\', '/')
      // Remove leading raw string prefix
      .replaceFirst("r'", "'")
      .replaceFirst('r"', '"');

  return sanitized
      // Strip leading/trailing quotes
      .substring(1, sanitized.length - 1)
      .split('/')
      .skip(path.startsWith('/') ? 1 : 0)
      .map((segment) {
        final si1Match = _stringInterpolation1.firstMatch(segment);
        if (si1Match != null) {
          final content = si1Match[1]!;
          if (si1Match[2] == null) {
            // Strip interpolation
            return content;
          } else {
            // Do not strip interpolation
            return "'$segment'";
          }
        }

        final si2Match = _stringInterpolation2.firstMatch(segment);
        if (si2Match != null) return si2Match[1]!;

        return "'$segment'";
      })
      .join(', ');
}