annotate Modules/stropmodule.c @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents fd25030223b0
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* strop module */
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
36557
787febae30db Make ssize_t-clean.
Martin v. Löwis <martin@v.loewis.de>
parents: 36518
diff changeset
3 #define PY_SSIZE_T_CLEAN
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
4 #include "Python.h"
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
5 #include <ctype.h>
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
6
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
7 PyDoc_STRVAR(strop_module__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
8 "Common string manipulations, optimized for speed.\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
9 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
10 "Always use \"import string\" rather than referencing\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
11 "this module directly.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
12
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
13 /* XXX This file assumes that the <ctype.h> is*() functions
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
14 XXX are defined for all 8-bit characters! */
1321
a80677798f84 stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
15
17834
ed1e8b6e26ec Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents: 17753
diff changeset
16 #define WARN if (PyErr_Warn(PyExc_DeprecationWarning, \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
17 "strop functions are obsolete; use string methods")) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
18 return NULL
17834
ed1e8b6e26ec Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents: 17753
diff changeset
19
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
20 /* The lstrip(), rstrip() and strip() functions are implemented
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
21 in do_strip(), which uses an additional parameter to indicate what
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
22 type of strip should occur. */
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
23
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
24 #define LEFTSTRIP 0
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
25 #define RIGHTSTRIP 1
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
26 #define BOTHSTRIP 2
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
27
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
29 static PyObject *
36557
787febae30db Make ssize_t-clean.
Martin v. Löwis <martin@v.loewis.de>
parents: 36518
diff changeset
30 split_whitespace(char *s, Py_ssize_t len, Py_ssize_t maxsplit)
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
32 Py_ssize_t i = 0, j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
33 int err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
34 Py_ssize_t countsplit = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
35 PyObject* item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
36 PyObject *list = PyList_New(0);
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
38 if (list == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
39 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
41 while (i < len) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
42 while (i < len && isspace(Py_CHARMASK(s[i]))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
43 i = i+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
44 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
45 j = i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
46 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
47 i = i+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
48 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
49 if (j < i) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
50 item = PyString_FromStringAndSize(s+j, i-j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
51 if (item == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
52 goto finally;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
53
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
54 err = PyList_Append(list, item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
55 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
56 if (err < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
57 goto finally;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
58
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
59 countsplit++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
60 while (i < len && isspace(Py_CHARMASK(s[i]))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
61 i = i+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
62 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
63 if (maxsplit && (countsplit >= maxsplit) && i < len) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
64 item = PyString_FromStringAndSize(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
65 s+i, len - i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
66 if (item == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
67 goto finally;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
68
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
69 err = PyList_Append(list, item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
70 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
71 if (err < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
72 goto finally;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
73
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
74 i = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
75 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
76 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
77 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
78 return list;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
79 finally:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
80 Py_DECREF(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
81 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
85 PyDoc_STRVAR(splitfields__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
86 "split(s [,sep [,maxsplit]]) -> list of strings\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
87 "splitfields(s [,sep [,maxsplit]]) -> list of strings\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
88 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
89 "Return a list of the words in the string s, using sep as the\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
90 "delimiter string. If maxsplit is nonzero, splits into at most\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
91 "maxsplit words. If sep is not specified, any whitespace string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
92 "is a separator. Maxsplit defaults to 0.\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
93 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
94 "(split and splitfields are synonymous)");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
95
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
96 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
97 strop_splitfields(PyObject *self, PyObject *args)
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
99 Py_ssize_t len, n, i, j, err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
100 Py_ssize_t splitcount, maxsplit;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
101 char *s, *sub;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
102 PyObject *list, *item;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
104 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
105 sub = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
106 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
107 splitcount = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
108 maxsplit = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
109 if (!PyArg_ParseTuple(args, "t#|z#n:split", &s, &len, &sub, &n, &maxsplit))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
110 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
111 if (sub == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
112 return split_whitespace(s, len, maxsplit);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
113 if (n == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
114 PyErr_SetString(PyExc_ValueError, "empty separator");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
115 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
116 }
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
118 list = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
119 if (list == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
120 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
122 i = j = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
123 while (i+n <= len) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
124 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
125 item = PyString_FromStringAndSize(s+j, i-j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
126 if (item == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
127 goto fail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
128 err = PyList_Append(list, item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
129 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
130 if (err < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
131 goto fail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
132 i = j = i + n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
133 splitcount++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
134 if (maxsplit && (splitcount >= maxsplit))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
135 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
136 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
137 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
138 i++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
139 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
140 item = PyString_FromStringAndSize(s+j, len-j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
141 if (item == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
142 goto fail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
143 err = PyList_Append(list, item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
144 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
145 if (err < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
146 goto fail;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
148 return list;
1394
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
149
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
150 fail:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
151 Py_DECREF(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
152 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
156 PyDoc_STRVAR(joinfields__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
157 "join(list [,sep]) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
158 "joinfields(list [,sep]) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
159 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
160 "Return a string composed of the words in list, with\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
161 "intervening occurrences of sep. Sep defaults to a single\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
162 "space.\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
163 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
164 "(join and joinfields are synonymous)");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
165
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
166 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
167 strop_joinfields(PyObject *self, PyObject *args)
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
168 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
169 PyObject *seq;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
170 char *sep = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
171 Py_ssize_t seqlen, seplen = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
172 Py_ssize_t i, reslen = 0, slen = 0, sz = 100;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
173 PyObject *res = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
174 char* p = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
175 ssizeargfunc getitemfunc;
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
176
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
177 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
178 if (!PyArg_ParseTuple(args, "O|t#:join", &seq, &sep, &seplen))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
179 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
180 if (sep == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
181 sep = " ";
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
182 seplen = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
183 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
184
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
185 seqlen = PySequence_Size(seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
186 if (seqlen < 0 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
187 return NULL;
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
188
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
189 if (seqlen == 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
190 /* Optimization if there's only one item */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
191 PyObject *item = PySequence_GetItem(seq, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
192 if (item && !PyString_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
193 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
194 "first argument must be sequence of strings");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
195 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
196 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
197 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
198 return item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
199 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
200
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
201 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
202 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
203 p = PyString_AsString(res);
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
204
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
205 /* optimize for lists, since it's the most common case. all others
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
206 * (tuples and arbitrary sequences) just use the sequence abstract
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
207 * interface.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
208 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
209 if (PyList_Check(seq)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
210 for (i = 0; i < seqlen; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
211 PyObject *item = PyList_GET_ITEM(seq, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
212 if (!PyString_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
213 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
214 "first argument must be sequence of strings");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
215 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
216 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
217 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
218 slen = PyString_GET_SIZE(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
219 if (slen > PY_SSIZE_T_MAX - reslen ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
220 seplen > PY_SSIZE_T_MAX - reslen - seplen) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
221 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
222 "input too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
223 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
224 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
225 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
226 while (reslen + slen + seplen >= sz) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
227 if (_PyString_Resize(&res, sz * 2) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
228 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
229 sz *= 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
230 p = PyString_AsString(res) + reslen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
231 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
232 if (i > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
233 memcpy(p, sep, seplen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
234 p += seplen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
235 reslen += seplen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
236 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
237 memcpy(p, PyString_AS_STRING(item), slen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
238 p += slen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
239 reslen += slen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
240 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
241 _PyString_Resize(&res, reslen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
242 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
243 }
8405
3c40af2d1de9 Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents: 8333
diff changeset
244
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
245 if (seq->ob_type->tp_as_sequence == NULL ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
246 (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
247 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
248 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
249 "first argument must be a sequence");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
250 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
251 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
252 /* This is now type safe */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
253 for (i = 0; i < seqlen; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
254 PyObject *item = getitemfunc(seq, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
255 if (!item || !PyString_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
256 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
257 "first argument must be sequence of strings");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
258 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
259 Py_XDECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
260 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
261 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
262 slen = PyString_GET_SIZE(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
263 if (slen > PY_SSIZE_T_MAX - reslen ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
264 seplen > PY_SSIZE_T_MAX - reslen - seplen) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
265 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
266 "input too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
267 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
268 Py_XDECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
269 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
270 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
271 while (reslen + slen + seplen >= sz) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
272 if (_PyString_Resize(&res, sz * 2) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
273 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
274 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
275 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
276 sz *= 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
277 p = PyString_AsString(res) + reslen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
278 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
279 if (i > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
280 memcpy(p, sep, seplen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
281 p += seplen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
282 reslen += seplen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
283 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
284 memcpy(p, PyString_AS_STRING(item), slen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
285 p += slen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
286 reslen += slen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
287 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
288 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
289 _PyString_Resize(&res, reslen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
290 return res;
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
291 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
292
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
293
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
294 PyDoc_STRVAR(find__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
295 "find(s, sub [,start [,end]]) -> in\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
296 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
297 "Return the lowest index in s where substring sub is found,\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
298 "such that sub is contained within s[start,end]. Optional\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
299 "arguments start and end are interpreted as in slice notation.\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
300 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
301 "Return -1 on failure.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
302
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
303 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
304 strop_find(PyObject *self, PyObject *args)
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
306 char *s, *sub;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
307 Py_ssize_t len, n, i = 0, last = PY_SSIZE_T_MAX;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
308
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
309 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
310 if (!PyArg_ParseTuple(args, "t#t#|nn:find", &s, &len, &sub, &n, &i, &last))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
311 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
313 if (last > len)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
314 last = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
315 if (last < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
316 last += len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
317 if (last < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
318 last = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
319 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
320 i += len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
321 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
322 i = 0;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
324 if (n == 0 && i <= last)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
325 return PyInt_FromLong((long)i);
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
327 last -= n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
328 for (; i <= last; ++i)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
329 if (s[i] == sub[0] &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
330 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
331 return PyInt_FromLong((long)i);
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
332
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
333 return PyInt_FromLong(-1L);
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
334 }
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
335
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
336
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
337 PyDoc_STRVAR(rfind__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
338 "rfind(s, sub [,start [,end]]) -> int\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
339 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
340 "Return the highest index in s where substring sub is found,\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
341 "such that sub is contained within s[start,end]. Optional\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
342 "arguments start and end are interpreted as in slice notation.\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
343 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
344 "Return -1 on failure.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
345
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
346 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
347 strop_rfind(PyObject *self, PyObject *args)
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
348 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
349 char *s, *sub;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
350 Py_ssize_t len, n, j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
351 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX;
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
352
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
353 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
354 if (!PyArg_ParseTuple(args, "t#t#|nn:rfind", &s, &len, &sub, &n, &i, &last))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
355 return NULL;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
356
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
357 if (last > len)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
358 last = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
359 if (last < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
360 last += len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
361 if (last < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
362 last = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
363 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
364 i += len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
365 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
366 i = 0;
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
367
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
368 if (n == 0 && i <= last)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
369 return PyInt_FromLong((long)last);
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
370
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
371 for (j = last-n; j >= i; --j)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
372 if (s[j] == sub[0] &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
373 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
374 return PyInt_FromLong((long)j);
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
376 return PyInt_FromLong(-1L);
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
379
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
380 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
381 do_strip(PyObject *args, int striptype)
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
382 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
383 char *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
384 Py_ssize_t len, i, j;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
385
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
386
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
387 if (PyString_AsStringAndSize(args, &s, &len))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
388 return NULL;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
389
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
390 i = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
391 if (striptype != RIGHTSTRIP) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
392 while (i < len && isspace(Py_CHARMASK(s[i]))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
393 i++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
394 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
395 }
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
396
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
397 j = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
398 if (striptype != LEFTSTRIP) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
399 do {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
400 j--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
401 } while (j >= i && isspace(Py_CHARMASK(s[j])));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
402 j++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
403 }
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
404
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
405 if (i == 0 && j == len) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
406 Py_INCREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
407 return args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
408 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
409 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
410 return PyString_FromStringAndSize(s+i, j-i);
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
411 }
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
412
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
414 PyDoc_STRVAR(strip__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
415 "strip(s) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
416 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
417 "Return a copy of the string s with leading and trailing\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
418 "whitespace removed.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
419
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
420 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
421 strop_strip(PyObject *self, PyObject *args)
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
423 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
424 return do_strip(args, BOTHSTRIP);
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
425 }
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
427
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
428 PyDoc_STRVAR(lstrip__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
429 "lstrip(s) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
430 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
431 "Return a copy of the string s with leading whitespace removed.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
432
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
433 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
434 strop_lstrip(PyObject *self, PyObject *args)
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
435 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
436 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
437 return do_strip(args, LEFTSTRIP);
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
438 }
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
440
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
441 PyDoc_STRVAR(rstrip__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
442 "rstrip(s) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
443 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
444 "Return a copy of the string s with trailing whitespace removed.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
445
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
446 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
447 strop_rstrip(PyObject *self, PyObject *args)
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
448 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
449 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
450 return do_strip(args, RIGHTSTRIP);
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
454 PyDoc_STRVAR(lower__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
455 "lower(s) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
456 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
457 "Return a copy of the string s converted to lowercase.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
458
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
459 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
460 strop_lower(PyObject *self, PyObject *args)
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
461 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
462 char *s, *s_new;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
463 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
464 PyObject *newstr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
465 int changed;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
466
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
467 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
468 if (PyString_AsStringAndSize(args, &s, &n))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
469 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
470 newstr = PyString_FromStringAndSize(NULL, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
471 if (newstr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
472 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
473 s_new = PyString_AsString(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
474 changed = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
475 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
476 int c = Py_CHARMASK(*s++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
477 if (isupper(c)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
478 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
479 *s_new = tolower(c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
480 } else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
481 *s_new = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
482 s_new++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
483 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
484 if (!changed) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
485 Py_DECREF(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
486 Py_INCREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
487 return args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
488 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
489 return newstr;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
490 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
491
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
492
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
493 PyDoc_STRVAR(upper__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
494 "upper(s) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
495 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
496 "Return a copy of the string s converted to uppercase.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
497
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
498 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
499 strop_upper(PyObject *self, PyObject *args)
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
500 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
501 char *s, *s_new;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
502 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
503 PyObject *newstr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
504 int changed;
4894
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
505
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
506 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
507 if (PyString_AsStringAndSize(args, &s, &n))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
508 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
509 newstr = PyString_FromStringAndSize(NULL, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
510 if (newstr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
511 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
512 s_new = PyString_AsString(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
513 changed = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
514 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
515 int c = Py_CHARMASK(*s++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
516 if (islower(c)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
517 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
518 *s_new = toupper(c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
519 } else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
520 *s_new = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
521 s_new++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
522 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
523 if (!changed) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
524 Py_DECREF(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
525 Py_INCREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
526 return args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
527 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
528 return newstr;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
529 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
530
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
531
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
532 PyDoc_STRVAR(capitalize__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
533 "capitalize(s) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
534 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
535 "Return a copy of the string s with only its first character\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
536 "capitalized.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
537
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
538 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
539 strop_capitalize(PyObject *self, PyObject *args)
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
540 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
541 char *s, *s_new;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
542 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
543 PyObject *newstr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
544 int changed;
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
545
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
546 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
547 if (PyString_AsStringAndSize(args, &s, &n))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
548 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
549 newstr = PyString_FromStringAndSize(NULL, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
550 if (newstr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
551 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
552 s_new = PyString_AsString(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
553 changed = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
554 if (0 < n) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
555 int c = Py_CHARMASK(*s++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
556 if (islower(c)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
557 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
558 *s_new = toupper(c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
559 } else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
560 *s_new = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
561 s_new++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
562 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
563 for (i = 1; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
564 int c = Py_CHARMASK(*s++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
565 if (isupper(c)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
566 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
567 *s_new = tolower(c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
568 } else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
569 *s_new = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
570 s_new++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
571 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
572 if (!changed) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
573 Py_DECREF(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
574 Py_INCREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
575 return args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
576 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
577 return newstr;
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
578 }
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
579
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
580
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
581 PyDoc_STRVAR(expandtabs__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
582 "expandtabs(string, [tabsize]) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
583 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
584 "Expand tabs in a string, i.e. replace them by one or more spaces,\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
585 "depending on the current column and the given tab size (default 8).\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
586 "The column number is reset to zero after each newline occurring in the\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
587 "string. This doesn't understand other non-printing characters.");
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
588
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
589 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
590 strop_expandtabs(PyObject *self, PyObject *args)
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
591 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
592 /* Original by Fredrik Lundh */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
593 char* e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
594 char* p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
595 char* q;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
596 Py_ssize_t i, j, old_j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
597 PyObject* out;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
598 char* string;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
599 Py_ssize_t stringlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
600 int tabsize = 8;
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
601
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
602 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
603 /* Get arguments */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
604 if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
605 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
606 if (tabsize < 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
607 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
608 "tabsize must be at least 1");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
609 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
610 }
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
611
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
612 /* First pass: determine size of output string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
613 i = j = old_j = 0; /* j: current column; i: total of previous lines */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
614 e = string + stringlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
615 for (p = string; p < e; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
616 if (*p == '\t') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
617 j += tabsize - (j%tabsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
618 if (old_j > j) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
619 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
620 "new string is too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
621 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
622 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
623 old_j = j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
624 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
625 j++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
626 if (*p == '\n') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
627 i += j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
628 j = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
629 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
630 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
631 }
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
632
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
633 if ((i + j) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
634 PyErr_SetString(PyExc_OverflowError, "new string is too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
635 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
636 }
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
637
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
638 /* Second pass: create output string and fill it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
639 out = PyString_FromStringAndSize(NULL, i+j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
640 if (out == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
641 return NULL;
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
642
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
643 i = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
644 q = PyString_AS_STRING(out);
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
645
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
646 for (p = string; p < e; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
647 if (*p == '\t') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
648 j = tabsize - (i%tabsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
649 i += j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
650 while (j-- > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
651 *q++ = ' ';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
652 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
653 *q++ = *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
654 i++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
655 if (*p == '\n')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
656 i = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
657 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
658 }
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
659
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
660 return out;
9953
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
661 }
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
662
ca2745271111 A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
663
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
664 PyDoc_STRVAR(count__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
665 "count(s, sub[, start[, end]]) -> int\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
666 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
667 "Return the number of occurrences of substring sub in string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
668 "s[start:end]. Optional arguments start and end are\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
669 "interpreted as in slice notation.");
9258
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
670
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
671 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
672 strop_count(PyObject *self, PyObject *args)
9258
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
673 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
674 char *s, *sub;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
675 Py_ssize_t len, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
676 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
677 Py_ssize_t m, r;
9258
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
678
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
679 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
680 if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
681 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
682 if (last > len)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
683 last = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
684 if (last < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
685 last += len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
686 if (last < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
687 last = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
688 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
689 i += len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
690 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
691 i = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
692 m = last + 1 - n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
693 if (n == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
694 return PyInt_FromLong((long) (m-i));
9258
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
695
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
696 r = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
697 while (i < m) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
698 if (!memcmp(s+i, sub, n)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
699 r++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
700 i += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
701 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
702 i++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
703 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
704 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
705 return PyInt_FromLong((long) r);
9258
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
706 }
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
707
2577c0cc7482 Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents: 8809
diff changeset
708
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
709 PyDoc_STRVAR(swapcase__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
710 "swapcase(s) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
711 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
712 "Return a copy of the string s with upper case characters\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
713 "converted to lowercase and vice versa.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
714
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
715 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
716 strop_swapcase(PyObject *self, PyObject *args)
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
717 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
718 char *s, *s_new;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
719 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
720 PyObject *newstr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
721 int changed;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
722
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
723 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
724 if (PyString_AsStringAndSize(args, &s, &n))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
725 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
726 newstr = PyString_FromStringAndSize(NULL, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
727 if (newstr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
728 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
729 s_new = PyString_AsString(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
730 changed = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
731 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
732 int c = Py_CHARMASK(*s++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
733 if (islower(c)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
734 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
735 *s_new = toupper(c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
736 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
737 else if (isupper(c)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
738 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
739 *s_new = tolower(c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
740 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
741 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
742 *s_new = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
743 s_new++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
744 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
745 if (!changed) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
746 Py_DECREF(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
747 Py_INCREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
748 return args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
749 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
750 return newstr;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
751 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
752
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
753
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
754 PyDoc_STRVAR(atoi__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
755 "atoi(s [,base]) -> int\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
756 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
757 "Return the integer represented by the string s in the given\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
758 "base, which defaults to 10. The string s must consist of one\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
759 "or more digits, possibly preceded by a sign. If base is 0, it\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
760 "is chosen from the leading characters of s, 0 for octal, 0x or\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
761 "0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
762 "accepted.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
763
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
764 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
765 strop_atoi(PyObject *self, PyObject *args)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
766 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
767 char *s, *end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
768 int base = 10;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
769 long x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
770 char buffer[256]; /* For errors */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
771
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
772 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
773 if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
774 return NULL;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
775
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
776 if ((base != 0 && base < 2) || base > 36) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
777 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
778 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
779 }
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
780
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
781 while (*s && isspace(Py_CHARMASK(*s)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
782 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
783 errno = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
784 if (base == 0 && s[0] == '0')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
785 x = (long) PyOS_strtoul(s, &end, base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
786 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
787 x = PyOS_strtol(s, &end, base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
788 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
789 goto bad;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
790 while (*end && isspace(Py_CHARMASK(*end)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
791 end++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
792 if (*end != '\0') {
8809
c5d72bcda932 Better error messages when raising ValueError for int literals. (The
Guido van Rossum <guido@python.org>
parents: 8757
diff changeset
793 bad:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
794 PyOS_snprintf(buffer, sizeof(buffer),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
795 "invalid literal for atoi(): %.200s", s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
796 PyErr_SetString(PyExc_ValueError, buffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
797 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
798 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
799 else if (errno != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
800 PyOS_snprintf(buffer, sizeof(buffer),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
801 "atoi() literal too large: %.200s", s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
802 PyErr_SetString(PyExc_ValueError, buffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
803 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
804 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
805 return PyInt_FromLong(x);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
806 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
807
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
808
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
809 PyDoc_STRVAR(atol__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
810 "atol(s [,base]) -> long\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
811 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
812 "Return the long integer represented by the string s in the\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
813 "given base, which defaults to 10. The string s must consist\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
814 "of one or more digits, possibly preceded by a sign. If base\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
815 "is 0, it is chosen from the leading characters of s, 0 for\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
816 "octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
817 "0x or 0X is accepted. A trailing L or l is not accepted,\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
818 "unless base is 0.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
819
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
820 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
821 strop_atol(PyObject *self, PyObject *args)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
822 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
823 char *s, *end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
824 int base = 10;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
825 PyObject *x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
826 char buffer[256]; /* For errors */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
827
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
828 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
829 if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
830 return NULL;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
831
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
832 if ((base != 0 && base < 2) || base > 36) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
833 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
834 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
835 }
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
836
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
837 while (*s && isspace(Py_CHARMASK(*s)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
838 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
839 if (s[0] == '\0') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
840 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
841 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
842 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
843 x = PyLong_FromString(s, &end, base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
844 if (x == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
845 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
846 if (base == 0 && (*end == 'l' || *end == 'L'))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
847 end++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
848 while (*end && isspace(Py_CHARMASK(*end)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
849 end++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
850 if (*end != '\0') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
851 PyOS_snprintf(buffer, sizeof(buffer),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
852 "invalid literal for atol(): %.200s", s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
853 PyErr_SetString(PyExc_ValueError, buffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
854 Py_DECREF(x);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
855 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
856 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
857 return x;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
858 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
859
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
860
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
861 PyDoc_STRVAR(atof__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
862 "atof(s) -> float\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
863 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
864 "Return the floating point number represented by the string s.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
865
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
866 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
867 strop_atof(PyObject *self, PyObject *args)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
868 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
869 char *s, *end;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
870 double x;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
871 char buffer[256]; /* For errors */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
872
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
873 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
874 if (!PyArg_ParseTuple(args, "s:atof", &s))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
875 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
876 while (*s && isspace(Py_CHARMASK(*s)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
877 s++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
878 if (s[0] == '\0') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
879 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
880 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
881 }
56777
91a0c3c60209 Issue 7117: Replace PyOS_ascii_strtod with PyOS_string_to_double in stropmodule as part of short float repr.
Eric Smith <eric@trueblade.com>
parents: 48114
diff changeset
882
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
883 PyFPE_START_PROTECT("strop_atof", return 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
884 x = PyOS_string_to_double(s, &end, PyExc_OverflowError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
885 PyFPE_END_PROTECT(x)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
886 if (x == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
887 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
888 while (*end && isspace(Py_CHARMASK(*end)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
889 end++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
890 if (*end != '\0') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
891 PyOS_snprintf(buffer, sizeof(buffer),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
892 "invalid literal for atof(): %.200s", s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
893 PyErr_SetString(PyExc_ValueError, buffer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
894 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
895 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
896 return PyFloat_FromDouble(x);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
897 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
898
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
899
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
900 PyDoc_STRVAR(maketrans__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
901 "maketrans(frm, to) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
902 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
903 "Return a translation table (a string of 256 bytes long)\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
904 "suitable for use in string.translate. The strings frm and to\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
905 "must be of the same length.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
906
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
907 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
908 strop_maketrans(PyObject *self, PyObject *args)
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
909 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
910 unsigned char *c, *from=NULL, *to=NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
911 Py_ssize_t i, fromlen=0, tolen=0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
912 PyObject *result;
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
913
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
914 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
915 return NULL;
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
916
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
917 if (fromlen != tolen) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
918 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
919 "maketrans arguments must have same length");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
920 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
921 }
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
922
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
923 result = PyString_FromStringAndSize((char *)NULL, 256);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
924 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
925 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
926 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
927 for (i = 0; i < 256; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
928 c[i]=(unsigned char)i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
929 for (i = 0; i < fromlen; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
930 c[from[i]]=to[i];
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
931
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
932 return result;
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
933 }
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
934
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
935
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
936 PyDoc_STRVAR(translate__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
937 "translate(s,table [,deletechars]) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
938 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
939 "Return a copy of the string s, where all characters occurring\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
940 "in the optional argument deletechars are removed, and the\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
941 "remaining characters have been mapped through the given\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
942 "translation table, which must be a string of length 256.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
943
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
944 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
945 strop_translate(PyObject *self, PyObject *args)
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
946 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
947 register char *input, *table, *output;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
948 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
949 int c, changed = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
950 PyObject *input_obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
951 char *table1, *output_start, *del_table=NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
952 Py_ssize_t inlen, tablen, dellen = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
953 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
954 int trans_table[256];
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
955
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
956 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
957 if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
958 &table1, &tablen, &del_table, &dellen))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
959 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
960 if (tablen != 256) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
961 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
962 "translation table must be 256 characters long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
963 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
964 }
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
965
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
966 table = table1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
967 inlen = PyString_GET_SIZE(input_obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
968 result = PyString_FromStringAndSize((char *)NULL, inlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
969 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
970 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
971 output_start = output = PyString_AsString(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
972 input = PyString_AsString(input_obj);
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
973
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
974 if (dellen == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
975 /* If no deletions are required, use faster code */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
976 for (i = inlen; --i >= 0; ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
977 c = Py_CHARMASK(*input++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
978 if (Py_CHARMASK((*output++ = table[c])) != c)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
979 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
980 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
981 if (changed)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
982 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
983 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
984 Py_INCREF(input_obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
985 return input_obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
986 }
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
987
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
988 for (i = 0; i < 256; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
989 trans_table[i] = Py_CHARMASK(table[i]);
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
990
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
991 for (i = 0; i < dellen; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
992 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
993
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
994 for (i = inlen; --i >= 0; ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
995 c = Py_CHARMASK(*input++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
996 if (trans_table[c] != -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
997 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
998 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
999 changed = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1000 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1001 if (!changed) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1002 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1003 Py_INCREF(input_obj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1004 return input_obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1005 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1006 /* Fix the size of the resulting string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1007 if (inlen > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1008 _PyString_Resize(&result, output - output_start);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1009 return result;
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
1010 }
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
1011
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
1012
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1013 /* What follows is used for implementing replace(). Perry Stoll. */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1014
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1015 /*
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1016 mymemfind
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1017
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1018 strstr replacement for arbitrary blocks of memory.
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1019
12025
7994d9db7f4d On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents: 11882
diff changeset
1020 Locates the first occurrence in the memory pointed to by MEM of the
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1021 contents of memory pointed to by PAT. Returns the index into MEM if
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1022 found, or -1 if not found. If len of PAT is greater than length of
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
1023 MEM, the function returns -1.
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1024 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1025 static Py_ssize_t
36557
787febae30db Make ssize_t-clean.
Martin v. Löwis <martin@v.loewis.de>
parents: 36518
diff changeset
1026 mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1027 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1028 register Py_ssize_t ii;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1029
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1030 /* pattern can not occur in the last pat_len-1 chars */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1031 len -= pat_len;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1032
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1033 for (ii = 0; ii <= len; ii++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1034 if (mem[ii] == pat[0] &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1035 (pat_len == 1 ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1036 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1037 return ii;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1038 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1039 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1040 return -1;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1041 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1042
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1043 /*
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1044 mymemcnt
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1045
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1046 Return the number of distinct times PAT is found in MEM.
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1047 meaning mem=1111 and pat==11 returns 2.
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1048 mem=11111 and pat==11 also return 2.
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1049 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1050 static Py_ssize_t
36557
787febae30db Make ssize_t-clean.
Martin v. Löwis <martin@v.loewis.de>
parents: 36518
diff changeset
1051 mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1052 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1053 register Py_ssize_t offset = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1054 Py_ssize_t nfound = 0;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1055
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1056 while (len >= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1057 offset = mymemfind(mem, len, pat, pat_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1058 if (offset == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1059 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1060 mem += offset + pat_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1061 len -= offset + pat_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1062 nfound++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1063 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1064 return nfound;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1065 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1066
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
1067 /*
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1068 mymemreplace
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1069
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1070 Return a string in which all occurrences of PAT in memory STR are
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
1071 replaced with SUB.
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1072
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1073 If length of PAT is less than length of STR or there are no occurrences
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1074 of PAT in STR, then the original string is returned. Otherwise, a new
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1075 string is allocated here and returned.
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
1076
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1077 on return, out_len is:
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1078 the length of output string, or
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1079 -1 if the input string is returned, or
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1080 unchanged if an error occurs (no memory).
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1081
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1082 return value is:
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1083 the new string allocated locally, or
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1084 NULL if an error occurred.
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1085 */
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
1086 static char *
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1087 mymemreplace(const char *str, Py_ssize_t len, /* input string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1088 const char *pat, Py_ssize_t pat_len, /* pattern string to find */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1089 const char *sub, Py_ssize_t sub_len, /* substitution string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1090 Py_ssize_t count, /* number of replacements */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1091 Py_ssize_t *out_len)
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1092 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1093 char *out_s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1094 char *new_s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1095 Py_ssize_t nfound, offset, new_len;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1096
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1097 if (len == 0 || pat_len > len)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1098 goto return_same;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1099
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1100 /* find length of output string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1101 nfound = mymemcnt(str, len, pat, pat_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1102 if (count < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1103 count = PY_SSIZE_T_MAX;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1104 else if (nfound > count)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1105 nfound = count;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1106 if (nfound == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1107 goto return_same;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1108
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1109 new_len = len + nfound*(sub_len - pat_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1110 if (new_len == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1111 /* Have to allocate something for the caller to free(). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1112 out_s = (char *)PyMem_MALLOC(1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1113 if (out_s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1114 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1115 out_s[0] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1116 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1117 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1118 assert(new_len > 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1119 new_s = (char *)PyMem_MALLOC(new_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1120 if (new_s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1121 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1122 out_s = new_s;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1123
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1124 for (; count > 0 && len > 0; --count) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1125 /* find index of next instance of pattern */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1126 offset = mymemfind(str, len, pat, pat_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1127 if (offset == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1128 break;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1129
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1130 /* copy non matching part of input string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1131 memcpy(new_s, str, offset);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1132 str += offset + pat_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1133 len -= offset + pat_len;
17749
f465cae301ee SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents: 17748
diff changeset
1134
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1135 /* copy substitute into the output string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1136 new_s += offset;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1137 memcpy(new_s, sub, sub_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1138 new_s += sub_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1139 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1140 /* copy any remaining values into output string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1141 if (len > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1142 memcpy(new_s, str, len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1143 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1144 *out_len = new_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1145 return out_s;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1146
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1147 return_same:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1148 *out_len = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1149 return (char *)str; /* cast away const */
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1150 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1151
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1152
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1153 PyDoc_STRVAR(replace__doc__,
17748
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
1154 "replace (str, old, new[, maxsplit]) -> string\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
1155 "\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
1156 "Return a copy of string str with all occurrences of substring\n"
261196bd16d7 Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
1157 "old replaced by new. If the optional argument maxsplit is\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23295
diff changeset
1158 "given, only the first maxsplit occurrences are replaced.");
6966
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
1159
eed63049df95 Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6696
diff changeset
1160 static PyObject *
13494
4aa12e55bf95 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1161 strop_replace(PyObject *self, PyObject *args)
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1162 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1163 char *str, *pat,*sub,*new_s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1164 Py_ssize_t len,pat_len,sub_len,out_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1165 Py_ssize_t count = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1166 PyObject *newstr;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1167
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1168 WARN;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1169 if (!PyArg_ParseTuple(args, "t#t#t#|n:replace",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1170 &str, &len, &pat, &pat_len, &sub, &sub_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1171 &count))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1172 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1173 if (pat_len <= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1174 PyErr_SetString(PyExc_ValueError, "empty pattern string");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1175 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1176 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1177 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1178 * current (2.1) string.py and string methods. Preserve this for
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1179 * ... well, hard to say for what <wink>.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1180 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1181 if (count == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1182 count = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1183 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1184 if (new_s == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1185 PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1186 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1187 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1188 if (out_len == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1189 /* we're returning another reference to the input string */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1190 newstr = PyTuple_GetItem(args, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1191 Py_XINCREF(newstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1192 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1193 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1194 newstr = PyString_FromStringAndSize(new_s, out_len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1195 PyMem_FREE(new_s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1196 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1197 return newstr;
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1198 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1199
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
1200
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 /* List of functions defined in the module */
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
1203 static PyMethodDef
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
1204 strop_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1205 {"atof", strop_atof, METH_VARARGS, atof__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1206 {"atoi", strop_atoi, METH_VARARGS, atoi__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1207 {"atol", strop_atol, METH_VARARGS, atol__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1208 {"capitalize", strop_capitalize, METH_O, capitalize__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1209 {"count", strop_count, METH_VARARGS, count__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1210 {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1211 {"find", strop_find, METH_VARARGS, find__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1212 {"join", strop_joinfields, METH_VARARGS, joinfields__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1213 {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1214 {"lstrip", strop_lstrip, METH_O, lstrip__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1215 {"lower", strop_lower, METH_O, lower__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1216 {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1217 {"replace", strop_replace, METH_VARARGS, replace__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1218 {"rfind", strop_rfind, METH_VARARGS, rfind__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1219 {"rstrip", strop_rstrip, METH_O, rstrip__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1220 {"split", strop_splitfields, METH_VARARGS, splitfields__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1221 {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1222 {"strip", strop_strip, METH_O, strip__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1223 {"swapcase", strop_swapcase, METH_O, swapcase__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1224 {"translate", strop_translate, METH_VARARGS, translate__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1225 {"upper", strop_upper, METH_O, upper__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1226 {NULL, NULL} /* sentinel */
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1227 };
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
1230 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1231 initstrop(void)
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1233 PyObject *m, *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1234 char buf[256];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1235 int c, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1236 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1237 (PyObject*)NULL, PYTHON_API_VERSION);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1238 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1239 return;
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1240
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1241 /* Create 'whitespace' object */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1242 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1243 for (c = 0; c < 256; c++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1244 if (isspace(c))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1245 buf[n++] = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1246 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1247 s = PyString_FromStringAndSize(buf, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1248 if (s)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1249 PyModule_AddObject(m, "whitespace", s);
22729
cdff3c7c1a76 Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents: 22421
diff changeset
1250
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1251 /* Create 'lowercase' object */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1252 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1253 for (c = 0; c < 256; c++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1254 if (islower(c))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1255 buf[n++] = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1256 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1257 s = PyString_FromStringAndSize(buf, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1258 if (s)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1259 PyModule_AddObject(m, "lowercase", s);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1260
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1261 /* Create 'uppercase' object */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1262 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1263 for (c = 0; c < 256; c++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1264 if (isupper(c))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1265 buf[n++] = c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1266 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1267 s = PyString_FromStringAndSize(buf, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1268 if (s)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56777
diff changeset
1269 PyModule_AddObject(m, "uppercase", s);
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270 }