void
_Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- /*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (!newobj)
- return NULL;
+ Py_MEMCPY(result, cptr, len);
- s = PyBytes_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);
+ }
}
void
_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
{
- Py_ssize_t i;
-
- /*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (!newobj)
- return NULL;
+ Py_ssize_t i;
- s = PyBytes_AS_STRING(newobj);
- */
+ 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);
+ }
}
void
_Py_bytes_title(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
- int previous_is_cased = 0;
-
- /*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyBytes_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;
+ }
}
void
_Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
-
- /*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyBytes_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++;
+ }
}
void
_Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
-
- /*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyBytes_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++;
+ }
}
}
if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
- return -1;
+ 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] = i;
- for (i = 0; i < bfrm.len; i++) {
- p[((unsigned char *)bfrm.buf)[i]] = ((char *)bto.buf)[i];
- }
+ 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] = 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;
+done:
+ if (bfrm.len != -1)
+ PyBuffer_Release(&bfrm);
+ if (bto.len != -1)
+ PyBuffer_Release(&bto);
+ return res;
}