aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Objects/bytes_methods.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/bytes_methods.c')
-rw-r--r--Objects/bytes_methods.c225
1 files changed, 129 insertions, 96 deletions
diff --git a/Objects/bytes_methods.c b/Objects/bytes_methods.c
index 1406ac11cae..7233cea4095 100644
--- a/Objects/bytes_methods.c
+++ b/Objects/bytes_methods.c
@@ -246,23 +246,15 @@ Return a copy of B with all ASCII characters converted to lowercase.");
void
_Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- /*
- newobj = PyString_FromStringAndSize(NULL, len);
- if (!newobj)
- return NULL;
+ Py_MEMCPY(result, cptr, len);
- s = PyString_AS_STRING(newobj);
- */
-
- Py_MEMCPY(result, cptr, len);
-
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(result[i]);
- if (Py_ISUPPER(c))
- result[i] = Py_TOLOWER(c);
- }
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(result[i]);
+ if (Py_ISUPPER(c))
+ result[i] = Py_TOLOWER(c);
+ }
}
@@ -274,23 +266,15 @@ Return a copy of B with all ASCII characters converted to uppercase.");
void
_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
{
- Py_ssize_t i;
-
- /*
- newobj = PyString_FromStringAndSize(NULL, len);
- if (!newobj)
- return NULL;
-
- s = PyString_AS_STRING(newobj);
- */
+ Py_ssize_t i;
- Py_MEMCPY(result, cptr, len);
+ Py_MEMCPY(result, cptr, len);
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(result[i]);
- if (Py_ISLOWER(c))
- result[i] = Py_TOUPPER(c);
- }
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(result[i]);
+ if (Py_ISLOWER(c))
+ result[i] = Py_TOUPPER(c);
+ }
}
@@ -303,29 +287,23 @@ characters, all remaining cased characters have lowercase.");
void
_Py_bytes_title(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
- int previous_is_cased = 0;
-
- /*
- newobj = PyString_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyString_AsString(newobj);
- */
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISLOWER(c)) {
- if (!previous_is_cased)
- c = Py_TOUPPER(c);
- previous_is_cased = 1;
- } else if (Py_ISUPPER(c)) {
- if (previous_is_cased)
- c = Py_TOLOWER(c);
- previous_is_cased = 1;
- } else
- previous_is_cased = 0;
- *result++ = c;
- }
+ Py_ssize_t i;
+ int previous_is_cased = 0;
+
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISLOWER(c)) {
+ if (!previous_is_cased)
+ c = Py_TOUPPER(c);
+ previous_is_cased = 1;
+ } else if (Py_ISUPPER(c)) {
+ if (previous_is_cased)
+ c = Py_TOLOWER(c);
+ previous_is_cased = 1;
+ } else
+ previous_is_cased = 0;
+ *result++ = c;
+ }
}
@@ -338,30 +316,24 @@ and the rest lower-cased.");
void
_Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
-
- /*
- newobj = PyString_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyString_AsString(newobj);
- */
- if (0 < len) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISLOWER(c))
- *result = Py_TOUPPER(c);
- else
- *result = c;
- result++;
- }
- for (i = 1; i < len; i++) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISUPPER(c))
- *result = Py_TOLOWER(c);
- else
- *result = c;
- result++;
- }
+ Py_ssize_t i;
+
+ if (0 < len) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISLOWER(c))
+ *result = Py_TOUPPER(c);
+ else
+ *result = c;
+ result++;
+ }
+ for (i = 1; i < len; i++) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISUPPER(c))
+ *result = Py_TOLOWER(c);
+ else
+ *result = c;
+ result++;
+ }
}
@@ -374,25 +346,86 @@ to lowercase ASCII and vice versa.");
void
_Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
-
- /*
- newobj = PyString_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyString_AsString(newobj);
- */
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISLOWER(c)) {
- *result = Py_TOUPPER(c);
- }
- else if (Py_ISUPPER(c)) {
- *result = Py_TOLOWER(c);
- }
- else
- *result = c;
- result++;
+ Py_ssize_t i;
+
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISLOWER(c)) {
+ *result = Py_TOUPPER(c);
}
+ else if (Py_ISUPPER(c)) {
+ *result = Py_TOLOWER(c);
+ }
+ else
+ *result = c;
+ result++;
+ }
+}
+
+
+PyDoc_STRVAR_shared(_Py_maketrans__doc__,
+"B.maketrans(frm, to) -> translation table\n\
+\n\
+Return a translation table (a bytes object of length 256) suitable\n\
+for use in the bytes or bytearray translate method where each byte\n\
+in frm is mapped to the byte at the same position in to.\n\
+The bytes objects frm and to must be of the same length.");
+
+static Py_ssize_t
+_getbuffer(PyObject *obj, Py_buffer *view)
+{
+ PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer;
+
+ if (buffer == NULL || buffer->bf_getbuffer == NULL)
+ {
+ PyErr_Format(PyExc_TypeError,
+ "Type %.100s doesn't support the buffer API",
+ Py_TYPE(obj)->tp_name);
+ return -1;
+ }
+
+ if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
+ return -1;
+ return view->len;
}
+PyObject *
+_Py_bytes_maketrans(PyObject *args)
+{
+ PyObject *frm, *to, *res = NULL;
+ Py_buffer bfrm, bto;
+ Py_ssize_t i;
+ char *p;
+
+ bfrm.len = -1;
+ bto.len = -1;
+
+ if (!PyArg_ParseTuple(args, "OO:maketrans", &frm, &to))
+ return NULL;
+ if (_getbuffer(frm, &bfrm) < 0)
+ return NULL;
+ if (_getbuffer(to, &bto) < 0)
+ goto done;
+ if (bfrm.len != bto.len) {
+ PyErr_Format(PyExc_ValueError,
+ "maketrans arguments must have same length");
+ goto done;
+ }
+ res = PyBytes_FromStringAndSize(NULL, 256);
+ if (!res) {
+ goto done;
+ }
+ p = PyBytes_AS_STRING(res);
+ for (i = 0; i < 256; i++)
+ p[i] = (char) i;
+ for (i = 0; i < bfrm.len; i++) {
+ p[((unsigned char *)bfrm.buf)[i]] = ((char *)bto.buf)[i];
+ }
+
+done:
+ if (bfrm.len != -1)
+ PyBuffer_Release(&bfrm);
+ if (bto.len != -1)
+ PyBuffer_Release(&bto);
+ return res;
+}