summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2001-05-24 16:56:35 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2001-05-24 16:56:35 (GMT)
commitcd35306a25ef4a7c67edc50f0cec0af5a4b7ea41 (patch)
treeb1a5ec63de12626b5597d786d247b21854a32f5c /Objects/stringobject.c
parentf8a548c23c13762ce739380c4d6f530b3297e16a (diff)
downloadcpython-cd35306a25ef4a7c67edc50f0cec0af5a4b7ea41.zip
cpython-cd35306a25ef4a7c67edc50f0cec0af5a4b7ea41.tar.gz
cpython-cd35306a25ef4a7c67edc50f0cec0af5a4b7ea41.tar.bz2
Patch #424335: Implement string_richcompare, remove string_compare.
Use new _PyString_Eq in lookdict_string.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c89
1 files changed, 77 insertions, 12 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index cf6421a..da49d81 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -630,20 +630,79 @@ string_item(PyStringObject *a, register int i)
return v;
}
-static int
-string_compare(PyStringObject *a, PyStringObject *b)
+static PyObject*
+string_richcompare(PyStringObject *a, PyStringObject *b, int op)
{
- int len_a = a->ob_size, len_b = b->ob_size;
- int min_len = (len_a < len_b) ? len_a : len_b;
- int cmp;
+ int c;
+ int len_a, len_b;
+ int min_len;
+ PyObject *result;
+
+ /* One of the objects is a string object. Make sure the
+ other one is one, too. */
+ if (a->ob_type != b->ob_type) {
+ result = Py_NotImplemented;
+ goto out;
+ }
+ if (a == b) {
+ switch (op) {
+ case Py_EQ:case Py_LE:case Py_GE:
+ result = Py_True;
+ goto out;
+ case Py_NE:case Py_LT:case Py_GT:
+ result = Py_False;
+ goto out;
+ }
+ }
+ if (op == Py_EQ) {
+ /* Supporting Py_NE here as well does not save
+ much time, since Py_NE is rarely used. */
+ if (a->ob_size == b->ob_size
+ && (a->ob_sval[0] == b->ob_sval[0]
+ && memcmp(a->ob_sval, b->ob_sval,
+ a->ob_size) == 0)) {
+ result = Py_True;
+ } else {
+ result = Py_False;
+ }
+ goto out;
+ }
+ len_a = a->ob_size; len_b = b->ob_size;
+ min_len = (len_a < len_b) ? len_a : len_b;
if (min_len > 0) {
- cmp = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
- if (cmp == 0)
- cmp = memcmp(a->ob_sval, b->ob_sval, min_len);
- if (cmp != 0)
- return cmp;
+ c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
+ if (c==0)
+ c = memcmp(a->ob_sval, b->ob_sval, min_len);
+ }else
+ c = 0;
+ if (c == 0)
+ c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
+ switch (op) {
+ case Py_LT: c = c < 0; break;
+ case Py_LE: c = c <= 0; break;
+ case Py_EQ: assert(0); break; /* unreachable */
+ case Py_NE: c = c != 0; break;
+ case Py_GT: c = c > 0; break;
+ case Py_GE: c = c >= 0; break;
+ default:
+ result = Py_NotImplemented;
+ goto out;
}
- return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
+ result = c ? Py_True : Py_False;
+ out:
+ Py_INCREF(result);
+ return result;
+}
+
+int
+_PyString_Eq(PyObject *o1, PyObject *o2)
+{
+ PyStringObject *a, *b;
+ a = (PyStringObject*)o1;
+ b = (PyStringObject*)o2;
+ return a->ob_size == b->ob_size
+ && *a->ob_sval == *b->ob_sval
+ && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0;
}
static long
@@ -2466,7 +2525,7 @@ PyTypeObject PyString_Type = {
(printfunc)string_print, /*tp_print*/
(getattrfunc)string_getattr, /*tp_getattr*/
0, /*tp_setattr*/
- (cmpfunc)string_compare, /*tp_compare*/
+ 0, /*tp_compare*/
(reprfunc)string_repr, /*tp_repr*/
0, /*tp_as_number*/
&string_as_sequence, /*tp_as_sequence*/
@@ -2479,6 +2538,12 @@ PyTypeObject PyString_Type = {
&string_as_buffer, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ (richcmpfunc)string_richcompare, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
};
void