#include "path.h"

std::string
concat_path(std::string dirname, std::string basename) {
  // Exceptional cases (function was called incorrectly).
  if (basename.size() == 0) {
    return dirname;
  }
  if (dirname.size() == 0) {
    return basename;
  }

  // Normal case.

  // Multiple slashes are considered to be the same as one slash,
  // except in the case of exactly two loading slashes.  See
  // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271
  // as well as http://pubs.opengroup.org/onlinepubs/9699919799/
  // (section 4.13 Pathname Resolution).
  if (dirname.find_first_not_of("/") == std::string::npos) {
    return dirname + basename.substr(basename.find_first_not_of('/'));
  }
  return dirname + "/" + basename;
}
