/Objects/bytes_methods.c

http://unladen-swallow.googlecode.com/ · C · 610 lines · 490 code · 73 blank · 47 comment · 74 complexity · 7661c091501f5cf621d57e6f458b47e3 MD5 · raw file

  1. #include "Python.h"
  2. #include "bytes_methods.h"
  3. /* Our own locale-independent ctype.h-like macros */
  4. const unsigned int _Py_ctype_table[256] = {
  5. 0, /* 0x0 '\x00' */
  6. 0, /* 0x1 '\x01' */
  7. 0, /* 0x2 '\x02' */
  8. 0, /* 0x3 '\x03' */
  9. 0, /* 0x4 '\x04' */
  10. 0, /* 0x5 '\x05' */
  11. 0, /* 0x6 '\x06' */
  12. 0, /* 0x7 '\x07' */
  13. 0, /* 0x8 '\x08' */
  14. FLAG_SPACE, /* 0x9 '\t' */
  15. FLAG_SPACE, /* 0xa '\n' */
  16. FLAG_SPACE, /* 0xb '\v' */
  17. FLAG_SPACE, /* 0xc '\f' */
  18. FLAG_SPACE, /* 0xd '\r' */
  19. 0, /* 0xe '\x0e' */
  20. 0, /* 0xf '\x0f' */
  21. 0, /* 0x10 '\x10' */
  22. 0, /* 0x11 '\x11' */
  23. 0, /* 0x12 '\x12' */
  24. 0, /* 0x13 '\x13' */
  25. 0, /* 0x14 '\x14' */
  26. 0, /* 0x15 '\x15' */
  27. 0, /* 0x16 '\x16' */
  28. 0, /* 0x17 '\x17' */
  29. 0, /* 0x18 '\x18' */
  30. 0, /* 0x19 '\x19' */
  31. 0, /* 0x1a '\x1a' */
  32. 0, /* 0x1b '\x1b' */
  33. 0, /* 0x1c '\x1c' */
  34. 0, /* 0x1d '\x1d' */
  35. 0, /* 0x1e '\x1e' */
  36. 0, /* 0x1f '\x1f' */
  37. FLAG_SPACE, /* 0x20 ' ' */
  38. 0, /* 0x21 '!' */
  39. 0, /* 0x22 '"' */
  40. 0, /* 0x23 '#' */
  41. 0, /* 0x24 '$' */
  42. 0, /* 0x25 '%' */
  43. 0, /* 0x26 '&' */
  44. 0, /* 0x27 "'" */
  45. 0, /* 0x28 '(' */
  46. 0, /* 0x29 ')' */
  47. 0, /* 0x2a '*' */
  48. 0, /* 0x2b '+' */
  49. 0, /* 0x2c ',' */
  50. 0, /* 0x2d '-' */
  51. 0, /* 0x2e '.' */
  52. 0, /* 0x2f '/' */
  53. FLAG_DIGIT|FLAG_XDIGIT, /* 0x30 '0' */
  54. FLAG_DIGIT|FLAG_XDIGIT, /* 0x31 '1' */
  55. FLAG_DIGIT|FLAG_XDIGIT, /* 0x32 '2' */
  56. FLAG_DIGIT|FLAG_XDIGIT, /* 0x33 '3' */
  57. FLAG_DIGIT|FLAG_XDIGIT, /* 0x34 '4' */
  58. FLAG_DIGIT|FLAG_XDIGIT, /* 0x35 '5' */
  59. FLAG_DIGIT|FLAG_XDIGIT, /* 0x36 '6' */
  60. FLAG_DIGIT|FLAG_XDIGIT, /* 0x37 '7' */
  61. FLAG_DIGIT|FLAG_XDIGIT, /* 0x38 '8' */
  62. FLAG_DIGIT|FLAG_XDIGIT, /* 0x39 '9' */
  63. 0, /* 0x3a ':' */
  64. 0, /* 0x3b ';' */
  65. 0, /* 0x3c '<' */
  66. 0, /* 0x3d '=' */
  67. 0, /* 0x3e '>' */
  68. 0, /* 0x3f '?' */
  69. 0, /* 0x40 '@' */
  70. FLAG_UPPER|FLAG_XDIGIT, /* 0x41 'A' */
  71. FLAG_UPPER|FLAG_XDIGIT, /* 0x42 'B' */
  72. FLAG_UPPER|FLAG_XDIGIT, /* 0x43 'C' */
  73. FLAG_UPPER|FLAG_XDIGIT, /* 0x44 'D' */
  74. FLAG_UPPER|FLAG_XDIGIT, /* 0x45 'E' */
  75. FLAG_UPPER|FLAG_XDIGIT, /* 0x46 'F' */
  76. FLAG_UPPER, /* 0x47 'G' */
  77. FLAG_UPPER, /* 0x48 'H' */
  78. FLAG_UPPER, /* 0x49 'I' */
  79. FLAG_UPPER, /* 0x4a 'J' */
  80. FLAG_UPPER, /* 0x4b 'K' */
  81. FLAG_UPPER, /* 0x4c 'L' */
  82. FLAG_UPPER, /* 0x4d 'M' */
  83. FLAG_UPPER, /* 0x4e 'N' */
  84. FLAG_UPPER, /* 0x4f 'O' */
  85. FLAG_UPPER, /* 0x50 'P' */
  86. FLAG_UPPER, /* 0x51 'Q' */
  87. FLAG_UPPER, /* 0x52 'R' */
  88. FLAG_UPPER, /* 0x53 'S' */
  89. FLAG_UPPER, /* 0x54 'T' */
  90. FLAG_UPPER, /* 0x55 'U' */
  91. FLAG_UPPER, /* 0x56 'V' */
  92. FLAG_UPPER, /* 0x57 'W' */
  93. FLAG_UPPER, /* 0x58 'X' */
  94. FLAG_UPPER, /* 0x59 'Y' */
  95. FLAG_UPPER, /* 0x5a 'Z' */
  96. 0, /* 0x5b '[' */
  97. 0, /* 0x5c '\\' */
  98. 0, /* 0x5d ']' */
  99. 0, /* 0x5e '^' */
  100. 0, /* 0x5f '_' */
  101. 0, /* 0x60 '`' */
  102. FLAG_LOWER|FLAG_XDIGIT, /* 0x61 'a' */
  103. FLAG_LOWER|FLAG_XDIGIT, /* 0x62 'b' */
  104. FLAG_LOWER|FLAG_XDIGIT, /* 0x63 'c' */
  105. FLAG_LOWER|FLAG_XDIGIT, /* 0x64 'd' */
  106. FLAG_LOWER|FLAG_XDIGIT, /* 0x65 'e' */
  107. FLAG_LOWER|FLAG_XDIGIT, /* 0x66 'f' */
  108. FLAG_LOWER, /* 0x67 'g' */
  109. FLAG_LOWER, /* 0x68 'h' */
  110. FLAG_LOWER, /* 0x69 'i' */
  111. FLAG_LOWER, /* 0x6a 'j' */
  112. FLAG_LOWER, /* 0x6b 'k' */
  113. FLAG_LOWER, /* 0x6c 'l' */
  114. FLAG_LOWER, /* 0x6d 'm' */
  115. FLAG_LOWER, /* 0x6e 'n' */
  116. FLAG_LOWER, /* 0x6f 'o' */
  117. FLAG_LOWER, /* 0x70 'p' */
  118. FLAG_LOWER, /* 0x71 'q' */
  119. FLAG_LOWER, /* 0x72 'r' */
  120. FLAG_LOWER, /* 0x73 's' */
  121. FLAG_LOWER, /* 0x74 't' */
  122. FLAG_LOWER, /* 0x75 'u' */
  123. FLAG_LOWER, /* 0x76 'v' */
  124. FLAG_LOWER, /* 0x77 'w' */
  125. FLAG_LOWER, /* 0x78 'x' */
  126. FLAG_LOWER, /* 0x79 'y' */
  127. FLAG_LOWER, /* 0x7a 'z' */
  128. 0, /* 0x7b '{' */
  129. 0, /* 0x7c '|' */
  130. 0, /* 0x7d '}' */
  131. 0, /* 0x7e '~' */
  132. 0, /* 0x7f '\x7f' */
  133. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  134. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  135. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  136. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  137. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  138. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  139. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  140. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  141. };
  142. const unsigned char _Py_ctype_tolower[256] = {
  143. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  144. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  145. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  146. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  147. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  148. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  149. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  150. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  151. 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  152. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  153. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  154. 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  155. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  156. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  157. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  158. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  159. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  160. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  161. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  162. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  163. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  164. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  165. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  166. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  167. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  168. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  169. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  170. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  171. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  172. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  173. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  174. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
  175. };
  176. const unsigned char _Py_ctype_toupper[256] = {
  177. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  178. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  179. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  180. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  181. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  182. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  183. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  184. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  185. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  186. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  187. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  188. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  189. 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  190. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  191. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  192. 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  193. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  194. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  195. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  196. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  197. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  198. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  199. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  200. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  201. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  202. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  203. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  204. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  205. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  206. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  207. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  208. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
  209. };
  210. PyDoc_STRVAR_shared(_Py_isspace__doc__,
  211. "B.isspace() -> bool\n\
  212. \n\
  213. Return True if all characters in B are whitespace\n\
  214. and there is at least one character in B, False otherwise.");
  215. PyObject*
  216. _Py_bytes_isspace(const char *cptr, Py_ssize_t len)
  217. {
  218. register const unsigned char *p
  219. = (unsigned char *) cptr;
  220. register const unsigned char *e;
  221. /* Shortcut for single character strings */
  222. if (len == 1 && ISSPACE(*p))
  223. Py_RETURN_TRUE;
  224. /* Special case for empty strings */
  225. if (len == 0)
  226. Py_RETURN_FALSE;
  227. e = p + len;
  228. for (; p < e; p++) {
  229. if (!ISSPACE(*p))
  230. Py_RETURN_FALSE;
  231. }
  232. Py_RETURN_TRUE;
  233. }
  234. PyDoc_STRVAR_shared(_Py_isalpha__doc__,
  235. "B.isalpha() -> bool\n\
  236. \n\
  237. Return True if all characters in B are alphabetic\n\
  238. and there is at least one character in B, False otherwise.");
  239. PyObject*
  240. _Py_bytes_isalpha(const char *cptr, Py_ssize_t len)
  241. {
  242. register const unsigned char *p
  243. = (unsigned char *) cptr;
  244. register const unsigned char *e;
  245. /* Shortcut for single character strings */
  246. if (len == 1 && ISALPHA(*p))
  247. Py_RETURN_TRUE;
  248. /* Special case for empty strings */
  249. if (len == 0)
  250. Py_RETURN_FALSE;
  251. e = p + len;
  252. for (; p < e; p++) {
  253. if (!ISALPHA(*p))
  254. Py_RETURN_FALSE;
  255. }
  256. Py_RETURN_TRUE;
  257. }
  258. PyDoc_STRVAR_shared(_Py_isalnum__doc__,
  259. "B.isalnum() -> bool\n\
  260. \n\
  261. Return True if all characters in B are alphanumeric\n\
  262. and there is at least one character in B, False otherwise.");
  263. PyObject*
  264. _Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
  265. {
  266. register const unsigned char *p
  267. = (unsigned char *) cptr;
  268. register const unsigned char *e;
  269. /* Shortcut for single character strings */
  270. if (len == 1 && ISALNUM(*p))
  271. Py_RETURN_TRUE;
  272. /* Special case for empty strings */
  273. if (len == 0)
  274. Py_RETURN_FALSE;
  275. e = p + len;
  276. for (; p < e; p++) {
  277. if (!ISALNUM(*p))
  278. Py_RETURN_FALSE;
  279. }
  280. Py_RETURN_TRUE;
  281. }
  282. PyDoc_STRVAR_shared(_Py_isdigit__doc__,
  283. "B.isdigit() -> bool\n\
  284. \n\
  285. Return True if all characters in B are digits\n\
  286. and there is at least one character in B, False otherwise.");
  287. PyObject*
  288. _Py_bytes_isdigit(const char *cptr, Py_ssize_t len)
  289. {
  290. register const unsigned char *p
  291. = (unsigned char *) cptr;
  292. register const unsigned char *e;
  293. /* Shortcut for single character strings */
  294. if (len == 1 && ISDIGIT(*p))
  295. Py_RETURN_TRUE;
  296. /* Special case for empty strings */
  297. if (len == 0)
  298. Py_RETURN_FALSE;
  299. e = p + len;
  300. for (; p < e; p++) {
  301. if (!ISDIGIT(*p))
  302. Py_RETURN_FALSE;
  303. }
  304. Py_RETURN_TRUE;
  305. }
  306. PyDoc_STRVAR_shared(_Py_islower__doc__,
  307. "B.islower() -> bool\n\
  308. \n\
  309. Return True if all cased characters in B are lowercase and there is\n\
  310. at least one cased character in B, False otherwise.");
  311. PyObject*
  312. _Py_bytes_islower(const char *cptr, Py_ssize_t len)
  313. {
  314. register const unsigned char *p
  315. = (unsigned char *) cptr;
  316. register const unsigned char *e;
  317. int cased;
  318. /* Shortcut for single character strings */
  319. if (len == 1)
  320. return PyBool_FromLong(ISLOWER(*p));
  321. /* Special case for empty strings */
  322. if (len == 0)
  323. Py_RETURN_FALSE;
  324. e = p + len;
  325. cased = 0;
  326. for (; p < e; p++) {
  327. if (ISUPPER(*p))
  328. Py_RETURN_FALSE;
  329. else if (!cased && ISLOWER(*p))
  330. cased = 1;
  331. }
  332. return PyBool_FromLong(cased);
  333. }
  334. PyDoc_STRVAR_shared(_Py_isupper__doc__,
  335. "B.isupper() -> bool\n\
  336. \n\
  337. Return True if all cased characters in B are uppercase and there is\n\
  338. at least one cased character in B, False otherwise.");
  339. PyObject*
  340. _Py_bytes_isupper(const char *cptr, Py_ssize_t len)
  341. {
  342. register const unsigned char *p
  343. = (unsigned char *) cptr;
  344. register const unsigned char *e;
  345. int cased;
  346. /* Shortcut for single character strings */
  347. if (len == 1)
  348. return PyBool_FromLong(ISUPPER(*p));
  349. /* Special case for empty strings */
  350. if (len == 0)
  351. Py_RETURN_FALSE;
  352. e = p + len;
  353. cased = 0;
  354. for (; p < e; p++) {
  355. if (ISLOWER(*p))
  356. Py_RETURN_FALSE;
  357. else if (!cased && ISUPPER(*p))
  358. cased = 1;
  359. }
  360. return PyBool_FromLong(cased);
  361. }
  362. PyDoc_STRVAR_shared(_Py_istitle__doc__,
  363. "B.istitle() -> bool\n\
  364. \n\
  365. Return True if B is a titlecased string and there is at least one\n\
  366. character in B, i.e. uppercase characters may only follow uncased\n\
  367. characters and lowercase characters only cased ones. Return False\n\
  368. otherwise.");
  369. PyObject*
  370. _Py_bytes_istitle(const char *cptr, Py_ssize_t len)
  371. {
  372. register const unsigned char *p
  373. = (unsigned char *) cptr;
  374. register const unsigned char *e;
  375. int cased, previous_is_cased;
  376. /* Shortcut for single character strings */
  377. if (len == 1)
  378. return PyBool_FromLong(ISUPPER(*p));
  379. /* Special case for empty strings */
  380. if (len == 0)
  381. Py_RETURN_FALSE;
  382. e = p + len;
  383. cased = 0;
  384. previous_is_cased = 0;
  385. for (; p < e; p++) {
  386. register const unsigned char ch = *p;
  387. if (ISUPPER(ch)) {
  388. if (previous_is_cased)
  389. Py_RETURN_FALSE;
  390. previous_is_cased = 1;
  391. cased = 1;
  392. }
  393. else if (ISLOWER(ch)) {
  394. if (!previous_is_cased)
  395. Py_RETURN_FALSE;
  396. previous_is_cased = 1;
  397. cased = 1;
  398. }
  399. else
  400. previous_is_cased = 0;
  401. }
  402. return PyBool_FromLong(cased);
  403. }
  404. PyDoc_STRVAR_shared(_Py_lower__doc__,
  405. "B.lower() -> copy of B\n\
  406. \n\
  407. Return a copy of B with all ASCII characters converted to lowercase.");
  408. void
  409. _Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
  410. {
  411. Py_ssize_t i;
  412. /*
  413. newobj = PyString_FromStringAndSize(NULL, len);
  414. if (!newobj)
  415. return NULL;
  416. s = PyString_AS_STRING(newobj);
  417. */
  418. Py_MEMCPY(result, cptr, len);
  419. for (i = 0; i < len; i++) {
  420. int c = Py_CHARMASK(result[i]);
  421. if (ISUPPER(c))
  422. result[i] = TOLOWER(c);
  423. }
  424. }
  425. PyDoc_STRVAR_shared(_Py_upper__doc__,
  426. "B.upper() -> copy of B\n\
  427. \n\
  428. Return a copy of B with all ASCII characters converted to uppercase.");
  429. void
  430. _Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
  431. {
  432. Py_ssize_t i;
  433. /*
  434. newobj = PyString_FromStringAndSize(NULL, len);
  435. if (!newobj)
  436. return NULL;
  437. s = PyString_AS_STRING(newobj);
  438. */
  439. Py_MEMCPY(result, cptr, len);
  440. for (i = 0; i < len; i++) {
  441. int c = Py_CHARMASK(result[i]);
  442. if (ISLOWER(c))
  443. result[i] = TOUPPER(c);
  444. }
  445. }
  446. PyDoc_STRVAR_shared(_Py_title__doc__,
  447. "B.title() -> copy of B\n\
  448. \n\
  449. Return a titlecased version of B, i.e. ASCII words start with uppercase\n\
  450. characters, all remaining cased characters have lowercase.");
  451. void
  452. _Py_bytes_title(char *result, char *s, Py_ssize_t len)
  453. {
  454. Py_ssize_t i;
  455. int previous_is_cased = 0;
  456. /*
  457. newobj = PyString_FromStringAndSize(NULL, len);
  458. if (newobj == NULL)
  459. return NULL;
  460. s_new = PyString_AsString(newobj);
  461. */
  462. for (i = 0; i < len; i++) {
  463. int c = Py_CHARMASK(*s++);
  464. if (ISLOWER(c)) {
  465. if (!previous_is_cased)
  466. c = TOUPPER(c);
  467. previous_is_cased = 1;
  468. } else if (ISUPPER(c)) {
  469. if (previous_is_cased)
  470. c = TOLOWER(c);
  471. previous_is_cased = 1;
  472. } else
  473. previous_is_cased = 0;
  474. *result++ = c;
  475. }
  476. }
  477. PyDoc_STRVAR_shared(_Py_capitalize__doc__,
  478. "B.capitalize() -> copy of B\n\
  479. \n\
  480. Return a copy of B with only its first character capitalized (ASCII).");
  481. void
  482. _Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
  483. {
  484. Py_ssize_t i;
  485. /*
  486. newobj = PyString_FromStringAndSize(NULL, len);
  487. if (newobj == NULL)
  488. return NULL;
  489. s_new = PyString_AsString(newobj);
  490. */
  491. if (0 < len) {
  492. int c = Py_CHARMASK(*s++);
  493. if (ISLOWER(c))
  494. *result = TOUPPER(c);
  495. else
  496. *result = c;
  497. result++;
  498. }
  499. for (i = 1; i < len; i++) {
  500. int c = Py_CHARMASK(*s++);
  501. if (ISUPPER(c))
  502. *result = TOLOWER(c);
  503. else
  504. *result = c;
  505. result++;
  506. }
  507. }
  508. PyDoc_STRVAR_shared(_Py_swapcase__doc__,
  509. "B.swapcase() -> copy of B\n\
  510. \n\
  511. Return a copy of B with uppercase ASCII characters converted\n\
  512. to lowercase ASCII and vice versa.");
  513. void
  514. _Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
  515. {
  516. Py_ssize_t i;
  517. /*
  518. newobj = PyString_FromStringAndSize(NULL, len);
  519. if (newobj == NULL)
  520. return NULL;
  521. s_new = PyString_AsString(newobj);
  522. */
  523. for (i = 0; i < len; i++) {
  524. int c = Py_CHARMASK(*s++);
  525. if (ISLOWER(c)) {
  526. *result = TOUPPER(c);
  527. }
  528. else if (ISUPPER(c)) {
  529. *result = TOLOWER(c);
  530. }
  531. else
  532. *result = c;
  533. result++;
  534. }
  535. }