(root)/
Python-3.12.0/
Objects/
stringlib/
clinic/
transmogrify.h.h
       1  /*[clinic input]
       2  preserve
       3  [clinic start generated code]*/
       4  
       5  #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
       6  #  include "pycore_gc.h"            // PyGC_Head
       7  #  include "pycore_runtime.h"       // _Py_ID()
       8  #endif
       9  
      10  
      11  PyDoc_STRVAR(stringlib_expandtabs__doc__,
      12  "expandtabs($self, /, tabsize=8)\n"
      13  "--\n"
      14  "\n"
      15  "Return a copy where all tab characters are expanded using spaces.\n"
      16  "\n"
      17  "If tabsize is not given, a tab size of 8 characters is assumed.");
      18  
      19  #define STRINGLIB_EXPANDTABS_METHODDEF    \
      20      {"expandtabs", _PyCFunction_CAST(stringlib_expandtabs), METH_FASTCALL|METH_KEYWORDS, stringlib_expandtabs__doc__},
      21  
      22  static PyObject *
      23  stringlib_expandtabs_impl(PyObject *self, int tabsize);
      24  
      25  static PyObject *
      26  stringlib_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
      27  {
      28      PyObject *return_value = NULL;
      29      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
      30  
      31      #define NUM_KEYWORDS 1
      32      static struct {
      33          PyGC_Head _this_is_not_used;
      34          PyObject_VAR_HEAD
      35          PyObject *ob_item[NUM_KEYWORDS];
      36      } _kwtuple = {
      37          .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
      38          .ob_item = { &_Py_ID(tabsize), },
      39      };
      40      #undef NUM_KEYWORDS
      41      #define KWTUPLE (&_kwtuple.ob_base.ob_base)
      42  
      43      #else  // !Py_BUILD_CORE
      44      #  define KWTUPLE NULL
      45      #endif  // !Py_BUILD_CORE
      46  
      47      static const char * const _keywords[] = {"tabsize", NULL};
      48      static _PyArg_Parser _parser = {
      49          .keywords = _keywords,
      50          .fname = "expandtabs",
      51          .kwtuple = KWTUPLE,
      52      };
      53      #undef KWTUPLE
      54      PyObject *argsbuf[1];
      55      Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
      56      int tabsize = 8;
      57  
      58      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
      59      if (!args) {
      60          goto exit;
      61      }
      62      if (!noptargs) {
      63          goto skip_optional_pos;
      64      }
      65      tabsize = _PyLong_AsInt(args[0]);
      66      if (tabsize == -1 && PyErr_Occurred()) {
      67          goto exit;
      68      }
      69  skip_optional_pos:
      70      return_value = stringlib_expandtabs_impl(self, tabsize);
      71  
      72  exit:
      73      return return_value;
      74  }
      75  
      76  PyDoc_STRVAR(stringlib_ljust__doc__,
      77  "ljust($self, width, fillchar=b\' \', /)\n"
      78  "--\n"
      79  "\n"
      80  "Return a left-justified string of length width.\n"
      81  "\n"
      82  "Padding is done using the specified fill character.");
      83  
      84  #define STRINGLIB_LJUST_METHODDEF    \
      85      {"ljust", _PyCFunction_CAST(stringlib_ljust), METH_FASTCALL, stringlib_ljust__doc__},
      86  
      87  static PyObject *
      88  stringlib_ljust_impl(PyObject *self, Py_ssize_t width, char fillchar);
      89  
      90  static PyObject *
      91  stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
      92  {
      93      PyObject *return_value = NULL;
      94      Py_ssize_t width;
      95      char fillchar = ' ';
      96  
      97      if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
      98          goto exit;
      99      }
     100      {
     101          Py_ssize_t ival = -1;
     102          PyObject *iobj = _PyNumber_Index(args[0]);
     103          if (iobj != NULL) {
     104              ival = PyLong_AsSsize_t(iobj);
     105              Py_DECREF(iobj);
     106          }
     107          if (ival == -1 && PyErr_Occurred()) {
     108              goto exit;
     109          }
     110          width = ival;
     111      }
     112      if (nargs < 2) {
     113          goto skip_optional;
     114      }
     115      if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
     116          fillchar = PyBytes_AS_STRING(args[1])[0];
     117      }
     118      else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
     119          fillchar = PyByteArray_AS_STRING(args[1])[0];
     120      }
     121      else {
     122          _PyArg_BadArgument("ljust", "argument 2", "a byte string of length 1", args[1]);
     123          goto exit;
     124      }
     125  skip_optional:
     126      return_value = stringlib_ljust_impl(self, width, fillchar);
     127  
     128  exit:
     129      return return_value;
     130  }
     131  
     132  PyDoc_STRVAR(stringlib_rjust__doc__,
     133  "rjust($self, width, fillchar=b\' \', /)\n"
     134  "--\n"
     135  "\n"
     136  "Return a right-justified string of length width.\n"
     137  "\n"
     138  "Padding is done using the specified fill character.");
     139  
     140  #define STRINGLIB_RJUST_METHODDEF    \
     141      {"rjust", _PyCFunction_CAST(stringlib_rjust), METH_FASTCALL, stringlib_rjust__doc__},
     142  
     143  static PyObject *
     144  stringlib_rjust_impl(PyObject *self, Py_ssize_t width, char fillchar);
     145  
     146  static PyObject *
     147  stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
     148  {
     149      PyObject *return_value = NULL;
     150      Py_ssize_t width;
     151      char fillchar = ' ';
     152  
     153      if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
     154          goto exit;
     155      }
     156      {
     157          Py_ssize_t ival = -1;
     158          PyObject *iobj = _PyNumber_Index(args[0]);
     159          if (iobj != NULL) {
     160              ival = PyLong_AsSsize_t(iobj);
     161              Py_DECREF(iobj);
     162          }
     163          if (ival == -1 && PyErr_Occurred()) {
     164              goto exit;
     165          }
     166          width = ival;
     167      }
     168      if (nargs < 2) {
     169          goto skip_optional;
     170      }
     171      if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
     172          fillchar = PyBytes_AS_STRING(args[1])[0];
     173      }
     174      else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
     175          fillchar = PyByteArray_AS_STRING(args[1])[0];
     176      }
     177      else {
     178          _PyArg_BadArgument("rjust", "argument 2", "a byte string of length 1", args[1]);
     179          goto exit;
     180      }
     181  skip_optional:
     182      return_value = stringlib_rjust_impl(self, width, fillchar);
     183  
     184  exit:
     185      return return_value;
     186  }
     187  
     188  PyDoc_STRVAR(stringlib_center__doc__,
     189  "center($self, width, fillchar=b\' \', /)\n"
     190  "--\n"
     191  "\n"
     192  "Return a centered string of length width.\n"
     193  "\n"
     194  "Padding is done using the specified fill character.");
     195  
     196  #define STRINGLIB_CENTER_METHODDEF    \
     197      {"center", _PyCFunction_CAST(stringlib_center), METH_FASTCALL, stringlib_center__doc__},
     198  
     199  static PyObject *
     200  stringlib_center_impl(PyObject *self, Py_ssize_t width, char fillchar);
     201  
     202  static PyObject *
     203  stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
     204  {
     205      PyObject *return_value = NULL;
     206      Py_ssize_t width;
     207      char fillchar = ' ';
     208  
     209      if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
     210          goto exit;
     211      }
     212      {
     213          Py_ssize_t ival = -1;
     214          PyObject *iobj = _PyNumber_Index(args[0]);
     215          if (iobj != NULL) {
     216              ival = PyLong_AsSsize_t(iobj);
     217              Py_DECREF(iobj);
     218          }
     219          if (ival == -1 && PyErr_Occurred()) {
     220              goto exit;
     221          }
     222          width = ival;
     223      }
     224      if (nargs < 2) {
     225          goto skip_optional;
     226      }
     227      if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
     228          fillchar = PyBytes_AS_STRING(args[1])[0];
     229      }
     230      else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
     231          fillchar = PyByteArray_AS_STRING(args[1])[0];
     232      }
     233      else {
     234          _PyArg_BadArgument("center", "argument 2", "a byte string of length 1", args[1]);
     235          goto exit;
     236      }
     237  skip_optional:
     238      return_value = stringlib_center_impl(self, width, fillchar);
     239  
     240  exit:
     241      return return_value;
     242  }
     243  
     244  PyDoc_STRVAR(stringlib_zfill__doc__,
     245  "zfill($self, width, /)\n"
     246  "--\n"
     247  "\n"
     248  "Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
     249  "\n"
     250  "The original string is never truncated.");
     251  
     252  #define STRINGLIB_ZFILL_METHODDEF    \
     253      {"zfill", (PyCFunction)stringlib_zfill, METH_O, stringlib_zfill__doc__},
     254  
     255  static PyObject *
     256  stringlib_zfill_impl(PyObject *self, Py_ssize_t width);
     257  
     258  static PyObject *
     259  stringlib_zfill(PyObject *self, PyObject *arg)
     260  {
     261      PyObject *return_value = NULL;
     262      Py_ssize_t width;
     263  
     264      {
     265          Py_ssize_t ival = -1;
     266          PyObject *iobj = _PyNumber_Index(arg);
     267          if (iobj != NULL) {
     268              ival = PyLong_AsSsize_t(iobj);
     269              Py_DECREF(iobj);
     270          }
     271          if (ival == -1 && PyErr_Occurred()) {
     272              goto exit;
     273          }
     274          width = ival;
     275      }
     276      return_value = stringlib_zfill_impl(self, width);
     277  
     278  exit:
     279      return return_value;
     280  }
     281  /*[clinic end generated code: output=d44a269805f6739e input=a9049054013a1b77]*/