unicodeobject.c.h 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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(EncodingMap_size__doc__,
  9. "size($self, /)\n"
  10. "--\n"
  11. "\n"
  12. "Return the size (in bytes) of this object.");
  13. #define ENCODINGMAP_SIZE_METHODDEF \
  14. {"size", (PyCFunction)EncodingMap_size, METH_NOARGS, EncodingMap_size__doc__},
  15. static PyObject *
  16. EncodingMap_size_impl(struct encoding_map *self);
  17. static PyObject *
  18. EncodingMap_size(struct encoding_map *self, PyObject *Py_UNUSED(ignored))
  19. {
  20. return EncodingMap_size_impl(self);
  21. }
  22. PyDoc_STRVAR(unicode_title__doc__,
  23. "title($self, /)\n"
  24. "--\n"
  25. "\n"
  26. "Return a version of the string where each word is titlecased.\n"
  27. "\n"
  28. "More specifically, words start with uppercased characters and all remaining\n"
  29. "cased characters have lower case.");
  30. #define UNICODE_TITLE_METHODDEF \
  31. {"title", (PyCFunction)unicode_title, METH_NOARGS, unicode_title__doc__},
  32. static PyObject *
  33. unicode_title_impl(PyObject *self);
  34. static PyObject *
  35. unicode_title(PyObject *self, PyObject *Py_UNUSED(ignored))
  36. {
  37. return unicode_title_impl(self);
  38. }
  39. PyDoc_STRVAR(unicode_capitalize__doc__,
  40. "capitalize($self, /)\n"
  41. "--\n"
  42. "\n"
  43. "Return a capitalized version of the string.\n"
  44. "\n"
  45. "More specifically, make the first character have upper case and the rest lower\n"
  46. "case.");
  47. #define UNICODE_CAPITALIZE_METHODDEF \
  48. {"capitalize", (PyCFunction)unicode_capitalize, METH_NOARGS, unicode_capitalize__doc__},
  49. static PyObject *
  50. unicode_capitalize_impl(PyObject *self);
  51. static PyObject *
  52. unicode_capitalize(PyObject *self, PyObject *Py_UNUSED(ignored))
  53. {
  54. return unicode_capitalize_impl(self);
  55. }
  56. PyDoc_STRVAR(unicode_casefold__doc__,
  57. "casefold($self, /)\n"
  58. "--\n"
  59. "\n"
  60. "Return a version of the string suitable for caseless comparisons.");
  61. #define UNICODE_CASEFOLD_METHODDEF \
  62. {"casefold", (PyCFunction)unicode_casefold, METH_NOARGS, unicode_casefold__doc__},
  63. static PyObject *
  64. unicode_casefold_impl(PyObject *self);
  65. static PyObject *
  66. unicode_casefold(PyObject *self, PyObject *Py_UNUSED(ignored))
  67. {
  68. return unicode_casefold_impl(self);
  69. }
  70. PyDoc_STRVAR(unicode_center__doc__,
  71. "center($self, width, fillchar=\' \', /)\n"
  72. "--\n"
  73. "\n"
  74. "Return a centered string of length width.\n"
  75. "\n"
  76. "Padding is done using the specified fill character (default is a space).");
  77. #define UNICODE_CENTER_METHODDEF \
  78. {"center", _PyCFunction_CAST(unicode_center), METH_FASTCALL, unicode_center__doc__},
  79. static PyObject *
  80. unicode_center_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
  81. static PyObject *
  82. unicode_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  83. {
  84. PyObject *return_value = NULL;
  85. Py_ssize_t width;
  86. Py_UCS4 fillchar = ' ';
  87. if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
  88. goto exit;
  89. }
  90. {
  91. Py_ssize_t ival = -1;
  92. PyObject *iobj = _PyNumber_Index(args[0]);
  93. if (iobj != NULL) {
  94. ival = PyLong_AsSsize_t(iobj);
  95. Py_DECREF(iobj);
  96. }
  97. if (ival == -1 && PyErr_Occurred()) {
  98. goto exit;
  99. }
  100. width = ival;
  101. }
  102. if (nargs < 2) {
  103. goto skip_optional;
  104. }
  105. if (!convert_uc(args[1], &fillchar)) {
  106. goto exit;
  107. }
  108. skip_optional:
  109. return_value = unicode_center_impl(self, width, fillchar);
  110. exit:
  111. return return_value;
  112. }
  113. PyDoc_STRVAR(unicode_encode__doc__,
  114. "encode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
  115. "--\n"
  116. "\n"
  117. "Encode the string using the codec registered for encoding.\n"
  118. "\n"
  119. " encoding\n"
  120. " The encoding in which to encode the string.\n"
  121. " errors\n"
  122. " The error handling scheme to use for encoding errors.\n"
  123. " The default is \'strict\' meaning that encoding errors raise a\n"
  124. " UnicodeEncodeError. Other possible values are \'ignore\', \'replace\' and\n"
  125. " \'xmlcharrefreplace\' as well as any other name registered with\n"
  126. " codecs.register_error that can handle UnicodeEncodeErrors.");
  127. #define UNICODE_ENCODE_METHODDEF \
  128. {"encode", _PyCFunction_CAST(unicode_encode), METH_FASTCALL|METH_KEYWORDS, unicode_encode__doc__},
  129. static PyObject *
  130. unicode_encode_impl(PyObject *self, const char *encoding, const char *errors);
  131. static PyObject *
  132. unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  133. {
  134. PyObject *return_value = NULL;
  135. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  136. #define NUM_KEYWORDS 2
  137. static struct {
  138. PyGC_Head _this_is_not_used;
  139. PyObject_VAR_HEAD
  140. PyObject *ob_item[NUM_KEYWORDS];
  141. } _kwtuple = {
  142. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  143. .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
  144. };
  145. #undef NUM_KEYWORDS
  146. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  147. #else // !Py_BUILD_CORE
  148. # define KWTUPLE NULL
  149. #endif // !Py_BUILD_CORE
  150. static const char * const _keywords[] = {"encoding", "errors", NULL};
  151. static _PyArg_Parser _parser = {
  152. .keywords = _keywords,
  153. .fname = "encode",
  154. .kwtuple = KWTUPLE,
  155. };
  156. #undef KWTUPLE
  157. PyObject *argsbuf[2];
  158. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  159. const char *encoding = NULL;
  160. const char *errors = NULL;
  161. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  162. if (!args) {
  163. goto exit;
  164. }
  165. if (!noptargs) {
  166. goto skip_optional_pos;
  167. }
  168. if (args[0]) {
  169. if (!PyUnicode_Check(args[0])) {
  170. _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]);
  171. goto exit;
  172. }
  173. Py_ssize_t encoding_length;
  174. encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
  175. if (encoding == NULL) {
  176. goto exit;
  177. }
  178. if (strlen(encoding) != (size_t)encoding_length) {
  179. PyErr_SetString(PyExc_ValueError, "embedded null character");
  180. goto exit;
  181. }
  182. if (!--noptargs) {
  183. goto skip_optional_pos;
  184. }
  185. }
  186. if (!PyUnicode_Check(args[1])) {
  187. _PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]);
  188. goto exit;
  189. }
  190. Py_ssize_t errors_length;
  191. errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
  192. if (errors == NULL) {
  193. goto exit;
  194. }
  195. if (strlen(errors) != (size_t)errors_length) {
  196. PyErr_SetString(PyExc_ValueError, "embedded null character");
  197. goto exit;
  198. }
  199. skip_optional_pos:
  200. return_value = unicode_encode_impl(self, encoding, errors);
  201. exit:
  202. return return_value;
  203. }
  204. PyDoc_STRVAR(unicode_expandtabs__doc__,
  205. "expandtabs($self, /, tabsize=8)\n"
  206. "--\n"
  207. "\n"
  208. "Return a copy where all tab characters are expanded using spaces.\n"
  209. "\n"
  210. "If tabsize is not given, a tab size of 8 characters is assumed.");
  211. #define UNICODE_EXPANDTABS_METHODDEF \
  212. {"expandtabs", _PyCFunction_CAST(unicode_expandtabs), METH_FASTCALL|METH_KEYWORDS, unicode_expandtabs__doc__},
  213. static PyObject *
  214. unicode_expandtabs_impl(PyObject *self, int tabsize);
  215. static PyObject *
  216. unicode_expandtabs(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  217. {
  218. PyObject *return_value = NULL;
  219. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  220. #define NUM_KEYWORDS 1
  221. static struct {
  222. PyGC_Head _this_is_not_used;
  223. PyObject_VAR_HEAD
  224. PyObject *ob_item[NUM_KEYWORDS];
  225. } _kwtuple = {
  226. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  227. .ob_item = { &_Py_ID(tabsize), },
  228. };
  229. #undef NUM_KEYWORDS
  230. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  231. #else // !Py_BUILD_CORE
  232. # define KWTUPLE NULL
  233. #endif // !Py_BUILD_CORE
  234. static const char * const _keywords[] = {"tabsize", NULL};
  235. static _PyArg_Parser _parser = {
  236. .keywords = _keywords,
  237. .fname = "expandtabs",
  238. .kwtuple = KWTUPLE,
  239. };
  240. #undef KWTUPLE
  241. PyObject *argsbuf[1];
  242. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  243. int tabsize = 8;
  244. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  245. if (!args) {
  246. goto exit;
  247. }
  248. if (!noptargs) {
  249. goto skip_optional_pos;
  250. }
  251. tabsize = _PyLong_AsInt(args[0]);
  252. if (tabsize == -1 && PyErr_Occurred()) {
  253. goto exit;
  254. }
  255. skip_optional_pos:
  256. return_value = unicode_expandtabs_impl(self, tabsize);
  257. exit:
  258. return return_value;
  259. }
  260. PyDoc_STRVAR(unicode_isascii__doc__,
  261. "isascii($self, /)\n"
  262. "--\n"
  263. "\n"
  264. "Return True if all characters in the string are ASCII, False otherwise.\n"
  265. "\n"
  266. "ASCII characters have code points in the range U+0000-U+007F.\n"
  267. "Empty string is ASCII too.");
  268. #define UNICODE_ISASCII_METHODDEF \
  269. {"isascii", (PyCFunction)unicode_isascii, METH_NOARGS, unicode_isascii__doc__},
  270. static PyObject *
  271. unicode_isascii_impl(PyObject *self);
  272. static PyObject *
  273. unicode_isascii(PyObject *self, PyObject *Py_UNUSED(ignored))
  274. {
  275. return unicode_isascii_impl(self);
  276. }
  277. PyDoc_STRVAR(unicode_islower__doc__,
  278. "islower($self, /)\n"
  279. "--\n"
  280. "\n"
  281. "Return True if the string is a lowercase string, False otherwise.\n"
  282. "\n"
  283. "A string is lowercase if all cased characters in the string are lowercase and\n"
  284. "there is at least one cased character in the string.");
  285. #define UNICODE_ISLOWER_METHODDEF \
  286. {"islower", (PyCFunction)unicode_islower, METH_NOARGS, unicode_islower__doc__},
  287. static PyObject *
  288. unicode_islower_impl(PyObject *self);
  289. static PyObject *
  290. unicode_islower(PyObject *self, PyObject *Py_UNUSED(ignored))
  291. {
  292. return unicode_islower_impl(self);
  293. }
  294. PyDoc_STRVAR(unicode_isupper__doc__,
  295. "isupper($self, /)\n"
  296. "--\n"
  297. "\n"
  298. "Return True if the string is an uppercase string, False otherwise.\n"
  299. "\n"
  300. "A string is uppercase if all cased characters in the string are uppercase and\n"
  301. "there is at least one cased character in the string.");
  302. #define UNICODE_ISUPPER_METHODDEF \
  303. {"isupper", (PyCFunction)unicode_isupper, METH_NOARGS, unicode_isupper__doc__},
  304. static PyObject *
  305. unicode_isupper_impl(PyObject *self);
  306. static PyObject *
  307. unicode_isupper(PyObject *self, PyObject *Py_UNUSED(ignored))
  308. {
  309. return unicode_isupper_impl(self);
  310. }
  311. PyDoc_STRVAR(unicode_istitle__doc__,
  312. "istitle($self, /)\n"
  313. "--\n"
  314. "\n"
  315. "Return True if the string is a title-cased string, False otherwise.\n"
  316. "\n"
  317. "In a title-cased string, upper- and title-case characters may only\n"
  318. "follow uncased characters and lowercase characters only cased ones.");
  319. #define UNICODE_ISTITLE_METHODDEF \
  320. {"istitle", (PyCFunction)unicode_istitle, METH_NOARGS, unicode_istitle__doc__},
  321. static PyObject *
  322. unicode_istitle_impl(PyObject *self);
  323. static PyObject *
  324. unicode_istitle(PyObject *self, PyObject *Py_UNUSED(ignored))
  325. {
  326. return unicode_istitle_impl(self);
  327. }
  328. PyDoc_STRVAR(unicode_isspace__doc__,
  329. "isspace($self, /)\n"
  330. "--\n"
  331. "\n"
  332. "Return True if the string is a whitespace string, False otherwise.\n"
  333. "\n"
  334. "A string is whitespace if all characters in the string are whitespace and there\n"
  335. "is at least one character in the string.");
  336. #define UNICODE_ISSPACE_METHODDEF \
  337. {"isspace", (PyCFunction)unicode_isspace, METH_NOARGS, unicode_isspace__doc__},
  338. static PyObject *
  339. unicode_isspace_impl(PyObject *self);
  340. static PyObject *
  341. unicode_isspace(PyObject *self, PyObject *Py_UNUSED(ignored))
  342. {
  343. return unicode_isspace_impl(self);
  344. }
  345. PyDoc_STRVAR(unicode_isalpha__doc__,
  346. "isalpha($self, /)\n"
  347. "--\n"
  348. "\n"
  349. "Return True if the string is an alphabetic string, False otherwise.\n"
  350. "\n"
  351. "A string is alphabetic if all characters in the string are alphabetic and there\n"
  352. "is at least one character in the string.");
  353. #define UNICODE_ISALPHA_METHODDEF \
  354. {"isalpha", (PyCFunction)unicode_isalpha, METH_NOARGS, unicode_isalpha__doc__},
  355. static PyObject *
  356. unicode_isalpha_impl(PyObject *self);
  357. static PyObject *
  358. unicode_isalpha(PyObject *self, PyObject *Py_UNUSED(ignored))
  359. {
  360. return unicode_isalpha_impl(self);
  361. }
  362. PyDoc_STRVAR(unicode_isalnum__doc__,
  363. "isalnum($self, /)\n"
  364. "--\n"
  365. "\n"
  366. "Return True if the string is an alpha-numeric string, False otherwise.\n"
  367. "\n"
  368. "A string is alpha-numeric if all characters in the string are alpha-numeric and\n"
  369. "there is at least one character in the string.");
  370. #define UNICODE_ISALNUM_METHODDEF \
  371. {"isalnum", (PyCFunction)unicode_isalnum, METH_NOARGS, unicode_isalnum__doc__},
  372. static PyObject *
  373. unicode_isalnum_impl(PyObject *self);
  374. static PyObject *
  375. unicode_isalnum(PyObject *self, PyObject *Py_UNUSED(ignored))
  376. {
  377. return unicode_isalnum_impl(self);
  378. }
  379. PyDoc_STRVAR(unicode_isdecimal__doc__,
  380. "isdecimal($self, /)\n"
  381. "--\n"
  382. "\n"
  383. "Return True if the string is a decimal string, False otherwise.\n"
  384. "\n"
  385. "A string is a decimal string if all characters in the string are decimal and\n"
  386. "there is at least one character in the string.");
  387. #define UNICODE_ISDECIMAL_METHODDEF \
  388. {"isdecimal", (PyCFunction)unicode_isdecimal, METH_NOARGS, unicode_isdecimal__doc__},
  389. static PyObject *
  390. unicode_isdecimal_impl(PyObject *self);
  391. static PyObject *
  392. unicode_isdecimal(PyObject *self, PyObject *Py_UNUSED(ignored))
  393. {
  394. return unicode_isdecimal_impl(self);
  395. }
  396. PyDoc_STRVAR(unicode_isdigit__doc__,
  397. "isdigit($self, /)\n"
  398. "--\n"
  399. "\n"
  400. "Return True if the string is a digit string, False otherwise.\n"
  401. "\n"
  402. "A string is a digit string if all characters in the string are digits and there\n"
  403. "is at least one character in the string.");
  404. #define UNICODE_ISDIGIT_METHODDEF \
  405. {"isdigit", (PyCFunction)unicode_isdigit, METH_NOARGS, unicode_isdigit__doc__},
  406. static PyObject *
  407. unicode_isdigit_impl(PyObject *self);
  408. static PyObject *
  409. unicode_isdigit(PyObject *self, PyObject *Py_UNUSED(ignored))
  410. {
  411. return unicode_isdigit_impl(self);
  412. }
  413. PyDoc_STRVAR(unicode_isnumeric__doc__,
  414. "isnumeric($self, /)\n"
  415. "--\n"
  416. "\n"
  417. "Return True if the string is a numeric string, False otherwise.\n"
  418. "\n"
  419. "A string is numeric if all characters in the string are numeric and there is at\n"
  420. "least one character in the string.");
  421. #define UNICODE_ISNUMERIC_METHODDEF \
  422. {"isnumeric", (PyCFunction)unicode_isnumeric, METH_NOARGS, unicode_isnumeric__doc__},
  423. static PyObject *
  424. unicode_isnumeric_impl(PyObject *self);
  425. static PyObject *
  426. unicode_isnumeric(PyObject *self, PyObject *Py_UNUSED(ignored))
  427. {
  428. return unicode_isnumeric_impl(self);
  429. }
  430. PyDoc_STRVAR(unicode_isidentifier__doc__,
  431. "isidentifier($self, /)\n"
  432. "--\n"
  433. "\n"
  434. "Return True if the string is a valid Python identifier, False otherwise.\n"
  435. "\n"
  436. "Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n"
  437. "such as \"def\" or \"class\".");
  438. #define UNICODE_ISIDENTIFIER_METHODDEF \
  439. {"isidentifier", (PyCFunction)unicode_isidentifier, METH_NOARGS, unicode_isidentifier__doc__},
  440. static PyObject *
  441. unicode_isidentifier_impl(PyObject *self);
  442. static PyObject *
  443. unicode_isidentifier(PyObject *self, PyObject *Py_UNUSED(ignored))
  444. {
  445. return unicode_isidentifier_impl(self);
  446. }
  447. PyDoc_STRVAR(unicode_isprintable__doc__,
  448. "isprintable($self, /)\n"
  449. "--\n"
  450. "\n"
  451. "Return True if the string is printable, False otherwise.\n"
  452. "\n"
  453. "A string is printable if all of its characters are considered printable in\n"
  454. "repr() or if it is empty.");
  455. #define UNICODE_ISPRINTABLE_METHODDEF \
  456. {"isprintable", (PyCFunction)unicode_isprintable, METH_NOARGS, unicode_isprintable__doc__},
  457. static PyObject *
  458. unicode_isprintable_impl(PyObject *self);
  459. static PyObject *
  460. unicode_isprintable(PyObject *self, PyObject *Py_UNUSED(ignored))
  461. {
  462. return unicode_isprintable_impl(self);
  463. }
  464. PyDoc_STRVAR(unicode_join__doc__,
  465. "join($self, iterable, /)\n"
  466. "--\n"
  467. "\n"
  468. "Concatenate any number of strings.\n"
  469. "\n"
  470. "The string whose method is called is inserted in between each given string.\n"
  471. "The result is returned as a new string.\n"
  472. "\n"
  473. "Example: \'.\'.join([\'ab\', \'pq\', \'rs\']) -> \'ab.pq.rs\'");
  474. #define UNICODE_JOIN_METHODDEF \
  475. {"join", (PyCFunction)unicode_join, METH_O, unicode_join__doc__},
  476. PyDoc_STRVAR(unicode_ljust__doc__,
  477. "ljust($self, width, fillchar=\' \', /)\n"
  478. "--\n"
  479. "\n"
  480. "Return a left-justified string of length width.\n"
  481. "\n"
  482. "Padding is done using the specified fill character (default is a space).");
  483. #define UNICODE_LJUST_METHODDEF \
  484. {"ljust", _PyCFunction_CAST(unicode_ljust), METH_FASTCALL, unicode_ljust__doc__},
  485. static PyObject *
  486. unicode_ljust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
  487. static PyObject *
  488. unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  489. {
  490. PyObject *return_value = NULL;
  491. Py_ssize_t width;
  492. Py_UCS4 fillchar = ' ';
  493. if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
  494. goto exit;
  495. }
  496. {
  497. Py_ssize_t ival = -1;
  498. PyObject *iobj = _PyNumber_Index(args[0]);
  499. if (iobj != NULL) {
  500. ival = PyLong_AsSsize_t(iobj);
  501. Py_DECREF(iobj);
  502. }
  503. if (ival == -1 && PyErr_Occurred()) {
  504. goto exit;
  505. }
  506. width = ival;
  507. }
  508. if (nargs < 2) {
  509. goto skip_optional;
  510. }
  511. if (!convert_uc(args[1], &fillchar)) {
  512. goto exit;
  513. }
  514. skip_optional:
  515. return_value = unicode_ljust_impl(self, width, fillchar);
  516. exit:
  517. return return_value;
  518. }
  519. PyDoc_STRVAR(unicode_lower__doc__,
  520. "lower($self, /)\n"
  521. "--\n"
  522. "\n"
  523. "Return a copy of the string converted to lowercase.");
  524. #define UNICODE_LOWER_METHODDEF \
  525. {"lower", (PyCFunction)unicode_lower, METH_NOARGS, unicode_lower__doc__},
  526. static PyObject *
  527. unicode_lower_impl(PyObject *self);
  528. static PyObject *
  529. unicode_lower(PyObject *self, PyObject *Py_UNUSED(ignored))
  530. {
  531. return unicode_lower_impl(self);
  532. }
  533. PyDoc_STRVAR(unicode_strip__doc__,
  534. "strip($self, chars=None, /)\n"
  535. "--\n"
  536. "\n"
  537. "Return a copy of the string with leading and trailing whitespace removed.\n"
  538. "\n"
  539. "If chars is given and not None, remove characters in chars instead.");
  540. #define UNICODE_STRIP_METHODDEF \
  541. {"strip", _PyCFunction_CAST(unicode_strip), METH_FASTCALL, unicode_strip__doc__},
  542. static PyObject *
  543. unicode_strip_impl(PyObject *self, PyObject *chars);
  544. static PyObject *
  545. unicode_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  546. {
  547. PyObject *return_value = NULL;
  548. PyObject *chars = Py_None;
  549. if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
  550. goto exit;
  551. }
  552. if (nargs < 1) {
  553. goto skip_optional;
  554. }
  555. chars = args[0];
  556. skip_optional:
  557. return_value = unicode_strip_impl(self, chars);
  558. exit:
  559. return return_value;
  560. }
  561. PyDoc_STRVAR(unicode_lstrip__doc__,
  562. "lstrip($self, chars=None, /)\n"
  563. "--\n"
  564. "\n"
  565. "Return a copy of the string with leading whitespace removed.\n"
  566. "\n"
  567. "If chars is given and not None, remove characters in chars instead.");
  568. #define UNICODE_LSTRIP_METHODDEF \
  569. {"lstrip", _PyCFunction_CAST(unicode_lstrip), METH_FASTCALL, unicode_lstrip__doc__},
  570. static PyObject *
  571. unicode_lstrip_impl(PyObject *self, PyObject *chars);
  572. static PyObject *
  573. unicode_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  574. {
  575. PyObject *return_value = NULL;
  576. PyObject *chars = Py_None;
  577. if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
  578. goto exit;
  579. }
  580. if (nargs < 1) {
  581. goto skip_optional;
  582. }
  583. chars = args[0];
  584. skip_optional:
  585. return_value = unicode_lstrip_impl(self, chars);
  586. exit:
  587. return return_value;
  588. }
  589. PyDoc_STRVAR(unicode_rstrip__doc__,
  590. "rstrip($self, chars=None, /)\n"
  591. "--\n"
  592. "\n"
  593. "Return a copy of the string with trailing whitespace removed.\n"
  594. "\n"
  595. "If chars is given and not None, remove characters in chars instead.");
  596. #define UNICODE_RSTRIP_METHODDEF \
  597. {"rstrip", _PyCFunction_CAST(unicode_rstrip), METH_FASTCALL, unicode_rstrip__doc__},
  598. static PyObject *
  599. unicode_rstrip_impl(PyObject *self, PyObject *chars);
  600. static PyObject *
  601. unicode_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  602. {
  603. PyObject *return_value = NULL;
  604. PyObject *chars = Py_None;
  605. if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
  606. goto exit;
  607. }
  608. if (nargs < 1) {
  609. goto skip_optional;
  610. }
  611. chars = args[0];
  612. skip_optional:
  613. return_value = unicode_rstrip_impl(self, chars);
  614. exit:
  615. return return_value;
  616. }
  617. PyDoc_STRVAR(unicode_replace__doc__,
  618. "replace($self, old, new, count=-1, /)\n"
  619. "--\n"
  620. "\n"
  621. "Return a copy with all occurrences of substring old replaced by new.\n"
  622. "\n"
  623. " count\n"
  624. " Maximum number of occurrences to replace.\n"
  625. " -1 (the default value) means replace all occurrences.\n"
  626. "\n"
  627. "If the optional argument count is given, only the first count occurrences are\n"
  628. "replaced.");
  629. #define UNICODE_REPLACE_METHODDEF \
  630. {"replace", _PyCFunction_CAST(unicode_replace), METH_FASTCALL, unicode_replace__doc__},
  631. static PyObject *
  632. unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
  633. Py_ssize_t count);
  634. static PyObject *
  635. unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  636. {
  637. PyObject *return_value = NULL;
  638. PyObject *old;
  639. PyObject *new;
  640. Py_ssize_t count = -1;
  641. if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
  642. goto exit;
  643. }
  644. if (!PyUnicode_Check(args[0])) {
  645. _PyArg_BadArgument("replace", "argument 1", "str", args[0]);
  646. goto exit;
  647. }
  648. if (PyUnicode_READY(args[0]) == -1) {
  649. goto exit;
  650. }
  651. old = args[0];
  652. if (!PyUnicode_Check(args[1])) {
  653. _PyArg_BadArgument("replace", "argument 2", "str", args[1]);
  654. goto exit;
  655. }
  656. if (PyUnicode_READY(args[1]) == -1) {
  657. goto exit;
  658. }
  659. new = args[1];
  660. if (nargs < 3) {
  661. goto skip_optional;
  662. }
  663. {
  664. Py_ssize_t ival = -1;
  665. PyObject *iobj = _PyNumber_Index(args[2]);
  666. if (iobj != NULL) {
  667. ival = PyLong_AsSsize_t(iobj);
  668. Py_DECREF(iobj);
  669. }
  670. if (ival == -1 && PyErr_Occurred()) {
  671. goto exit;
  672. }
  673. count = ival;
  674. }
  675. skip_optional:
  676. return_value = unicode_replace_impl(self, old, new, count);
  677. exit:
  678. return return_value;
  679. }
  680. PyDoc_STRVAR(unicode_removeprefix__doc__,
  681. "removeprefix($self, prefix, /)\n"
  682. "--\n"
  683. "\n"
  684. "Return a str with the given prefix string removed if present.\n"
  685. "\n"
  686. "If the string starts with the prefix string, return string[len(prefix):].\n"
  687. "Otherwise, return a copy of the original string.");
  688. #define UNICODE_REMOVEPREFIX_METHODDEF \
  689. {"removeprefix", (PyCFunction)unicode_removeprefix, METH_O, unicode_removeprefix__doc__},
  690. static PyObject *
  691. unicode_removeprefix_impl(PyObject *self, PyObject *prefix);
  692. static PyObject *
  693. unicode_removeprefix(PyObject *self, PyObject *arg)
  694. {
  695. PyObject *return_value = NULL;
  696. PyObject *prefix;
  697. if (!PyUnicode_Check(arg)) {
  698. _PyArg_BadArgument("removeprefix", "argument", "str", arg);
  699. goto exit;
  700. }
  701. if (PyUnicode_READY(arg) == -1) {
  702. goto exit;
  703. }
  704. prefix = arg;
  705. return_value = unicode_removeprefix_impl(self, prefix);
  706. exit:
  707. return return_value;
  708. }
  709. PyDoc_STRVAR(unicode_removesuffix__doc__,
  710. "removesuffix($self, suffix, /)\n"
  711. "--\n"
  712. "\n"
  713. "Return a str with the given suffix string removed if present.\n"
  714. "\n"
  715. "If the string ends with the suffix string and that suffix is not empty,\n"
  716. "return string[:-len(suffix)]. Otherwise, return a copy of the original\n"
  717. "string.");
  718. #define UNICODE_REMOVESUFFIX_METHODDEF \
  719. {"removesuffix", (PyCFunction)unicode_removesuffix, METH_O, unicode_removesuffix__doc__},
  720. static PyObject *
  721. unicode_removesuffix_impl(PyObject *self, PyObject *suffix);
  722. static PyObject *
  723. unicode_removesuffix(PyObject *self, PyObject *arg)
  724. {
  725. PyObject *return_value = NULL;
  726. PyObject *suffix;
  727. if (!PyUnicode_Check(arg)) {
  728. _PyArg_BadArgument("removesuffix", "argument", "str", arg);
  729. goto exit;
  730. }
  731. if (PyUnicode_READY(arg) == -1) {
  732. goto exit;
  733. }
  734. suffix = arg;
  735. return_value = unicode_removesuffix_impl(self, suffix);
  736. exit:
  737. return return_value;
  738. }
  739. PyDoc_STRVAR(unicode_rjust__doc__,
  740. "rjust($self, width, fillchar=\' \', /)\n"
  741. "--\n"
  742. "\n"
  743. "Return a right-justified string of length width.\n"
  744. "\n"
  745. "Padding is done using the specified fill character (default is a space).");
  746. #define UNICODE_RJUST_METHODDEF \
  747. {"rjust", _PyCFunction_CAST(unicode_rjust), METH_FASTCALL, unicode_rjust__doc__},
  748. static PyObject *
  749. unicode_rjust_impl(PyObject *self, Py_ssize_t width, Py_UCS4 fillchar);
  750. static PyObject *
  751. unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
  752. {
  753. PyObject *return_value = NULL;
  754. Py_ssize_t width;
  755. Py_UCS4 fillchar = ' ';
  756. if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
  757. goto exit;
  758. }
  759. {
  760. Py_ssize_t ival = -1;
  761. PyObject *iobj = _PyNumber_Index(args[0]);
  762. if (iobj != NULL) {
  763. ival = PyLong_AsSsize_t(iobj);
  764. Py_DECREF(iobj);
  765. }
  766. if (ival == -1 && PyErr_Occurred()) {
  767. goto exit;
  768. }
  769. width = ival;
  770. }
  771. if (nargs < 2) {
  772. goto skip_optional;
  773. }
  774. if (!convert_uc(args[1], &fillchar)) {
  775. goto exit;
  776. }
  777. skip_optional:
  778. return_value = unicode_rjust_impl(self, width, fillchar);
  779. exit:
  780. return return_value;
  781. }
  782. PyDoc_STRVAR(unicode_split__doc__,
  783. "split($self, /, sep=None, maxsplit=-1)\n"
  784. "--\n"
  785. "\n"
  786. "Return a list of the substrings in the string, using sep as the separator string.\n"
  787. "\n"
  788. " sep\n"
  789. " The separator used to split the string.\n"
  790. "\n"
  791. " When set to None (the default value), will split on any whitespace\n"
  792. " character (including \\n \\r \\t \\f and spaces) and will discard\n"
  793. " empty strings from the result.\n"
  794. " maxsplit\n"
  795. " Maximum number of splits.\n"
  796. " -1 (the default value) means no limit.\n"
  797. "\n"
  798. "Splitting starts at the front of the string and works to the end.\n"
  799. "\n"
  800. "Note, str.split() is mainly useful for data that has been intentionally\n"
  801. "delimited. With natural text that includes punctuation, consider using\n"
  802. "the regular expression module.");
  803. #define UNICODE_SPLIT_METHODDEF \
  804. {"split", _PyCFunction_CAST(unicode_split), METH_FASTCALL|METH_KEYWORDS, unicode_split__doc__},
  805. static PyObject *
  806. unicode_split_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
  807. static PyObject *
  808. unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  809. {
  810. PyObject *return_value = NULL;
  811. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  812. #define NUM_KEYWORDS 2
  813. static struct {
  814. PyGC_Head _this_is_not_used;
  815. PyObject_VAR_HEAD
  816. PyObject *ob_item[NUM_KEYWORDS];
  817. } _kwtuple = {
  818. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  819. .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
  820. };
  821. #undef NUM_KEYWORDS
  822. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  823. #else // !Py_BUILD_CORE
  824. # define KWTUPLE NULL
  825. #endif // !Py_BUILD_CORE
  826. static const char * const _keywords[] = {"sep", "maxsplit", NULL};
  827. static _PyArg_Parser _parser = {
  828. .keywords = _keywords,
  829. .fname = "split",
  830. .kwtuple = KWTUPLE,
  831. };
  832. #undef KWTUPLE
  833. PyObject *argsbuf[2];
  834. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  835. PyObject *sep = Py_None;
  836. Py_ssize_t maxsplit = -1;
  837. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  838. if (!args) {
  839. goto exit;
  840. }
  841. if (!noptargs) {
  842. goto skip_optional_pos;
  843. }
  844. if (args[0]) {
  845. sep = args[0];
  846. if (!--noptargs) {
  847. goto skip_optional_pos;
  848. }
  849. }
  850. {
  851. Py_ssize_t ival = -1;
  852. PyObject *iobj = _PyNumber_Index(args[1]);
  853. if (iobj != NULL) {
  854. ival = PyLong_AsSsize_t(iobj);
  855. Py_DECREF(iobj);
  856. }
  857. if (ival == -1 && PyErr_Occurred()) {
  858. goto exit;
  859. }
  860. maxsplit = ival;
  861. }
  862. skip_optional_pos:
  863. return_value = unicode_split_impl(self, sep, maxsplit);
  864. exit:
  865. return return_value;
  866. }
  867. PyDoc_STRVAR(unicode_partition__doc__,
  868. "partition($self, sep, /)\n"
  869. "--\n"
  870. "\n"
  871. "Partition the string into three parts using the given separator.\n"
  872. "\n"
  873. "This will search for the separator in the string. If the separator is found,\n"
  874. "returns a 3-tuple containing the part before the separator, the separator\n"
  875. "itself, and the part after it.\n"
  876. "\n"
  877. "If the separator is not found, returns a 3-tuple containing the original string\n"
  878. "and two empty strings.");
  879. #define UNICODE_PARTITION_METHODDEF \
  880. {"partition", (PyCFunction)unicode_partition, METH_O, unicode_partition__doc__},
  881. PyDoc_STRVAR(unicode_rpartition__doc__,
  882. "rpartition($self, sep, /)\n"
  883. "--\n"
  884. "\n"
  885. "Partition the string into three parts using the given separator.\n"
  886. "\n"
  887. "This will search for the separator in the string, starting at the end. If\n"
  888. "the separator is found, returns a 3-tuple containing the part before the\n"
  889. "separator, the separator itself, and the part after it.\n"
  890. "\n"
  891. "If the separator is not found, returns a 3-tuple containing two empty strings\n"
  892. "and the original string.");
  893. #define UNICODE_RPARTITION_METHODDEF \
  894. {"rpartition", (PyCFunction)unicode_rpartition, METH_O, unicode_rpartition__doc__},
  895. PyDoc_STRVAR(unicode_rsplit__doc__,
  896. "rsplit($self, /, sep=None, maxsplit=-1)\n"
  897. "--\n"
  898. "\n"
  899. "Return a list of the substrings in the string, using sep as the separator string.\n"
  900. "\n"
  901. " sep\n"
  902. " The separator used to split the string.\n"
  903. "\n"
  904. " When set to None (the default value), will split on any whitespace\n"
  905. " character (including \\n \\r \\t \\f and spaces) and will discard\n"
  906. " empty strings from the result.\n"
  907. " maxsplit\n"
  908. " Maximum number of splits.\n"
  909. " -1 (the default value) means no limit.\n"
  910. "\n"
  911. "Splitting starts at the end of the string and works to the front.");
  912. #define UNICODE_RSPLIT_METHODDEF \
  913. {"rsplit", _PyCFunction_CAST(unicode_rsplit), METH_FASTCALL|METH_KEYWORDS, unicode_rsplit__doc__},
  914. static PyObject *
  915. unicode_rsplit_impl(PyObject *self, PyObject *sep, Py_ssize_t maxsplit);
  916. static PyObject *
  917. unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  918. {
  919. PyObject *return_value = NULL;
  920. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  921. #define NUM_KEYWORDS 2
  922. static struct {
  923. PyGC_Head _this_is_not_used;
  924. PyObject_VAR_HEAD
  925. PyObject *ob_item[NUM_KEYWORDS];
  926. } _kwtuple = {
  927. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  928. .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
  929. };
  930. #undef NUM_KEYWORDS
  931. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  932. #else // !Py_BUILD_CORE
  933. # define KWTUPLE NULL
  934. #endif // !Py_BUILD_CORE
  935. static const char * const _keywords[] = {"sep", "maxsplit", NULL};
  936. static _PyArg_Parser _parser = {
  937. .keywords = _keywords,
  938. .fname = "rsplit",
  939. .kwtuple = KWTUPLE,
  940. };
  941. #undef KWTUPLE
  942. PyObject *argsbuf[2];
  943. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  944. PyObject *sep = Py_None;
  945. Py_ssize_t maxsplit = -1;
  946. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  947. if (!args) {
  948. goto exit;
  949. }
  950. if (!noptargs) {
  951. goto skip_optional_pos;
  952. }
  953. if (args[0]) {
  954. sep = args[0];
  955. if (!--noptargs) {
  956. goto skip_optional_pos;
  957. }
  958. }
  959. {
  960. Py_ssize_t ival = -1;
  961. PyObject *iobj = _PyNumber_Index(args[1]);
  962. if (iobj != NULL) {
  963. ival = PyLong_AsSsize_t(iobj);
  964. Py_DECREF(iobj);
  965. }
  966. if (ival == -1 && PyErr_Occurred()) {
  967. goto exit;
  968. }
  969. maxsplit = ival;
  970. }
  971. skip_optional_pos:
  972. return_value = unicode_rsplit_impl(self, sep, maxsplit);
  973. exit:
  974. return return_value;
  975. }
  976. PyDoc_STRVAR(unicode_splitlines__doc__,
  977. "splitlines($self, /, keepends=False)\n"
  978. "--\n"
  979. "\n"
  980. "Return a list of the lines in the string, breaking at line boundaries.\n"
  981. "\n"
  982. "Line breaks are not included in the resulting list unless keepends is given and\n"
  983. "true.");
  984. #define UNICODE_SPLITLINES_METHODDEF \
  985. {"splitlines", _PyCFunction_CAST(unicode_splitlines), METH_FASTCALL|METH_KEYWORDS, unicode_splitlines__doc__},
  986. static PyObject *
  987. unicode_splitlines_impl(PyObject *self, int keepends);
  988. static PyObject *
  989. unicode_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  990. {
  991. PyObject *return_value = NULL;
  992. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  993. #define NUM_KEYWORDS 1
  994. static struct {
  995. PyGC_Head _this_is_not_used;
  996. PyObject_VAR_HEAD
  997. PyObject *ob_item[NUM_KEYWORDS];
  998. } _kwtuple = {
  999. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  1000. .ob_item = { &_Py_ID(keepends), },
  1001. };
  1002. #undef NUM_KEYWORDS
  1003. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  1004. #else // !Py_BUILD_CORE
  1005. # define KWTUPLE NULL
  1006. #endif // !Py_BUILD_CORE
  1007. static const char * const _keywords[] = {"keepends", NULL};
  1008. static _PyArg_Parser _parser = {
  1009. .keywords = _keywords,
  1010. .fname = "splitlines",
  1011. .kwtuple = KWTUPLE,
  1012. };
  1013. #undef KWTUPLE
  1014. PyObject *argsbuf[1];
  1015. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1016. int keepends = 0;
  1017. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1018. if (!args) {
  1019. goto exit;
  1020. }
  1021. if (!noptargs) {
  1022. goto skip_optional_pos;
  1023. }
  1024. keepends = PyObject_IsTrue(args[0]);
  1025. if (keepends < 0) {
  1026. goto exit;
  1027. }
  1028. skip_optional_pos:
  1029. return_value = unicode_splitlines_impl(self, keepends);
  1030. exit:
  1031. return return_value;
  1032. }
  1033. PyDoc_STRVAR(unicode_swapcase__doc__,
  1034. "swapcase($self, /)\n"
  1035. "--\n"
  1036. "\n"
  1037. "Convert uppercase characters to lowercase and lowercase characters to uppercase.");
  1038. #define UNICODE_SWAPCASE_METHODDEF \
  1039. {"swapcase", (PyCFunction)unicode_swapcase, METH_NOARGS, unicode_swapcase__doc__},
  1040. static PyObject *
  1041. unicode_swapcase_impl(PyObject *self);
  1042. static PyObject *
  1043. unicode_swapcase(PyObject *self, PyObject *Py_UNUSED(ignored))
  1044. {
  1045. return unicode_swapcase_impl(self);
  1046. }
  1047. PyDoc_STRVAR(unicode_maketrans__doc__,
  1048. "maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)\n"
  1049. "--\n"
  1050. "\n"
  1051. "Return a translation table usable for str.translate().\n"
  1052. "\n"
  1053. "If there is only one argument, it must be a dictionary mapping Unicode\n"
  1054. "ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
  1055. "Character keys will be then converted to ordinals.\n"
  1056. "If there are two arguments, they must be strings of equal length, and\n"
  1057. "in the resulting dictionary, each character in x will be mapped to the\n"
  1058. "character at the same position in y. If there is a third argument, it\n"
  1059. "must be a string, whose characters will be mapped to None in the result.");
  1060. #define UNICODE_MAKETRANS_METHODDEF \
  1061. {"maketrans", _PyCFunction_CAST(unicode_maketrans), METH_FASTCALL|METH_STATIC, unicode_maketrans__doc__},
  1062. static PyObject *
  1063. unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z);
  1064. static PyObject *
  1065. unicode_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
  1066. {
  1067. PyObject *return_value = NULL;
  1068. PyObject *x;
  1069. PyObject *y = NULL;
  1070. PyObject *z = NULL;
  1071. if (!_PyArg_CheckPositional("maketrans", nargs, 1, 3)) {
  1072. goto exit;
  1073. }
  1074. x = args[0];
  1075. if (nargs < 2) {
  1076. goto skip_optional;
  1077. }
  1078. if (!PyUnicode_Check(args[1])) {
  1079. _PyArg_BadArgument("maketrans", "argument 2", "str", args[1]);
  1080. goto exit;
  1081. }
  1082. if (PyUnicode_READY(args[1]) == -1) {
  1083. goto exit;
  1084. }
  1085. y = args[1];
  1086. if (nargs < 3) {
  1087. goto skip_optional;
  1088. }
  1089. if (!PyUnicode_Check(args[2])) {
  1090. _PyArg_BadArgument("maketrans", "argument 3", "str", args[2]);
  1091. goto exit;
  1092. }
  1093. if (PyUnicode_READY(args[2]) == -1) {
  1094. goto exit;
  1095. }
  1096. z = args[2];
  1097. skip_optional:
  1098. return_value = unicode_maketrans_impl(x, y, z);
  1099. exit:
  1100. return return_value;
  1101. }
  1102. PyDoc_STRVAR(unicode_translate__doc__,
  1103. "translate($self, table, /)\n"
  1104. "--\n"
  1105. "\n"
  1106. "Replace each character in the string using the given translation table.\n"
  1107. "\n"
  1108. " table\n"
  1109. " Translation table, which must be a mapping of Unicode ordinals to\n"
  1110. " Unicode ordinals, strings, or None.\n"
  1111. "\n"
  1112. "The table must implement lookup/indexing via __getitem__, for instance a\n"
  1113. "dictionary or list. If this operation raises LookupError, the character is\n"
  1114. "left untouched. Characters mapped to None are deleted.");
  1115. #define UNICODE_TRANSLATE_METHODDEF \
  1116. {"translate", (PyCFunction)unicode_translate, METH_O, unicode_translate__doc__},
  1117. PyDoc_STRVAR(unicode_upper__doc__,
  1118. "upper($self, /)\n"
  1119. "--\n"
  1120. "\n"
  1121. "Return a copy of the string converted to uppercase.");
  1122. #define UNICODE_UPPER_METHODDEF \
  1123. {"upper", (PyCFunction)unicode_upper, METH_NOARGS, unicode_upper__doc__},
  1124. static PyObject *
  1125. unicode_upper_impl(PyObject *self);
  1126. static PyObject *
  1127. unicode_upper(PyObject *self, PyObject *Py_UNUSED(ignored))
  1128. {
  1129. return unicode_upper_impl(self);
  1130. }
  1131. PyDoc_STRVAR(unicode_zfill__doc__,
  1132. "zfill($self, width, /)\n"
  1133. "--\n"
  1134. "\n"
  1135. "Pad a numeric string with zeros on the left, to fill a field of the given width.\n"
  1136. "\n"
  1137. "The string is never truncated.");
  1138. #define UNICODE_ZFILL_METHODDEF \
  1139. {"zfill", (PyCFunction)unicode_zfill, METH_O, unicode_zfill__doc__},
  1140. static PyObject *
  1141. unicode_zfill_impl(PyObject *self, Py_ssize_t width);
  1142. static PyObject *
  1143. unicode_zfill(PyObject *self, PyObject *arg)
  1144. {
  1145. PyObject *return_value = NULL;
  1146. Py_ssize_t width;
  1147. {
  1148. Py_ssize_t ival = -1;
  1149. PyObject *iobj = _PyNumber_Index(arg);
  1150. if (iobj != NULL) {
  1151. ival = PyLong_AsSsize_t(iobj);
  1152. Py_DECREF(iobj);
  1153. }
  1154. if (ival == -1 && PyErr_Occurred()) {
  1155. goto exit;
  1156. }
  1157. width = ival;
  1158. }
  1159. return_value = unicode_zfill_impl(self, width);
  1160. exit:
  1161. return return_value;
  1162. }
  1163. PyDoc_STRVAR(unicode___format____doc__,
  1164. "__format__($self, format_spec, /)\n"
  1165. "--\n"
  1166. "\n"
  1167. "Return a formatted version of the string as described by format_spec.");
  1168. #define UNICODE___FORMAT___METHODDEF \
  1169. {"__format__", (PyCFunction)unicode___format__, METH_O, unicode___format____doc__},
  1170. static PyObject *
  1171. unicode___format___impl(PyObject *self, PyObject *format_spec);
  1172. static PyObject *
  1173. unicode___format__(PyObject *self, PyObject *arg)
  1174. {
  1175. PyObject *return_value = NULL;
  1176. PyObject *format_spec;
  1177. if (!PyUnicode_Check(arg)) {
  1178. _PyArg_BadArgument("__format__", "argument", "str", arg);
  1179. goto exit;
  1180. }
  1181. if (PyUnicode_READY(arg) == -1) {
  1182. goto exit;
  1183. }
  1184. format_spec = arg;
  1185. return_value = unicode___format___impl(self, format_spec);
  1186. exit:
  1187. return return_value;
  1188. }
  1189. PyDoc_STRVAR(unicode_sizeof__doc__,
  1190. "__sizeof__($self, /)\n"
  1191. "--\n"
  1192. "\n"
  1193. "Return the size of the string in memory, in bytes.");
  1194. #define UNICODE_SIZEOF_METHODDEF \
  1195. {"__sizeof__", (PyCFunction)unicode_sizeof, METH_NOARGS, unicode_sizeof__doc__},
  1196. static PyObject *
  1197. unicode_sizeof_impl(PyObject *self);
  1198. static PyObject *
  1199. unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
  1200. {
  1201. return unicode_sizeof_impl(self);
  1202. }
  1203. static PyObject *
  1204. unicode_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
  1205. const char *errors);
  1206. static PyObject *
  1207. unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  1208. {
  1209. PyObject *return_value = NULL;
  1210. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  1211. #define NUM_KEYWORDS 3
  1212. static struct {
  1213. PyGC_Head _this_is_not_used;
  1214. PyObject_VAR_HEAD
  1215. PyObject *ob_item[NUM_KEYWORDS];
  1216. } _kwtuple = {
  1217. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  1218. .ob_item = { &_Py_ID(object), &_Py_ID(encoding), &_Py_ID(errors), },
  1219. };
  1220. #undef NUM_KEYWORDS
  1221. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  1222. #else // !Py_BUILD_CORE
  1223. # define KWTUPLE NULL
  1224. #endif // !Py_BUILD_CORE
  1225. static const char * const _keywords[] = {"object", "encoding", "errors", NULL};
  1226. static _PyArg_Parser _parser = {
  1227. .keywords = _keywords,
  1228. .fname = "str",
  1229. .kwtuple = KWTUPLE,
  1230. };
  1231. #undef KWTUPLE
  1232. PyObject *argsbuf[3];
  1233. PyObject * const *fastargs;
  1234. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  1235. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  1236. PyObject *x = NULL;
  1237. const char *encoding = NULL;
  1238. const char *errors = NULL;
  1239. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
  1240. if (!fastargs) {
  1241. goto exit;
  1242. }
  1243. if (!noptargs) {
  1244. goto skip_optional_pos;
  1245. }
  1246. if (fastargs[0]) {
  1247. x = fastargs[0];
  1248. if (!--noptargs) {
  1249. goto skip_optional_pos;
  1250. }
  1251. }
  1252. if (fastargs[1]) {
  1253. if (!PyUnicode_Check(fastargs[1])) {
  1254. _PyArg_BadArgument("str", "argument 'encoding'", "str", fastargs[1]);
  1255. goto exit;
  1256. }
  1257. Py_ssize_t encoding_length;
  1258. encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
  1259. if (encoding == NULL) {
  1260. goto exit;
  1261. }
  1262. if (strlen(encoding) != (size_t)encoding_length) {
  1263. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1264. goto exit;
  1265. }
  1266. if (!--noptargs) {
  1267. goto skip_optional_pos;
  1268. }
  1269. }
  1270. if (!PyUnicode_Check(fastargs[2])) {
  1271. _PyArg_BadArgument("str", "argument 'errors'", "str", fastargs[2]);
  1272. goto exit;
  1273. }
  1274. Py_ssize_t errors_length;
  1275. errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
  1276. if (errors == NULL) {
  1277. goto exit;
  1278. }
  1279. if (strlen(errors) != (size_t)errors_length) {
  1280. PyErr_SetString(PyExc_ValueError, "embedded null character");
  1281. goto exit;
  1282. }
  1283. skip_optional_pos:
  1284. return_value = unicode_new_impl(type, x, encoding, errors);
  1285. exit:
  1286. return return_value;
  1287. }
  1288. /*[clinic end generated code: output=d8f67f37fdbe21c4 input=a9049054013a1b77]*/