<commit_before>// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Simple utility to wrap a binary file in a C++ source file.

#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <utility>
#include <vector>

#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_replace.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/strerror.h"
#include "sandboxed_api/util/raw_logging.h"

// C-escapes a character and writes it to a file stream.
void FWriteCEscapedC(int c, FILE* out) {
  /* clang-format off */
  constexpr char kCEscapedLen[256] = {
      4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4,  // \t, \n, \r
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // "
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // '0'..'9'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // 'A'..'O'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1,  // 'P'..'Z', '\'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // 'a'..'o'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4,  // 'p'..'z', DEL
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  };
  /* clang-format on */

  int char_len = kCEscapedLen[c];
  if (char_len == 1) {
    fputc(c, out);
  } else if (char_len == 2) {
    fputc('\\', out);
    switch (c) {
      case '\0':
        fputc('0', out);
        break;
      case '\n':
        fputc('n', out);
        break;
      case '\r':
        fputc('r', out);
        break;
      case '\t':
        fputc('t', out);
        break;
      case '\"':
        fputc('\"', out);
        break;
      case '\'':
        fputc('\'', out);
        break;
      case '\\':
        fputc('\\', out);
        break;
    }
  } else {
    fputc('\\', out);
    fputc('0' + c / 64, out);
    fputc('0' + (c % 64) / 8, out);
    fputc('0' + c % 8, out);
  }
}

// Small RAII class that wraps C-style FILE streams and sets up buffering.
class File {
 public:
  File(const char* name, const char* mode)
      : name_{name}, stream_{fopen(name, mode)}, buf_(4096, '\0') {
    SAPI_RAW_PCHECK(stream_ != nullptr, "Open %s", name_);
    std::setvbuf(stream_, &buf_[0], _IOFBF, buf_.size());
    Check();
  }
  ~File() { fclose(stream_); }

  void Check() { SAPI_RAW_PCHECK(!ferror(stream_), "I/O on %s", name_); }

  FILE* get() const { return stream_; }

 private:
  const char* name_;<commit_msg>Copybara import of the project:<commit_after>// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Simple utility to wrap a binary file in a C++ source file.

#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <utility>
#include <vector>

#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_replace.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/strerror.h"
#include "sandboxed_api/util/raw_logging.h"

// C-escapes a character and writes it to a file stream.
void FWriteCEscapedC(int c, FILE* out) {
  /* clang-format off */
  constexpr char kCEscapedLen[256] = {
      4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4,  // \t, \n, \r
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // "
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,  // '0'..'9'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // 'A'..'O'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1,  // 'P'..'Z', '\'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // 'a'..'o'
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4,  // 'p'..'z', DEL
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  };
  /* clang-format on */

  int char_len = kCEscapedLen[c];
  if (char_len == 1) {
    fputc(c, out);
  } else if (char_len == 2) {
    fputc('\\', out);
    switch (c) {
      case '\0':
        fputc('0', out);
        break;
      case '\n':
        fputc('n', out);
        break;
      case '\r':
        fputc('r', out);
        break;
      case '\t':
        fputc('t', out);
        break;
      case '\"':
      case '\'':
      case '\\':
      case '?':
        fputc(c, out);
        break;
    }
  } else {
    fputc('\\', out);
    fputc('0' + c / 64, out);
    fputc('0' + (c % 64) / 8, out);
    fputc('0' + c % 8, out);
  }
}

// Small RAII class that wraps C-style FILE streams and sets up buffering.
class File {
 public:
  File(const char* name, const char* mode)
      : name_{name}, stream_{fopen(name, mode)}, buf_(4096, '\0') {
    SAPI_RAW_PCHECK(stream_ != nullptr, "Open %s", name_);
    std::setvbuf(stream_, &buf_[0], _IOFBF, buf_.size());
    Check();
  }
  ~File() { fclose(stream_); }

  void Check() { SAPI_RAW_PCHECK(!ferror(stream_), "I/O on %s", name_); }

  FILE* get() const { return stream_; }

 private:
  const char* name_;
{'fraction_non_alphanumeric': 0.20238962552819467, 'fraction_numerical': 0.08451114672883579, 'mean_word_length': 2.2392638036809815, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5880926', 'n_tokens_mistral': 3463, 'n_tokens_neox': 2912, 'n_words': 1056}