<commit_before>/**
 ** \file object/float-class.cc
 ** \brief Creation of the URBI object float.
 */

#include <algorithm>

#include <libport/cmath>
#include <libport/compiler.hh>
#include <libport/format.hh>

#include <kernel/config.h>
#ifndef HAVE_ROUND
# include <libport/ufloat.h>
#endif
#include <libport/ufloat.hh>

#include <object/float.hh>
#include <object/format-info.hh>
#include <object/list.hh>
#include <object/object.hh>
#include <object/string.hh>
#include <object/symbols.hh>

#include <runner/raise.hh>

namespace object
{
  Float::Float(value_type value)
    : value_(value)
  {
    proto_add(proto ? proto : Object::proto);
  }

  Float::Float(const rFloat& model)
    : value_(model->value_get())
  {
    proto_add(model);
  }

  const Float::value_type&
  Float::value_get() const
  {
    return value_;
  }

  Float::value_type&
  Float::value_get()
  {
    return value_;
  }

#define CONVERSION(Name, Type)				\
  Type							\
  Float::to_##Name(const std::string fmt) const         \
  {							\
    try							\
    {							\
      return libport::numeric_cast<Type>(value_);       \
    }							\
    catch (libport::bad_numeric_cast&)                  \
    {							\
      runner::raise_bad_integer_error(value_, fmt);	\
    }							\
  }							\

  CONVERSION(int, int);
  CONVERSION(unsigned_int, unsigned int);
  CONVERSION(unsigned_type, Float::unsigned_type);
  CONVERSION(long_long, long long);

#undef CONVERSION

  bool
  Float::as_bool() const
  {
    return value_;
  }

  Float::value_type
  Float::inf()
  {
    return std::numeric_limits<libport::ufloat>::infinity();
  }

  Float::value_type
  Float::nan()
  {
    return std::numeric_limits<libport::ufloat>::quiet_NaN();
  }

  std::string
  Float::format(rFormatInfo finfo)
  {
    std::string pattern(finfo->pattern_get());

    replace(pattern.begin(), pattern.end(), 's', 'g');
    replace(pattern.begin(), pattern.end(), 'd', 'g');
    replace(pattern.begin(), pattern.end(), 'D', 'G');
    try
    {
      return libport::format(pattern,
                             libport::numeric_cast<long long>(value_));
    }
    catch (const libport::bad_numeric_cast&)
    {
      // Do nothing.
    }

    if (finfo->spec_get() == "x")
      runner::raise_bad_integer_error(value_);
    return libport::format(pattern, value_);
  }<commit_msg>format: fix octal.<commit_after>/**
 ** \file object/float-class.cc
 ** \brief Creation of the URBI object float.
 */

#include <algorithm>

#include <libport/cmath>
#include <libport/compiler.hh>
#include <libport/format.hh>

#include <kernel/config.h>
#ifndef HAVE_ROUND
# include <libport/ufloat.h>
#endif
#include <libport/ufloat.hh>

#include <object/float.hh>
#include <object/format-info.hh>
#include <object/list.hh>
#include <object/object.hh>
#include <object/string.hh>
#include <object/symbols.hh>

#include <runner/raise.hh>

namespace object
{
  Float::Float(value_type value)
    : value_(value)
  {
    proto_add(proto ? proto : Object::proto);
  }

  Float::Float(const rFloat& model)
    : value_(model->value_get())
  {
    proto_add(model);
  }

  const Float::value_type&
  Float::value_get() const
  {
    return value_;
  }

  Float::value_type&
  Float::value_get()
  {
    return value_;
  }

#define CONVERSION(Name, Type)				\
  Type							\
  Float::to_##Name(const std::string fmt) const         \
  {							\
    try							\
    {							\
      return libport::numeric_cast<Type>(value_);       \
    }							\
    catch (libport::bad_numeric_cast&)                  \
    {							\
      runner::raise_bad_integer_error(value_, fmt);	\
    }							\
  }							\

  CONVERSION(int, int);
  CONVERSION(unsigned_int, unsigned int);
  CONVERSION(unsigned_type, Float::unsigned_type);
  CONVERSION(long_long, long long);

#undef CONVERSION

  bool
  Float::as_bool() const
  {
    return value_;
  }

  Float::value_type
  Float::inf()
  {
    return std::numeric_limits<libport::ufloat>::infinity();
  }

  Float::value_type
  Float::nan()
  {
    return std::numeric_limits<libport::ufloat>::quiet_NaN();
  }

  std::string
  Float::format(rFormatInfo finfo)
  {
    std::string pattern(finfo->pattern_get());

    replace(pattern.begin(), pattern.end(), 's', 'g');
    replace(pattern.begin(), pattern.end(), 'd', 'g');
    replace(pattern.begin(), pattern.end(), 'D', 'G');
    try
    {
      return libport::format(pattern,
                             libport::numeric_cast<long long>(value_));
    }
    catch (const libport::bad_numeric_cast&)
    {
      // Do nothing.
    }

    if (finfo->spec_get() == "x" || finfo->spec_get() == "o")
      runner::raise_bad_integer_error(value_);
    return libport::format(pattern, value_);
  }
{'fraction_non_alphanumeric': 0.15763756107924368, 'fraction_numerical': 0.0, 'mean_word_length': 2.9463537300922047, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 46, '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': '1636276', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1627, 'n_words': 280}