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

//#define ENABLE_DEBUG_TRACES
#include <libport/compiler.hh>
#include <libport/throw-exception.hh>
#include <libport/tokenizer.hh>

#include "kernel/uconnection.hh"

#include "runner/runner.hh"

#include "object/atom.hh"
#include "object/object-class.hh"
#include "object/object.hh"

namespace object
{
  rObject object_class;

  /*--------------------.
  | Object primitives.  |
  `--------------------*/

  static rObject
  object_class_clone (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    return clone(args[0]);
  }

  static rObject
  object_class_init (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    return args[0];
  }

  /// Send dumped self on the connection.
  /// args[1], if present, can be the tag to use.
  static rObject
  object_class_dump (runner::Runner& r, objects_type args)
  {
    // Second argument is the tag name.
    std::string tag;
    if (args.size() == 2)
    {
      FETCH_ARG(1, String);
      tag = arg1->value_get().name_get();
    }
    std::ostringstream os;
    dump(r, args[0], os);
    //for now our best choice is to dump line by line in "system" messages.
    const std::string stream = os.str();
    boost::tokenizer< boost::char_separator<char> > tok =
      libport::make_tokenizer(stream, "\n");
    std::string system_header("*** ");
    BOOST_FOREACH(std::string line, tok)
      r.lobby_get()->value_get().connection.send (
	(system_header+line+"\n").c_str(), tag.c_str());
    return void_class;
  }

  static rObject
  object_echo(runner::Runner& r, objects_type args, bool is_echo)
  {
    // Second argument is the tag name.
    std::string tag;
    if (args.size() == 3)
    {
      FETCH_ARG(2, String);
      tag = arg2->value_get().name_get();
    }
    // Call asString.
    rObject asString = args[1]->slot_get(SYMBOL(asString));
    objects_type as_args;
    as_args.push_back(args[1]);
    rObject data = r.apply(asString, as_args);
    std::string s = VALUE(data, String).name_get();

    //Hack: special case for Strings to have k1 behavior
    //special case for Strings
    if (is_echo && args[1]->kind_is(Object::kind_string)
      && s[0] == '"' && s.length() && s[s.length()-1] == '"')
      s = libport::unescape(s.substr(1, s.length()-2));

    r.lobby_get()->value_get().connection.send(
    	(std::string(is_echo?"*** ":"") + s  + "\n").c_str(),
			   tag.c_str());

    return void_class;
  }

  /// Send pretty-printed args[1] to the connection.
  static rObject
  object_class_echo (runner::Runner& r, objects_type args)
  {
    return object_echo(r, args, true);
  }

  /// Send pretty-printed self on the connection.
  /// args[1], if present, can be the tag to use.
  static rObject
  object_class_print (runner::Runner& r, objects_type args)
  {
    CHECK_ARG_COUNT_RANGE (1, 2);
    objects_type nargs;
    nargs.push_back(args[0]);
    nargs.insert(nargs.end(),  args.begin(), args.end());
    return object_echo(r, nargs, false);
  }

  /// Send pretty-printed self on the connection.
  /// args[1], if present, can be the tag to use.
  static rObject
  object_class_asString (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    std::ostringstream os;
    // FIXME: Decide what should be printed, but at least print something
    os << "<object_" << std::hex << (long)args[0].get() << ">";
    return new String(libport::Symbol(os.str()));
  }

  /// Return the address of an object as a number, mostly
  /// for debugging purpose.
  static rObject
  object_class_uid (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    std::string str = (boost::format ("0x%x")
		       % reinterpret_cast<long long> (args[0].get ())).str ();
    return new String (libport::Symbol (str));
  }

  static rObject
  object_class_sameAs(runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (2);
    return args[0]->slot_get((args[0] == args[1])
			     ? SYMBOL(true)
			     : SYMBOL(false));
  }

  static rObject
  object_class_apply (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (2);
    FETCH_ARG (1, List);
    if (!arg1->value_get ().size () == 1)
      throw
	PrimitiveError ("apply", "first argument must be [self]");
    return arg1->value_get ().front ();
  }

  /*---------.
  | Protos.  |
  `---------*/<commit_msg>Prefer !=.<commit_after>/**
 ** \file object/object-class.cc
 ** \brief Creation of the URBI object object.
 */

//#define ENABLE_DEBUG_TRACES
#include <libport/compiler.hh>
#include <libport/throw-exception.hh>
#include <libport/tokenizer.hh>

#include "kernel/uconnection.hh"

#include "runner/runner.hh"

#include "object/atom.hh"
#include "object/object-class.hh"
#include "object/object.hh"

namespace object
{
  rObject object_class;

  /*--------------------.
  | Object primitives.  |
  `--------------------*/

  static rObject
  object_class_clone (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    return clone(args[0]);
  }

  static rObject
  object_class_init (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    return args[0];
  }

  /// Send dumped self on the connection.
  /// args[1], if present, can be the tag to use.
  static rObject
  object_class_dump (runner::Runner& r, objects_type args)
  {
    // Second argument is the tag name.
    std::string tag;
    if (args.size() == 2)
    {
      FETCH_ARG(1, String);
      tag = arg1->value_get().name_get();
    }
    std::ostringstream os;
    dump(r, args[0], os);
    //for now our best choice is to dump line by line in "system" messages.
    const std::string stream = os.str();
    boost::tokenizer< boost::char_separator<char> > tok =
      libport::make_tokenizer(stream, "\n");
    std::string system_header("*** ");
    BOOST_FOREACH(std::string line, tok)
      r.lobby_get()->value_get().connection.send (
	(system_header+line+"\n").c_str(), tag.c_str());
    return void_class;
  }

  static rObject
  object_echo(runner::Runner& r, objects_type args, bool is_echo)
  {
    // Second argument is the tag name.
    std::string tag;
    if (args.size() == 3)
    {
      FETCH_ARG(2, String);
      tag = arg2->value_get().name_get();
    }
    // Call asString.
    rObject asString = args[1]->slot_get(SYMBOL(asString));
    objects_type as_args;
    as_args.push_back(args[1]);
    rObject data = r.apply(asString, as_args);
    std::string s = VALUE(data, String).name_get();

    //Hack: special case for Strings to have k1 behavior
    //special case for Strings
    if (is_echo && args[1]->kind_is(Object::kind_string)
      && s[0] == '"' && s.length() && s[s.length()-1] == '"')
      s = libport::unescape(s.substr(1, s.length()-2));

    r.lobby_get()->value_get().connection.send
      ((std::string(is_echo?"*** ":"") + s + "\n").c_str(),
       tag.c_str());

    return void_class;
  }

  /// Send pretty-printed args[1] to the connection.
  static rObject
  object_class_echo (runner::Runner& r, objects_type args)
  {
    return object_echo(r, args, true);
  }

  /// Send pretty-printed self on the connection.
  /// args[1], if present, can be the tag to use.
  static rObject
  object_class_print (runner::Runner& r, objects_type args)
  {
    CHECK_ARG_COUNT_RANGE (1, 2);
    objects_type nargs;
    nargs.push_back(args[0]);
    nargs.insert(nargs.end(),  args.begin(), args.end());
    return object_echo(r, nargs, false);
  }

  /// Send pretty-printed self on the connection.
  /// args[1], if present, can be the tag to use.
  static rObject
  object_class_asString (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    std::ostringstream os;
    // FIXME: Decide what should be printed, but at least print something
    os << "<object_" << std::hex << (long)args[0].get() << ">";
    return new String(libport::Symbol(os.str()));
  }

  /// Return the address of an object as a number, mostly
  /// for debugging purpose.
  static rObject
  object_class_uid (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (1);
    std::string str = (boost::format ("0x%x")
		       % reinterpret_cast<long long> (args[0].get ())).str ();
    return new String (libport::Symbol (str));
  }

  static rObject
  object_class_sameAs(runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (2);
    return args[0]->slot_get((args[0] == args[1])
			     ? SYMBOL(true)
			     : SYMBOL(false));
  }

  static rObject
  object_class_apply (runner::Runner&, objects_type args)
  {
    CHECK_ARG_COUNT (2);
    FETCH_ARG (1, List);
    if (arg1->value_get ().size () != 1)
      throw PrimitiveError ("apply", "first argument must be [self]");
    return arg1->value_get ().front ();
  }

  /*---------.
  | Protos.  |
  `---------*/
{'fraction_non_alphanumeric': 0.16822217163669473, 'fraction_numerical': 0.009105394946505805, 'mean_word_length': 3.0215102974828376, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 35, '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': '2330656', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2999, 'n_words': 731}