looksLikeFilePath function

bool looksLikeFilePath(
  1. String input
)

Check if a string looks like a file path.

Implementation

bool looksLikeFilePath(String input) {
  if (input.isEmpty) return false;
  // Starts with common path prefixes.
  if (input.startsWith('/') ||
      input.startsWith('./') ||
      input.startsWith('~/') ||
      input.startsWith('../')) {
    return true;
  }
  // Contains path separator and has a file extension.
  if (input.contains('/') && RegExp(r'\.\w{1,10}$').hasMatch(input)) {
    return true;
  }
  // Single filename with extension.
  if (RegExp(r'^[\w.-]+\.\w{1,10}$').hasMatch(input)) {
    return true;
  }
  // Starts with common source directories.
  if (RegExp(
    r'^(?:src|lib|test|bin|build|packages?|config)/',
  ).hasMatch(input)) {
    return true;
  }
  return false;
}