summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2000-07-05 09:49:44 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2000-07-05 09:49:44 (GMT)
commita7acf425f68419066c8673f124703e16e463db29 (patch)
tree7901a515a0a933f4dbd0c851b7fd00565c78fdc9 /Objects
parentf3938f55c7db32deeb617da2c6f3239eb01cc20d (diff)
downloadcpython-a7acf425f68419066c8673f124703e16e463db29.zip
cpython-a7acf425f68419066c8673f124703e16e463db29.tar.gz
cpython-a7acf425f68419066c8673f124703e16e463db29.tar.bz2
Added new .isalpha() and .isalnum() methods which provide interfaces
to the new alphabetic lookup APIs in unicodectype.c.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 59824c6..78ab5ea 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3588,6 +3588,70 @@ unicode_isspace(PyUnicodeObject *self, PyObject *args)
return PyInt_FromLong(1);
}
+static char isalpha__doc__[] =
+"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.";
+
+static PyObject*
+unicode_isalpha(PyUnicodeObject *self, PyObject *args)
+{
+ register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
+ register const Py_UNICODE *e;
+
+ if (!PyArg_NoArgs(args))
+ return NULL;
+
+ /* Shortcut for single character strings */
+ if (PyUnicode_GET_SIZE(self) == 1 &&
+ Py_UNICODE_ISALPHA(*p))
+ return PyInt_FromLong(1);
+
+ /* Special case for empty strings */
+ if (PyString_GET_SIZE(self) == 0)
+ return PyInt_FromLong(0);
+
+ e = p + PyUnicode_GET_SIZE(self);
+ for (; p < e; p++) {
+ if (!Py_UNICODE_ISALPHA(*p))
+ return PyInt_FromLong(0);
+ }
+ return PyInt_FromLong(1);
+}
+
+static char isalnum__doc__[] =
+"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.";
+
+static PyObject*
+unicode_isalnum(PyUnicodeObject *self, PyObject *args)
+{
+ register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
+ register const Py_UNICODE *e;
+
+ if (!PyArg_NoArgs(args))
+ return NULL;
+
+ /* Shortcut for single character strings */
+ if (PyUnicode_GET_SIZE(self) == 1 &&
+ Py_UNICODE_ISALNUM(*p))
+ return PyInt_FromLong(1);
+
+ /* Special case for empty strings */
+ if (PyString_GET_SIZE(self) == 0)
+ return PyInt_FromLong(0);
+
+ e = p + PyUnicode_GET_SIZE(self);
+ for (; p < e; p++) {
+ if (!Py_UNICODE_ISALNUM(*p))
+ return PyInt_FromLong(0);
+ }
+ return PyInt_FromLong(1);
+}
+
static char isdecimal__doc__[] =
"S.isdecimal() -> int\n\
\n\
@@ -4253,6 +4317,8 @@ static PyMethodDef unicode_methods[] = {
{"isdecimal", (PyCFunction) unicode_isdecimal, 0, isdecimal__doc__},
{"isdigit", (PyCFunction) unicode_isdigit, 0, isdigit__doc__},
{"isnumeric", (PyCFunction) unicode_isnumeric, 0, isnumeric__doc__},
+ {"isalpha", (PyCFunction) unicode_isalpha, 0, isalpha__doc__},
+ {"isalnum", (PyCFunction) unicode_isalnum, 0, isalnum__doc__},
#if 0
{"zfill", (PyCFunction) unicode_zfill, 1, zfill__doc__},
{"capwords", (PyCFunction) unicode_capwords, 0, capwords__doc__},