<commit_before>/* This is free and unencumbered software released into the public domain. */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "cpr/vector.h"

#include <cerrno>       /* for errno */
#include <cstdlib>      /* for std::calloc(), std::free() */
#include <new>          /* for std::bad_alloc */
#include <stdexcept>    /* for std::out_of_range */
#include <system_error> /* for std::errc */
#include <vector>       /* for std::vector */

struct cpr_vector : public std::vector<void*> {};

extern const size_t cpr_vector_sizeof = sizeof(cpr_vector);

cpr_vector*
cpr_vector_alloc(void) {
  return reinterpret_cast<cpr_vector*>(std::calloc(1, sizeof(cpr_vector)));
}

void
cpr_vector_free(cpr_vector* const vector) {
  std::free(vector);
}

void
cpr_vector_init(cpr_vector* const vector) {
  new(vector) cpr_vector(); // TODO: handle exceptions?
}

void
cpr_vector_dispose(cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  vector->~cpr_vector();
}

bool
cpr_vector_empty(const cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  return vector->empty();
}

size_t
cpr_vector_size(const cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  return vector->size();
}

void*
cpr_vector_data(const cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  return const_cast<cpr_vector*>(vector)->data();
}

void*
cpr_vector_at(const cpr_vector* const vector,
              const size_t position) {
  try {
    return vector->at(position);
  }
  catch (const std::out_of_range& error) {
    errno = static_cast<int>(std::errc::argument_out_of_domain); /* EDOM */
    return nullptr;
  }
}

void
cpr_vector_clear(cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  vector->clear();
}

void
cpr_vector_push_back(cpr_vector* const vector,
                     const void* const element) {
  try {
    vector->push_back(const_cast<void*>(element));
  }
  catch (const std::bad_alloc& error) {
    errno = static_cast<int>(std::errc::not_enough_memory); /* ENOMEM */
  }
}

void
cpr_vector_pop_back(cpr_vector* const vector) {
  if (vector->empty()) {
    /* Invoking #pop_back() on an empty vector is defined to result in
     * undefined behavior. We'd rather avoid undefined behavior, so we'll
     * just fail gracefully instead. */
    errno = static_cast<int>(std::errc::bad_address); /* EFAULT */
    return;
  }
  /* Guaranteed to never throw an exception: */
  vector->pop_back();
}<commit_msg>Added static_assert(noexcept(...)) checks for no-throw guarantees.<commit_after>/* This is free and unencumbered software released into the public domain. */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "cpr/vector.h"

#include <cerrno>       /* for errno */
#include <cstdlib>      /* for std::calloc(), std::free() */
#include <new>          /* for std::bad_alloc */
#include <stdexcept>    /* for std::out_of_range */
#include <system_error> /* for std::errc */
#include <vector>       /* for std::vector */

struct cpr_vector : public std::vector<void*> {};

extern const size_t cpr_vector_sizeof = sizeof(cpr_vector);

cpr_vector*
cpr_vector_alloc(void) {
  return reinterpret_cast<cpr_vector*>(std::calloc(1, sizeof(cpr_vector)));
}

void
cpr_vector_free(cpr_vector* const vector) {
  std::free(vector);
}

void
cpr_vector_init(cpr_vector* const vector) {
  new(vector) cpr_vector(); // TODO: handle exceptions?
}

void
cpr_vector_dispose(cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  vector->~cpr_vector();
}

bool
cpr_vector_empty(const cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  return vector->empty();
#ifdef DEBUG
  static_assert(noexcept(vector->empty()),
    "std::vector::empty() declaration is missing the noexcept specifier");
#endif
}

size_t
cpr_vector_size(const cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  return vector->size();
#ifdef DEBUG
  static_assert(noexcept(vector->size()),
    "std::vector::size() declaration is missing the noexcept specifier");
#endif
}

void*
cpr_vector_data(const cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  return const_cast<cpr_vector*>(vector)->data();
#ifdef DEBUG
  static_assert(noexcept(vector->data()),
    "std::vector::data() declaration is missing the noexcept specifier");
#endif
}

void*
cpr_vector_at(const cpr_vector* const vector,
              const size_t position) {
  try {
    return vector->at(position);
  }
  catch (const std::out_of_range& error) {
    errno = static_cast<int>(std::errc::argument_out_of_domain); /* EDOM */
    return nullptr;
  }
}

void
cpr_vector_clear(cpr_vector* const vector) {
  /* Guaranteed to never throw an exception: */
  vector->clear();
#ifdef DEBUG
  static_assert(noexcept(vector->clear()),
    "std::vector::clear() declaration is missing the noexcept specifier");
#endif
}

void
cpr_vector_push_back(cpr_vector* const vector,
                     const void* const element) {
  try {
    vector->push_back(const_cast<void*>(element));
  }
  catch (const std::bad_alloc& error) {
    errno = static_cast<int>(std::errc::not_enough_memory); /* ENOMEM */
  }
}

void
cpr_vector_pop_back(cpr_vector* const vector) {
  if (vector->empty()) {
    /* Invoking #pop_back() on an empty vector is defined to result in
     * undefined behavior. We'd rather avoid undefined behavior, so we'll
     * just fail gracefully instead. */
    errno = static_cast<int>(std::errc::bad_address); /* EFAULT */
    return;
  }
  /* Guaranteed to never throw an exception: */
  vector->pop_back();
#if 0
  static_assert(noexcept(vector->pop_back()),
    "std::vector::pop_back() declaration is missing the noexcept specifier");
#endif
}
{'fraction_non_alphanumeric': 0.13678596008258775, 'fraction_numerical': 0.0005161734342739161, 'mean_word_length': 4.213452914798206, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 54, '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': '303876', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1910, 'n_words': 482}