transmogrify.h.h 7.6 KB

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