<commit_before>// Copyright (c) 2011 The Native Client Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

#include "webgtt/parser.h"

#include <boost/bind.hpp>

#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>

#include "webgtt/graph.h"
#include "webgtt/taskmap.h"
#include "webgtt/webgtt.h"

/// The sentinel/delimiter string that is used in the message format.
const char kSentinel[] = "::";

namespace webgtt {

Parser::Parser(const std::string& message)
    : message_(message),
      is_valid_(false),
      task_ID_(kInvalidValue) {}

/// This function decodes the message. A valid message is of the form:
/// "kSentinel<adjacency_matrix_>kSentinel<task_ID_>kSentinel<args_>kSentinel"
/// If at any stage during the decoding process, the message is found to be
/// invalid (not conforming to the above format), parsing is aborted, and the
/// is_valid_ bit contains false. Upon successful completion of the decoding
/// process, the is_valid_ bit is set to true.
bool Parser::DecodeMessage() {
  assert(!is_valid_);

  const size_t message_length = message_.size();
  // We start parsing from the beginning of the message.
  int parse_position = 0;

  // The shortest valid message is for a coloring request on a graph with
  // exactly one vertex, which would look like:
  // "<kSentinel>0<kSentinel>0<kSentinel>"
  if (message_length < 2 + (3 * strlen(kSentinel))) {
    return false;
  }
  // Look for the first sentinel.
  if (message_.substr(0, strlen(kSentinel)).compare(kSentinel) != 0) {
    return false;
  }
  // Skip over the sentinel that we just found, and continue parsing.
  parse_position += strlen(kSentinel);

  std::string adjacency_matrix = GetNextChunk(message_, &parse_position);
  if (adjacency_matrix.compare(kSentinel) == 0) {
    return false;
  }
  // adjacency_matrix should be in CSV format; obtain the positions of commas.
  std::vector<int> comma_positions = GetCommaPositions(adjacency_matrix);
  // There should be a total of (number_of_vertices)^2 - 1 commas, since
  // adjacency_matrix is a square matrix.
  int number_of_vertices = sqrt(comma_positions.size() + 1);
  if (static_cast<int>(comma_positions.size()) !=
      (number_of_vertices * number_of_vertices) - 1) {
    return false;
  }
  // Decode the adjacency matrix
  int adj_position = 0;
  for (int i = 1; i <= number_of_vertices; ++i) {
    std::vector<int> row = DecodeCSV(adjacency_matrix.substr(adj_position,
        comma_positions[(i * number_of_vertices) - 1] - adj_position));
    for (size_t j = 0; j < row.size(); ++j) {
      if (row[j] == kInvalidValue) {
        return false;
      }
    }
    adjacency_matrix_.push_back(row);
    adj_position = comma_positions[(i * number_of_vertices) - 1] + 1;
  }
  comma_positions.clear();
  // Construct the graph
  graph::Graph input_graph(number_of_vertices, adjacency_matrix_);

  std::string task_ID = GetNextChunk(message_, &parse_position);
  if (task_ID.compare(kSentinel) == 0) {
    return false;
  }
  if (StringToInteger(task_ID) == kInvalidValue) {
    return false;
  }
  task_ID_ = StringToInteger(task_ID);

  std::string args = "";
  if (parse_position != static_cast<int>(message_length)) {
    // There may be some arguments.
    args = GetNextChunk(message_, &parse_position);
    if (args.compare(kSentinel) == 0) {
      return false;
    }
  }<commit_msg>WebGTT bugfix. Review URL: http://codereview.chromium.org/7981016<commit_after>// Copyright (c) 2011 The Native Client Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

#include "webgtt/parser.h"

#include <boost/bind.hpp>

#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>

#include "webgtt/graph.h"
#include "webgtt/taskmap.h"
#include "webgtt/webgtt.h"

/// The sentinel/delimiter string that is used in the message format.
const char kSentinel[] = "::";

namespace webgtt {

Parser::Parser(const std::string& message)
    : message_(message),
      is_valid_(false),
      task_ID_(kInvalidValue) {}

/// This function decodes the message. A valid message is of the form:
/// "kSentinel<adjacency_matrix_>kSentinel<task_ID_>kSentinel<args_>kSentinel"
/// If at any stage during the decoding process, the message is found to be
/// invalid (not conforming to the above format), parsing is aborted, and the
/// is_valid_ bit contains false. Upon successful completion of the decoding
/// process, the is_valid_ bit is set to true.
bool Parser::DecodeMessage() {
  assert(!is_valid_);

  const size_t message_length = message_.size();
  // We start parsing from the beginning of the message.
  int parse_position = 0;

  // The shortest valid message is for a coloring request on a graph with
  // exactly one vertex, which would look like:
  // "<kSentinel>0<kSentinel>0<kSentinel>"
  if (message_length < 2 + (3 * strlen(kSentinel))) {
    return false;
  }
  // Look for the first sentinel.
  if (message_.substr(0, strlen(kSentinel)).compare(kSentinel) != 0) {
    return false;
  }
  // Skip over the sentinel that we just found, and continue parsing.
  parse_position += strlen(kSentinel);

  std::string adjacency_matrix = GetNextChunk(message_, &parse_position);
  if (adjacency_matrix.compare(kSentinel) == 0) {
    return false;
  }
  // adjacency_matrix should be in CSV format; obtain the positions of commas.
  std::vector<int> comma_positions = GetCommaPositions(adjacency_matrix);
  // There should be a total of (number_of_vertices)^2 - 1 commas, since
  // adjacency_matrix is a square matrix.
  int number_of_vertices = sqrt(comma_positions.size() + 1);
  if (static_cast<int>(comma_positions.size()) !=
      (number_of_vertices * number_of_vertices) - 1) {
    return false;
  }
  // Decode the adjacency matrix
  int adj_position = 0;
  for (int i = 1; i <= number_of_vertices; ++i) {
    int next_comma_position;
    // There is no comma at the end
    if (i == number_of_vertices) {
      next_comma_position = adjacency_matrix.size();
    } else {
      next_comma_position = comma_positions[(i * number_of_vertices) - 1];
    }

    std::vector<int> row = DecodeCSV(adjacency_matrix.substr(adj_position,
        next_comma_position - adj_position));
    for (size_t j = 0; j < row.size(); ++j) {
      if (row[j] == kInvalidValue) {
        return false;
      }
    }
    adjacency_matrix_.push_back(row);
    adj_position = next_comma_position + 1;
  }
  comma_positions.clear();
  // Construct the graph
  graph::Graph input_graph(number_of_vertices, adjacency_matrix_);

  std::string task_ID = GetNextChunk(message_, &parse_position);
  if (task_ID.compare(kSentinel) == 0) {
    return false;
  }
  if (StringToInteger(task_ID) == kInvalidValue) {
    return false;
  }
  task_ID_ = StringToInteger(task_ID);

  std::string args = "";
  if (parse_position != static_cast<int>(message_length)) {
    // There may be some arguments.
    args = GetNextChunk(message_, &parse_position);
    if (args.compare(kSentinel) == 0) {
      return false;
    }
  }
{'fraction_non_alphanumeric': 0.11785812862268838, 'fraction_numerical': 0.007452387524151256, 'mean_word_length': 3.802518223989397, 'pattern_counts': {'":': 2, '<': 45, '<?xml version=': 0, '>': 39, 'https://': 0, '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': '1336316', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2335, 'n_words': 683}