]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Objects/stringlib/transmogrify.h
1 /* NOTE: this API is -ONLY- for use with single byte character strings. */
2 /* Do not use it with Unicode. */
4 /* the more complicated methods. parts of these should be pulled out into the
5 shared code in bytes_methods.c to cut down on duplicate code bloat. */
7 PyDoc_STRVAR(expandtabs__doc__
,
8 "B.expandtabs([tabsize]) -> copy of B\n\
10 Return a copy of B where all tab characters are expanded using spaces.\n\
11 If tabsize is not given, a tab size of 8 characters is assumed.");
14 stringlib_expandtabs(PyObject
*self
, PyObject
*args
)
22 if (!PyArg_ParseTuple(args
, "|i:expandtabs", &tabsize
))
25 /* First pass: determine size of output string */
27 e
= STRINGLIB_STR(self
) + STRINGLIB_LEN(self
);
28 for (p
= STRINGLIB_STR(self
); p
< e
; p
++) {
31 Py_ssize_t incr
= tabsize
- (j
% tabsize
);
32 if (j
> PY_SSIZE_T_MAX
- incr
)
38 if (j
> PY_SSIZE_T_MAX
- 1)
41 if (*p
== '\n' || *p
== '\r') {
42 if (i
> PY_SSIZE_T_MAX
- j
)
50 if (i
> PY_SSIZE_T_MAX
- j
)
53 /* Second pass: create output string and fill it */
54 u
= STRINGLIB_NEW(NULL
, i
+ j
);
61 for (p
= STRINGLIB_STR(self
); p
< e
; p
++) {
64 i
= tabsize
- (j
% tabsize
);
73 if (*p
== '\n' || *p
== '\r')
80 PyErr_SetString(PyExc_OverflowError
, "result too long");
84 Py_LOCAL_INLINE(PyObject
*)
85 pad(PyObject
*self
, Py_ssize_t left
, Py_ssize_t right
, char fill
)
94 if (left
== 0 && right
== 0 && STRINGLIB_CHECK_EXACT(self
)) {
96 /* We're defined as returning a copy; If the object is mutable
97 * that means we must make an identical copy. */
98 return STRINGLIB_NEW(STRINGLIB_STR(self
), STRINGLIB_LEN(self
));
101 return (PyObject
*)self
;
102 #endif /* STRINGLIB_MUTABLE */
105 u
= STRINGLIB_NEW(NULL
,
106 left
+ STRINGLIB_LEN(self
) + right
);
109 memset(STRINGLIB_STR(u
), fill
, left
);
110 Py_MEMCPY(STRINGLIB_STR(u
) + left
,
112 STRINGLIB_LEN(self
));
114 memset(STRINGLIB_STR(u
) + left
+ STRINGLIB_LEN(self
),
121 PyDoc_STRVAR(ljust__doc__
,
122 "B.ljust(width[, fillchar]) -> copy of B\n"
124 "Return B left justified in a string of length width. Padding is\n"
125 "done using the specified fill character (default is a space).");
128 stringlib_ljust(PyObject
*self
, PyObject
*args
)
133 if (!PyArg_ParseTuple(args
, "n|c:ljust", &width
, &fillchar
))
136 if (STRINGLIB_LEN(self
) >= width
&& STRINGLIB_CHECK_EXACT(self
)) {
137 #if STRINGLIB_MUTABLE
138 /* We're defined as returning a copy; If the object is mutable
139 * that means we must make an identical copy. */
140 return STRINGLIB_NEW(STRINGLIB_STR(self
), STRINGLIB_LEN(self
));
143 return (PyObject
*) self
;
147 return pad(self
, 0, width
- STRINGLIB_LEN(self
), fillchar
);
151 PyDoc_STRVAR(rjust__doc__
,
152 "B.rjust(width[, fillchar]) -> copy of B\n"
154 "Return B right justified in a string of length width. Padding is\n"
155 "done using the specified fill character (default is a space)");
158 stringlib_rjust(PyObject
*self
, PyObject
*args
)
163 if (!PyArg_ParseTuple(args
, "n|c:rjust", &width
, &fillchar
))
166 if (STRINGLIB_LEN(self
) >= width
&& STRINGLIB_CHECK_EXACT(self
)) {
167 #if STRINGLIB_MUTABLE
168 /* We're defined as returning a copy; If the object is mutable
169 * that means we must make an identical copy. */
170 return STRINGLIB_NEW(STRINGLIB_STR(self
), STRINGLIB_LEN(self
));
173 return (PyObject
*) self
;
177 return pad(self
, width
- STRINGLIB_LEN(self
), 0, fillchar
);
181 PyDoc_STRVAR(center__doc__
,
182 "B.center(width[, fillchar]) -> copy of B\n"
184 "Return B centered in a string of length width. Padding is\n"
185 "done using the specified fill character (default is a space).");
188 stringlib_center(PyObject
*self
, PyObject
*args
)
190 Py_ssize_t marg
, left
;
194 if (!PyArg_ParseTuple(args
, "n|c:center", &width
, &fillchar
))
197 if (STRINGLIB_LEN(self
) >= width
&& STRINGLIB_CHECK_EXACT(self
)) {
198 #if STRINGLIB_MUTABLE
199 /* We're defined as returning a copy; If the object is mutable
200 * that means we must make an identical copy. */
201 return STRINGLIB_NEW(STRINGLIB_STR(self
), STRINGLIB_LEN(self
));
204 return (PyObject
*) self
;
208 marg
= width
- STRINGLIB_LEN(self
);
209 left
= marg
/ 2 + (marg
& width
& 1);
211 return pad(self
, left
, marg
- left
, fillchar
);
214 PyDoc_STRVAR(zfill__doc__
,
215 "B.zfill(width) -> copy of B\n"
217 "Pad a numeric string B with zeros on the left, to fill a field\n"
218 "of the specified width. B is never truncated.");
221 stringlib_zfill(PyObject
*self
, PyObject
*args
)
228 if (!PyArg_ParseTuple(args
, "n:zfill", &width
))
231 if (STRINGLIB_LEN(self
) >= width
) {
232 if (STRINGLIB_CHECK_EXACT(self
)) {
233 #if STRINGLIB_MUTABLE
234 /* We're defined as returning a copy; If the object is mutable
235 * that means we must make an identical copy. */
236 return STRINGLIB_NEW(STRINGLIB_STR(self
), STRINGLIB_LEN(self
));
239 return (PyObject
*) self
;
243 return STRINGLIB_NEW(
249 fill
= width
- STRINGLIB_LEN(self
);
251 s
= pad(self
, fill
, 0, '0');
256 p
= STRINGLIB_STR(s
);
257 if (p
[fill
] == '+' || p
[fill
] == '-') {
258 /* move sign to beginning of string */
263 return (PyObject
*) s
;