<commit_before>#include <memory>
#include <utility>

#include <Python.h>

#include "PyString.hh"

using namespace py;
using std::unique_ptr;

//------------------------------------------------------------------------------

namespace {

int tp_init(PyString* self, PyObject* args, PyObject* kw_args)
{
  static char const* arg_names[] 
      = {"size", "ellipsis", "pad", "elide_pos", "pad_pos", nullptr};

  int   size;
  char* ellipsis = nullptr;
  char* pad = nullptr;
  float elide_pos = 1;
  float pad_pos = fixfmt::PAD_POS_LEFT_JUSTIFY;
#if PY3K
  if (!PyArg_ParseTupleAndKeywords(
      args, kw_args, "i|ssff", (char**) arg_names,
      &size, &ellipsis, &pad, &elide_pos, &pad_pos)) 
    return -1;
#else
  if (!PyArg_ParseTupleAndKeywords(
      args, kw_args, "i|etetff", (char**) arg_names,
      &size, "utf-8", &ellipsis, "utf-8", &pad, &elide_pos, &pad_pos))
    return -1;
  PyMemGuard ellipsis_guard(ellipsis);
  PyMemGuard pad_guard(pad);
#endif

  if (ellipsis == nullptr)
    ellipsis = (char*) "\u2026";
  if (pad == nullptr)
    pad = (char*) " ";
  if (strlen(pad) == 0) {
    PyErr_SetString(PyExc_ValueError, "empty pad");
    return 1;
  }

  new(self) PyString;
  self->fmt_ = std::make_unique<fixfmt::String>(
    fixfmt::String::Args{
      size, ellipsis, pad, (float) elide_pos, (float) pad_pos});
  return 0;
}


PyObject* tp_call(PyString* self, PyObject* args, PyObject* kw_args)
{
  static char const* arg_names[] = {"str", nullptr};
  char* val;
#if PY3K
  if (!PyArg_ParseTupleAndKeywords(
      args, kw_args, "s", (char**) arg_names, &val))
    return nullptr;
#else
  if (!PyArg_ParseTupleAndKeywords(
      args, kw_args, "et", (char**) arg_names, "utf-8", &val))
    return nullptr;
  PyMemGuard str_guard(val);
#endif

  return Unicode::from((*self->fmt_)(val)).release();
}


auto methods = Methods<PyString>()
;


ref<Object> get_elide_pos(PyString* const self, void* /* closure */)
{
  return Float::FromDouble(self->fmt_->get_args().elide_pos);
}


void set_elide_pos(PyString* const self, Object* val, void* /* closure */)
{
  auto args = self->fmt_->get_args();
  args.elide_pos = val->double_value();
  self->fmt_->set_args(args);<commit_msg>Stringify String() arg.<commit_after>#include <memory>
#include <utility>

#include <Python.h>

#include "PyString.hh"

using namespace py;
using std::unique_ptr;

//------------------------------------------------------------------------------

namespace {

int tp_init(PyString* self, PyObject* args, PyObject* kw_args)
{
  static char const* arg_names[] 
      = {"size", "ellipsis", "pad", "elide_pos", "pad_pos", nullptr};

  int   size;
  char* ellipsis = nullptr;
  char* pad = nullptr;
  float elide_pos = 1;
  float pad_pos = fixfmt::PAD_POS_LEFT_JUSTIFY;
#if PY3K
  if (!PyArg_ParseTupleAndKeywords(
      args, kw_args, "i|ssff", (char**) arg_names,
      &size, &ellipsis, &pad, &elide_pos, &pad_pos)) 
    return -1;
#else
  if (!PyArg_ParseTupleAndKeywords(
      args, kw_args, "i|etetff", (char**) arg_names,
      &size, "utf-8", &ellipsis, "utf-8", &pad, &elide_pos, &pad_pos))
    return -1;
  PyMemGuard ellipsis_guard(ellipsis);
  PyMemGuard pad_guard(pad);
#endif

  if (ellipsis == nullptr)
    ellipsis = (char*) "\u2026";
  if (pad == nullptr)
    pad = (char*) " ";
  if (strlen(pad) == 0) {
    PyErr_SetString(PyExc_ValueError, "empty pad");
    return 1;
  }

  new(self) PyString;
  self->fmt_ = std::make_unique<fixfmt::String>(
    fixfmt::String::Args{
      size, ellipsis, pad, (float) elide_pos, (float) pad_pos});
  return 0;
}


PyObject* tp_call(PyString* self, PyObject* args, PyObject* kw_args)
{
  static char const* arg_names[] = {"str", nullptr};
  Object* val;
  if (!PyArg_ParseTupleAndKeywords(
      args, kw_args, "O", (char**) arg_names, &val))
    return nullptr;

  return Unicode::from((*self->fmt_)(val->Str()->as_utf8())).release();
}


auto methods = Methods<PyString>()
;


ref<Object> get_elide_pos(PyString* const self, void* /* closure */)
{
  return Float::FromDouble(self->fmt_->get_args().elide_pos);
}


void set_elide_pos(PyString* const self, Object* val, void* /* closure */)
{
  auto args = self->fmt_->get_args();
  args.elide_pos = val->double_value();
  self->fmt_->set_args(args);
{'fraction_non_alphanumeric': 0.1993402450518379, 'fraction_numerical': 0.006833176248821867, 'mean_word_length': 3.53525641025641, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2328215', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1568, 'n_words': 354}