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

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

#include "xsd++/boolean.h"

#include "xsd++/regex.h" /* for std::regex, std::regex_match() */

using namespace xsd;

constexpr char boolean::name[];

constexpr char boolean::pattern[];

static const std::regex boolean_regex(boolean::pattern);

bool
boolean::match(const std::string& literal) noexcept {
  return std::regex_match(literal, boolean_regex);
}

bool
boolean::validate() const noexcept {
  return boolean::match(_literal);
}

bool
boolean::canonicalize() {
  if (_literal.compare("true") == 0) {
    return false; /* already in canonical form */
  }

  if (_literal.compare("false") == 0) {
    return false; /* already in canonical form */
  }

  if (_literal.compare("1") == 0) {
    _literal.assign("true"); /* can throw std::bad_alloc */
    return true; /* now in canonical form */
  }

  if (_literal.compare("0") == 0) {
    _literal.assign("false"); /* can throw std::bad_alloc */
    return true; /* now in canonical form */
  }

  return false; /* invalid literal */
}

boolean::operator bool() const {
  return _literal.compare("true") == 0 ||
         _literal.compare("1") == 0;
}
<commit_msg>Implemented more unit tests for the xsd::boolean implementation.<commit_after>/* This is free and unencumbered software released into the public domain. */

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

#include "xsd++/boolean.h"

#include "xsd++/regex.h" /* for std::regex, std::regex_match() */

using namespace xsd;

constexpr char boolean::name[];

constexpr char boolean::pattern[];

static const std::regex boolean_regex(boolean::pattern);

bool
boolean::match(const std::string& literal) noexcept {
  return std::regex_match(literal, boolean_regex);
}

bool
boolean::validate() const noexcept {
  return boolean::match(_literal);
}

bool
boolean::canonicalize() {
  if (_literal.compare("true") == 0) {
    return false; /* already in canonical form */
  }

  if (_literal.compare("false") == 0) {
    return false; /* already in canonical form */
  }

  if (_literal.compare("1") == 0) {
    _literal.assign("true"); /* can throw std::bad_alloc */
    return true; /* now in canonical form */
  }

  if (_literal.compare("0") == 0) {
    _literal.assign("false"); /* can throw std::bad_alloc */
    return true; /* now in canonical form */
  }

  return false; /* invalid literal */
}

boolean::operator bool() const {
  if (!validate()) throw std::bad_cast();
  return _literal.compare("true") == 0 ||
         _literal.compare("1") == 0;
}

{'fraction_non_alphanumeric': 0.1618828932261768, 'fraction_numerical': 0.006888633754305396, 'mean_word_length': 4.017274472168906, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1613287', 'n_tokens_mistral': 823, 'n_tokens_neox': 798, 'n_words': 229}