<commit_before>/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "video_engine/test/auto_test/primitives/choice_helpers.h"

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

namespace webrtc {

ChoiceBuilder::ChoiceBuilder(const std::string& title, const Choices& choices)
    : choices_(choices),
      input_helper_(TypedInput(title)) {
  input_helper_.WithInputValidator(
      new IntegerWithinRangeValidator(1, choices.size()));
  input_helper_.WithAdditionalInfo(MakeHumanReadableOptions());
}

int ChoiceBuilder::Choose() {
  std::string input = input_helper_.AskForInput();
  return atoi(input.c_str());
}

ChoiceBuilder& ChoiceBuilder::WithDefault(const std::string& default_choice) {
  Choices::const_iterator iterator = std::find(
      choices_.begin(), choices_.end(), default_choice);
  assert(iterator != choices_.end() && "No such choice.");

  // Store the value as the choice number, e.g. its index + 1.
  int choice_index = (iterator - choices_.begin()) + 1;
  char number[16];
  sprintf(number, "%d", choice_index);

  input_helper_.WithDefault(number);
  return *this;
}

ChoiceBuilder& ChoiceBuilder::WithInputSource(FILE* input_source) {
  input_helper_.WithInputSource(input_source);
  return *this;
}

std::string ChoiceBuilder::MakeHumanReadableOptions() {
  std::string result = "";
  Choices::const_iterator iterator = choices_.begin();
  for (int number = 1; iterator != choices_.end(); ++iterator, ++number) {
    char buffer[128];
    sprintf(buffer, "\n  %d. %s", number, (*iterator).c_str());
    result += buffer;
  }
  return result;
}

Choices SplitChoices(const std::string& raw_choices) {
  return Split(raw_choices, "\n");
}

ChoiceBuilder FromChoices(
    const std::string& title, const std::string& raw_choices) {<commit_msg>Fix for buffer overflow, WebRTC issue 1196 Review URL: https://webrtc-codereview.appspot.com/998004<commit_after>/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "video_engine/test/auto_test/primitives/choice_helpers.h"

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <sstream>

namespace webrtc {

ChoiceBuilder::ChoiceBuilder(const std::string& title, const Choices& choices)
    : choices_(choices),
      input_helper_(TypedInput(title)) {
  input_helper_.WithInputValidator(
      new IntegerWithinRangeValidator(1, choices.size()));
  input_helper_.WithAdditionalInfo(MakeHumanReadableOptions());
}

int ChoiceBuilder::Choose() {
  std::string input = input_helper_.AskForInput();
  return atoi(input.c_str());
}

ChoiceBuilder& ChoiceBuilder::WithDefault(const std::string& default_choice) {
  Choices::const_iterator iterator = std::find(
      choices_.begin(), choices_.end(), default_choice);
  assert(iterator != choices_.end() && "No such choice.");

  // Store the value as the choice number, e.g. its index + 1.
  int choice_index = (iterator - choices_.begin()) + 1;
  char number[16];
  sprintf(number, "%d", choice_index);

  input_helper_.WithDefault(number);
  return *this;
}

ChoiceBuilder& ChoiceBuilder::WithInputSource(FILE* input_source) {
  input_helper_.WithInputSource(input_source);
  return *this;
}

std::string ChoiceBuilder::MakeHumanReadableOptions() {
  std::string result = "";
  Choices::const_iterator iterator = choices_.begin();
  for (int number = 1; iterator != choices_.end(); ++iterator, ++number) {
    std::ostringstream os;
    os << "\n  " << number << ". " << (*iterator).c_str();
    result += os.str();
  }
  return result;
}

Choices SplitChoices(const std::string& raw_choices) {
  return Split(raw_choices, "\n");
}

ChoiceBuilder FromChoices(
    const std::string& title, const std::string& raw_choices) {
{'fraction_non_alphanumeric': 0.12559081701553004, 'fraction_numerical': 0.007427413909520594, 'mean_word_length': 4.832020997375328, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '933554', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1327, 'n_words': 383}