📄 stringobject.c
字号:
const static char startswith__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.startswith(prefix[, start[, end]]) -> int\n\
\n\
Return 1 if S starts with the specified prefix, otherwise return 0. With\n\
optional start, test S beginning at that position. With optional end, stop\n\
comparing S at that position.";
#endif
static PyObject *
string_startswith(PyStringObject *self, PyObject *args)
{
const char* str = PyString_AS_STRING(self);
int len = PyString_GET_SIZE(self);
const char* prefix;
int plen;
int start = 0;
int end = INT_MAX;
PyObject *subobj;
if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
return NULL;
if (PyString_Check(subobj)) {
prefix = PyString_AS_STRING(subobj);
plen = PyString_GET_SIZE(subobj);
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(subobj)) {
int rc;
rc = PyUnicode_Tailmatch((PyObject *)self,
subobj, start, end, -1);
if (rc == -1)
return NULL;
else
return PyInt_FromLong((long) rc);
}
#endif
else if (PyObject_AsCharBuffer(subobj, &prefix, &plen))
return NULL;
/* adopt Java semantics for index out of range. it is legal for
* offset to be == plen, but this only returns true if prefix is
* the empty string.
*/
if (start < 0 || start+plen > len)
return PyInt_FromLong(0);
if (!memcmp(str+start, prefix, plen)) {
/* did the match end after the specified end? */
if (end < 0)
return PyInt_FromLong(1);
else if (end - start < plen)
return PyInt_FromLong(0);
else
return PyInt_FromLong(1);
}
else return PyInt_FromLong(0);
}
const static char endswith__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.endswith(suffix[, start[, end]]) -> int\n\
\n\
Return 1 if S ends with the specified suffix, otherwise return 0. With\n\
optional start, test S beginning at that position. With optional end, stop\n\
comparing S at that position.";
#endif
static PyObject *
string_endswith(PyStringObject *self, PyObject *args)
{
const char* str = PyString_AS_STRING(self);
int len = PyString_GET_SIZE(self);
const char* suffix;
int slen;
int start = 0;
int end = INT_MAX;
int lower, upper;
PyObject *subobj;
if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
return NULL;
if (PyString_Check(subobj)) {
suffix = PyString_AS_STRING(subobj);
slen = PyString_GET_SIZE(subobj);
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(subobj)) {
int rc;
rc = PyUnicode_Tailmatch((PyObject *)self,
subobj, start, end, +1);
if (rc == -1)
return NULL;
else
return PyInt_FromLong((long) rc);
}
#endif
else if (PyObject_AsCharBuffer(subobj, &suffix, &slen))
return NULL;
if (start < 0 || start > len || slen > len)
return PyInt_FromLong(0);
upper = (end >= 0 && end <= len) ? end : len;
lower = (upper - slen) > start ? (upper - slen) : start;
if (upper-lower >= slen && !memcmp(str+lower, suffix, slen))
return PyInt_FromLong(1);
else return PyInt_FromLong(0);
}
const static char encode__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.encode([encoding[,errors]]) -> object\n\
\n\
Encodes S using the codec registered for encoding. encoding defaults\n\
to the default encoding. errors may be given to set a different error\n\
handling scheme. Default is 'strict' meaning that encoding errors raise\n\
a ValueError. Other possible values are 'ignore' and 'replace'.";
#endif
static PyObject *
string_encode(PyStringObject *self, PyObject *args)
{
char *encoding = NULL;
char *errors = NULL;
if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
return NULL;
return PyString_AsEncodedObject((PyObject *)self, encoding, errors);
}
const static char decode__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.decode([encoding[,errors]]) -> object\n\
\n\
Decodes S using the codec registered for encoding. encoding defaults\n\
to the default encoding. errors may be given to set a different error\n\
handling scheme. Default is 'strict' meaning that encoding errors raise\n\
a ValueError. Other possible values are 'ignore' and 'replace'.";
#endif
static PyObject *
string_decode(PyStringObject *self, PyObject *args)
{
char *encoding = NULL;
char *errors = NULL;
if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
return NULL;
return PyString_AsDecodedObject((PyObject *)self, encoding, errors);
}
const static char expandtabs__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.expandtabs([tabsize]) -> string\n\
\n\
Return a copy of S where all tab characters are expanded using spaces.\n\
If tabsize is not given, a tab size of 8 characters is assumed.";
#endif
static PyObject*
string_expandtabs(PyStringObject *self, PyObject *args)
{
const char *e, *p;
char *q;
int i, j;
PyObject *u;
int tabsize = 8;
if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
return NULL;
/* First pass: determine size of output string */
i = j = 0;
e = PyString_AS_STRING(self) + PyString_GET_SIZE(self);
for (p = PyString_AS_STRING(self); p < e; p++)
if (*p == '\t') {
if (tabsize > 0)
j += tabsize - (j % tabsize);
}
else {
j++;
if (*p == '\n' || *p == '\r') {
i += j;
j = 0;
}
}
/* Second pass: create output string and fill it */
u = PyString_FromStringAndSize(NULL, i + j);
if (!u)
return NULL;
j = 0;
q = PyString_AS_STRING(u);
for (p = PyString_AS_STRING(self); p < e; p++)
if (*p == '\t') {
if (tabsize > 0) {
i = tabsize - (j % tabsize);
j += i;
while (i--)
*q++ = ' ';
}
}
else {
j++;
*q++ = *p;
if (*p == '\n' || *p == '\r')
j = 0;
}
return u;
}
static PyObject *
pad(PyStringObject *self, int left, int right, char fill)
{
PyObject *u;
if (left < 0)
left = 0;
if (right < 0)
right = 0;
if (left == 0 && right == 0 && PyString_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
}
u = PyString_FromStringAndSize(NULL,
left + PyString_GET_SIZE(self) + right);
if (u) {
if (left)
memset(PyString_AS_STRING(u), fill, left);
memcpy(PyString_AS_STRING(u) + left,
PyString_AS_STRING(self),
PyString_GET_SIZE(self));
if (right)
memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self),
fill, right);
}
return u;
}
const static char ljust__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.ljust(width) -> string\n"
"\n"
"Return S left justified in a string of length width. Padding is\n"
"done using spaces.";
#endif
static PyObject *
string_ljust(PyStringObject *self, PyObject *args)
{
int width;
if (!PyArg_ParseTuple(args, "i:ljust", &width))
return NULL;
if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
}
return pad(self, 0, width - PyString_GET_SIZE(self), ' ');
}
const static char rjust__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.rjust(width) -> string\n"
"\n"
"Return S right justified in a string of length width. Padding is\n"
"done using spaces.";
#endif
static PyObject *
string_rjust(PyStringObject *self, PyObject *args)
{
int width;
if (!PyArg_ParseTuple(args, "i:rjust", &width))
return NULL;
if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
}
return pad(self, width - PyString_GET_SIZE(self), 0, ' ');
}
const static char center__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.center(width) -> string\n"
"\n"
"Return S centered in a string of length width. Padding is done\n"
"using spaces.";
#endif
static PyObject *
string_center(PyStringObject *self, PyObject *args)
{
int marg, left;
int width;
if (!PyArg_ParseTuple(args, "i:center", &width))
return NULL;
if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
}
marg = width - PyString_GET_SIZE(self);
left = marg / 2 + (marg & width & 1);
return pad(self, left, marg - left, ' ');
}
const static char zfill__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.zfill(width) -> string\n"
"\n"
"Pad a numeric string S with zeros on the left, to fill a field\n"
"of the specified width. The string S is never truncated.";
#endif
static PyObject *
string_zfill(PyStringObject *self, PyObject *args)
{
int fill;
PyObject *s;
char *p;
int width;
if (!PyArg_ParseTuple(args, "i:zfill", &width))
return NULL;
if (PyString_GET_SIZE(self) >= width) {
if (PyString_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*) self;
}
else
return PyString_FromStringAndSize(
PyString_AS_STRING(self),
PyString_GET_SIZE(self)
);
}
fill = width - PyString_GET_SIZE(self);
s = pad(self, fill, 0, '0');
if (s == NULL)
return NULL;
p = PyString_AS_STRING(s);
if (p[fill] == '+' || p[fill] == '-') {
/* move sign to beginning of string */
p[0] = p[fill];
p[fill] = '0';
}
return (PyObject*) s;
}
const static char isspace__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.isspace() -> int\n"
"\n"
"Return 1 if there are only whitespace characters in S,\n"
"0 otherwise.";
#endif
static PyObject*
string_isspace(PyStringObject *self)
{
register const unsigned char *p
= (unsigned char *) PyString_AS_STRING(self);
register const unsigned char *e;
/* Shortcut for single character strings */
if (PyString_GET_SIZE(self) == 1 &&
isspace(*p))
return PyInt_FromLong(1);
/* Special case for empty strings */
if (PyString_GET_SIZE(self) == 0)
return PyInt_FromLong(0);
e = p + PyString_GET_SIZE(self);
for (; p < e; p++) {
if (!isspace(*p))
return PyInt_FromLong(0);
}
return PyInt_FromLong(1);
}
const static char isalpha__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.isalpha() -> int\n\
\n\
Return 1 if all characters in S are alphabetic\n\
and there is at least one character in S, 0 otherwise.";
#endif
static PyObject*
string_isalpha(PyStringObject *self)
{
register const unsigned char *p
= (unsigned char *) PyString_AS_STRING(self);
register const unsigned char *e;
/* Shortcut for single character strings */
if (PyString_GET_SIZE(self) == 1 &&
isalpha(*p))
return PyInt_FromLong(1);
/* Special case for empty strings */
if (PyString_GET_SIZE(self) == 0)
return PyInt_FromLong(0);
e = p + PyString_GET_SIZE(self);
for (; p < e; p++) {
if (!isalpha(*p))
return PyInt_FromLong(0);
}
return PyInt_FromLong(1);
}
const static char isalnum__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.isalnum() -> int\n\
\n\
Return 1 if all characters in S are alphanumeric\n\
and there is at least one character in S, 0 otherwise.";
#endif
static PyObject*
string_isalnum(PyStringObject *self)
{
register const unsigned char *p
= (unsigned char *) PyString_AS_STRING(self);
register const unsigned char *e;
/* Shortcut for single character strings */
if (PyString_GET_SIZE(self) == 1 &&
isalnum(*p))
return PyInt_FromLong(1);
/* Special case for empty strings */
if (PyString_GET_SIZE(self) == 0)
return PyInt_FromLong(0);
e = p + PyString_GET_SIZE(self);
for (; p < e; p++) {
if (!isalnum(*p))
return PyInt_FromLong(0);
}
return PyInt_FromLong(1);
}
const static char isdigit__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.isdigit() -> int\n\
\n\
Return 1 if there are only digit characters in S,\n\
0 otherwise.";
#endif
static PyObject*
string_isdigit(PyStringObject *self)
{
register const unsigned char *p
= (unsigned char *) PyString_AS_STRING(self);
register const unsigned char *e;
/* Shortcut for single character strings */
if (PyString_GET_SIZE(self) == 1 &&
isdigit(*p))
return PyInt_FromLong(1);
/* Special case for empty strings */
if (PyString_GET_SIZE(self) == 0)
return PyInt_FromLong(0);
e = p + PyString_GET_SIZE(self);
for (; p < e; p++) {
if (!isdigit(*p))
return PyInt_FromLong(0);
}
return PyInt_FromLong(1);
}
const static char islower__doc__[] =
#ifdef SYMBIAN
"";
#else
"S.islower() -> int\n\
\n\
Return 1 if all cased characters in S are lowercase and there is\n\
at least one cased character in S, 0 otherwise.";
#endif
static PyObject*
string_islower(PyStringObject *self)
{
register const unsigned char *p
= (unsigned char *) PyString_AS_STRING(self);
register co
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -