summaryrefslogtreecommitdiffstats
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c102
1 files changed, 60 insertions, 42 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 3a2906c..0a9d04d 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -802,6 +802,23 @@ bytes_item(PyBytesObject *a, Py_ssize_t i)
return PyLong_FromLong((unsigned char)a->ob_sval[i]);
}
+Py_LOCAL(int)
+bytes_compare_eq(PyBytesObject *a, PyBytesObject *b)
+{
+ int cmp;
+ Py_ssize_t len;
+
+ len = Py_SIZE(a);
+ if (Py_SIZE(b) != len)
+ return 0;
+
+ if (a->ob_sval[0] != b->ob_sval[0])
+ return 0;
+
+ cmp = memcmp(a->ob_sval, b->ob_sval, len);
+ return (cmp == 0);
+}
+
static PyObject*
bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
{
@@ -822,53 +839,55 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
return NULL;
}
result = Py_NotImplemented;
- goto out;
}
- if (a == b) {
+ else if (a == b) {
switch (op) {
- case Py_EQ:case Py_LE:case Py_GE:
+ case Py_EQ:
+ case Py_LE:
+ case Py_GE:
+ /* a string is equal to itself */
result = Py_True;
- goto out;
- case Py_NE:case Py_LT:case Py_GT:
+ break;
+ case Py_NE:
+ case Py_LT:
+ case Py_GT:
result = Py_False;
- goto out;
+ break;
+ default:
+ PyErr_BadArgument();
+ return NULL;
}
}
- if (op == Py_EQ) {
- /* Supporting Py_NE here as well does not save
- much time, since Py_NE is rarely used. */
- if (Py_SIZE(a) == Py_SIZE(b)
- && (a->ob_sval[0] == b->ob_sval[0]
- && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) {
- result = Py_True;
- } else {
- result = Py_False;
+ else if (op == Py_EQ || op == Py_NE) {
+ int eq = bytes_compare_eq(a, b);
+ eq ^= (op == Py_NE);
+ result = eq ? Py_True : Py_False;
+ }
+ else {
+ len_a = Py_SIZE(a);
+ len_b = Py_SIZE(b);
+ min_len = Py_MIN(len_a, len_b);
+ if (min_len > 0) {
+ c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
+ if (c == 0)
+ c = memcmp(a->ob_sval, b->ob_sval, min_len);
}
- goto out;
- }
- len_a = Py_SIZE(a); len_b = Py_SIZE(b);
- min_len = (len_a < len_b) ? len_a : len_b;
- if (min_len > 0) {
- 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;
+ 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_GT: c = c > 0; break;
+ case Py_GE: c = c >= 0; break;
+ default:
+ PyErr_BadArgument();
+ return NULL;
+ }
+ result = c ? Py_True : Py_False;
}
- result = c ? Py_True : Py_False;
- out:
+
Py_INCREF(result);
return result;
}
@@ -1347,7 +1366,7 @@ do_argstrip(PyBytesObject *self, int striptype, PyObject *args)
{
PyObject *sep = NULL;
- if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
+ if (!PyArg_ParseTuple(args, stripformat[striptype], &sep))
return NULL;
if (sep != NULL && sep != Py_None) {
@@ -2660,9 +2679,8 @@ PyBytes_FromObject(PyObject *x)
return new;
error:
- /* Error handling when new != NULL */
Py_XDECREF(it);
- Py_DECREF(new);
+ Py_XDECREF(new);
return NULL;
}