<commit_before>#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>

#include <kernel/uvalue-cast.hh>
#include <object/cxx-conversions.hh>
#include <object/float.hh>
#include <object/global.hh>
#include <object/list.hh>
#include <object/string.hh>
#include <runner/raise.hh>
#include <urbi/uvalue.hh>

urbi::UValue uvalue_cast(object::rObject o)
{
  static object::rObject Binary =
    object::global_class->slot_get(SYMBOL(Binary));
  urbi::UValue res;
  if (object::rFloat f = o->as<object::Float>())
    res = f->value_get();
  else if (object::rString s = o->as<object::String>())
    res = s->value_get();
  else if (object::rList s = o->as<object::List>())
  {
    res.type = urbi::DATA_LIST;
    res.list = new urbi::UList;
    object::List::value_type& t = o.cast<object::List>()->value_get();
    foreach (const object::rObject& co, t)
      res.list->array.push_back(new urbi::UValue(uvalue_cast(co)));
  }
  else if (is_a(o, Binary))
  {
    const std::string& data =
      o->slot_get(SYMBOL(data))->as<object::String>()->value_get();
    const std::string& keywords =
      o->slot_get(SYMBOL(keywords))->
      as<object::String>()->value_get();
    std::list<urbi::BinaryData> l;
    l.push_back(urbi::BinaryData(const_cast<char*>(data.c_str()),
				 data.size()));
    std::list<urbi::BinaryData>::iterator i = l.begin();
    res.type = urbi::DATA_BINARY;
    res.binary = new urbi::UBinary();
    res.binary->parse(
      (boost::lexical_cast<std::string>(data.size()) +
       " " + keywords + '\n').c_str(), 0, l, i);
  }
  else // We could not find how to cast this value
  {
    const object::rString& rs =
      o->slot_get(SYMBOL(type))->as<object::String>();
    runner::raise_argument_type_error
      (0, rs, object::to_urbi(SYMBOL(LT_exportable_SP_object_GT)),
       object::to_urbi(SYMBOL(cast)));
  }
  return res;
}

object::rObject
object_cast(const urbi::UValue& v)
{
  object::rObject res;
  switch(v.type)
  {
    case urbi::DATA_DOUBLE:
      return new object::Float(v.val);
      break;

    case urbi::DATA_STRING:
      return new object::String(*v.stringValue);
      break;

    case urbi::DATA_LIST:
    {
      object::rList l = new object::List();
      foreach (urbi::UValue *cv, v.list->array)
	l->value_get().push_back(object_cast(*cv));
      res = l;
      break;
    }

    case urbi::DATA_BINARY:
    {
      static object::rObject Binary =
	object::global_class->slot_get(SYMBOL(Binary));
      res = new object::Object();
      res->proto_add(Binary);
      std::string msg = v.binary->getMessage();
      // Trim it.
      if (!msg.empty() && msg[0] == ' ')
        msg = msg.substr(1, msg.npos);
      res->slot_set(SYMBOL(keywords),
                    new object::String(msg));
      res->slot_set(SYMBOL(data),
                    new object::String
                    (std::string(static_cast<char*>(v.binary->common.data),
				 v.binary->common.size)));
    }
    break;

    default:
      static boost::format m("<external data with type %1%>");
      runner::raise_argument_type_error
	(0, object::to_urbi((m % v.type).str()),
	 object::object_class,
	 object::to_urbi(SYMBOL(backcast)));
  }
  return res;
}
<commit_msg>Allow casting from void UValue to rObject.<commit_after>#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>

#include <kernel/uvalue-cast.hh>
#include <object/cxx-conversions.hh>
#include <object/float.hh>
#include <object/global.hh>
#include <object/list.hh>
#include <object/string.hh>
#include <runner/raise.hh>
#include <urbi/uvalue.hh>

urbi::UValue uvalue_cast(object::rObject o)
{
  static object::rObject Binary =
    object::global_class->slot_get(SYMBOL(Binary));
  urbi::UValue res;
  if (object::rFloat f = o->as<object::Float>())
    res = f->value_get();
  else if (object::rString s = o->as<object::String>())
    res = s->value_get();
  else if (object::rList s = o->as<object::List>())
  {
    res.type = urbi::DATA_LIST;
    res.list = new urbi::UList;
    object::List::value_type& t = o.cast<object::List>()->value_get();
    foreach (const object::rObject& co, t)
      res.list->array.push_back(new urbi::UValue(uvalue_cast(co)));
  }
  else if (is_a(o, Binary))
  {
    const std::string& data =
      o->slot_get(SYMBOL(data))->as<object::String>()->value_get();
    const std::string& keywords =
      o->slot_get(SYMBOL(keywords))->
      as<object::String>()->value_get();
    std::list<urbi::BinaryData> l;
    l.push_back(urbi::BinaryData(const_cast<char*>(data.c_str()),
				 data.size()));
    std::list<urbi::BinaryData>::iterator i = l.begin();
    res.type = urbi::DATA_BINARY;
    res.binary = new urbi::UBinary();
    res.binary->parse(
      (boost::lexical_cast<std::string>(data.size()) +
       " " + keywords + '\n').c_str(), 0, l, i);
  }
  else // We could not find how to cast this value
  {
    const object::rString& rs =
      o->slot_get(SYMBOL(type))->as<object::String>();
    runner::raise_argument_type_error
      (0, rs, object::to_urbi(SYMBOL(LT_exportable_SP_object_GT)),
       object::to_urbi(SYMBOL(cast)));
  }
  return res;
}

object::rObject
object_cast(const urbi::UValue& v)
{
  object::rObject res;
  switch(v.type)
  {
    case urbi::DATA_DOUBLE:
      return new object::Float(v.val);
      break;

    case urbi::DATA_STRING:
      return new object::String(*v.stringValue);
      break;

    case urbi::DATA_LIST:
    {
      object::rList l = new object::List();
      foreach (urbi::UValue *cv, v.list->array)
	l->value_get().push_back(object_cast(*cv));
      res = l;
      break;
    }

    case urbi::DATA_BINARY:
    {
      static object::rObject Binary =
	object::global_class->slot_get(SYMBOL(Binary));
      res = new object::Object();
      res->proto_add(Binary);
      std::string msg = v.binary->getMessage();
      // Trim it.
      if (!msg.empty() && msg[0] == ' ')
        msg = msg.substr(1, msg.npos);
      res->slot_set(SYMBOL(keywords),
                    new object::String(msg));
      res->slot_set(SYMBOL(data),
                    new object::String
                    (std::string(static_cast<char*>(v.binary->common.data),
				 v.binary->common.size)));
    }
    break;

    case urbi::DATA_VOID:
      res = object::void_class;
    break;

    default:
      static boost::format m("<external data with type %1%>");
      runner::raise_argument_type_error
	(0, object::to_urbi((m % v.type).str()),
	 object::object_class,
	 object::to_urbi(SYMBOL(backcast)));
  }
  return res;
}

{'fraction_non_alphanumeric': 0.17953459701032518, 'fraction_numerical': 0.0018492834026814608, 'mean_word_length': 3.3703703703703702, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 101, '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': '1174788', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2273, 'n_words': 399}