```ts
/** Prune a file listing with ordered drop and keep rules. */

function segmentFits(pattern: string, text: string): boolean {
  let p = 0;
  let t = 0;
  let star = -1;
  let mark = 0;
  while (t < text.length) {
    if (p < pattern.length && pattern[p] === "*") {
      star = p;
      mark = t;
      p += 1;
    } else if (p < pattern.length && pattern[p] === text[t]) {
      p += 1;
      t += 1;
    } else if (star >= 0) {
      p = star + 1;
      mark += 1;
      t = mark;
    } else {
      return false;
    }
  }
  while (p < pattern.length && pattern[p] === "*") {
    p += 1;
  }
  return p === pattern.length;
}

function splitStrict(text: string, what: string): string[] {
  if (typeof text !== "string" || text === "") {
    throw new Error(`${what} must be a non-empty string`);
  }
  const segments = text.split("/");
  if (segments.some((segment) => segment === "")) {
    throw new Error(`${what} has an empty segment: ${text}`);
  }
  return segments;
}

export function pruneListing(listing: string[], rules: string[]): string[] {
  for (const path of listing) {
    splitStrict(path, "path");
  }
  for (const rule of rules) {
    splitStrict(rule, "rule");
  }

  const result = [];
  for (const path of listing) {
    let matchesKeepRule = false;
    for (const rule of rules) {
      if (rule.startsWith("!")) {
        if (segmentFits(rule.substring(1), path)) {
          continue; // Negated keep rule
        }
      } else {
        if (segmentFits(rule, path)) {
          matchesKeepRule = true;
          break; // Matching keep rule
        }
      }
    }
    if (matchesKeepRule) {
      result.push(path);
    }
  }

  return result;
}
```