<commit_before>//===--- Job.cpp - Command to Execute -------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "swift/Driver/Job.h"

#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
using namespace swift::driver;

JobList::~JobList() {
  if (OwnsJobs) {
    llvm::DeleteContainerPointers(Jobs);
  }
}

void JobList::clear() {
  if (OwnsJobs) {
    llvm::DeleteContainerPointers(Jobs);
  } else {
    // We don't own the pointers, so just clear the list.
    Jobs.clear();
  }
}

void CommandOutput::setAdditionalOutputForType(types::ID type,
                                               StringRef OutputFilename) {
  AdditionalOutputsMap[type] = OutputFilename;
}

const std::string &
CommandOutput::getAdditionalOutputForType(types::ID type) const {
  auto iter = AdditionalOutputsMap.find(type);
  if (iter != AdditionalOutputsMap.end())
    return iter->second;

  static const std::string empty;
  return empty;
}

const std::string &
CommandOutput::getAnyOutputForType(types::ID type) const {
  if (PrimaryOutputType == type)
    return PrimaryOutputFilename;
  return getAdditionalOutputForType(type);
}

static void escapeAndPrintString(llvm::raw_ostream &os, const char *str) {
  StringRef String(str);
  if (String.empty()) {
    // Special-case the empty string.
    os << "\"\"";
    return;
  }

  bool NeedsEscape = String.find_first_of(" \"\\$") != StringRef::npos;

  if (!NeedsEscape) {
    // This string doesn't have anything we need to escape, so print it directly
    os << str;
    return;
  }

  // Quote and escape. This isn't really complete, but is good enough, and
  // matches how Clang's Command handles escaping arguments.
  os << '"';
  for (const char c : String) {
    switch (c) {
      case '"':
      case '\\':
      case '$':
        // These characters need to be escaped.
        os << '\\';
        // Fall-through to the default case, since we still need to print the
        // character.
      default:
        os << c;
    }
  }
  os << '"';

}

void Command::printCommandLine(llvm::raw_ostream &os) const {
  escapeAndPrintString(os, Executable);
  for (const char *Arg : Arguments) {
    os << ' ';
    escapeAndPrintString(os, Arg);
  }
  os << '\n';
}<commit_msg>StringRef'ize escapeAndPrintString<commit_after>//===--- Job.cpp - Command to Execute -------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "swift/Driver/Job.h"

#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
using namespace swift::driver;

JobList::~JobList() {
  if (OwnsJobs) {
    llvm::DeleteContainerPointers(Jobs);
  }
}

void JobList::clear() {
  if (OwnsJobs) {
    llvm::DeleteContainerPointers(Jobs);
  } else {
    // We don't own the pointers, so just clear the list.
    Jobs.clear();
  }
}

void CommandOutput::setAdditionalOutputForType(types::ID type,
                                               StringRef OutputFilename) {
  AdditionalOutputsMap[type] = OutputFilename;
}

const std::string &
CommandOutput::getAdditionalOutputForType(types::ID type) const {
  auto iter = AdditionalOutputsMap.find(type);
  if (iter != AdditionalOutputsMap.end())
    return iter->second;

  static const std::string empty;
  return empty;
}

const std::string &
CommandOutput::getAnyOutputForType(types::ID type) const {
  if (PrimaryOutputType == type)
    return PrimaryOutputFilename;
  return getAdditionalOutputForType(type);
}

static void escapeAndPrintString(llvm::raw_ostream &os, StringRef Str) {
  if (Str.empty()) {
    // Special-case the empty string.
    os << "\"\"";
    return;
  }

  bool NeedsEscape = Str.find_first_of(" \"\\$") != StringRef::npos;

  if (!NeedsEscape) {
    // This string doesn't have anything we need to escape, so print it directly
    os << Str;
    return;
  }

  // Quote and escape. This isn't really complete, but is good enough, and
  // matches how Clang's Command handles escaping arguments.
  os << '"';
  for (const char c : Str) {
    switch (c) {
    case '"':
    case '\\':
    case '$':
      // These characters need to be escaped.
      os << '\\';
      // Fall-through to the default case, since we still need to print the
      // character.
    default:
      os << c;
    }
  }
  os << '"';
}

void Command::printCommandLine(llvm::raw_ostream &os) const {
  escapeAndPrintString(os, Executable);
  for (const char *Arg : Arguments) {
    os << ' ';
    escapeAndPrintString(os, Arg);
  }
  os << '\n';
}
{'fraction_non_alphanumeric': 0.17091648189209166, 'fraction_numerical': 0.003695491500369549, 'mean_word_length': 2.6134846461949266, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7176419', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1596, 'n_words': 462}