Ejemplo n.º 1
0
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;
        }
}
Ejemplo n.º 2
0
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++;
        }
}
Ejemplo n.º 3
0
void
_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
{
    Py_ssize_t i;

    for (i = 0; i < len; i++) {
        result[i] = Py_TOUPPER((unsigned char) cptr[i]);
    }
}
Ejemplo n.º 4
0
void
_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
{
    Py_ssize_t i;

    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);
    }
}
Ejemplo n.º 5
0
void
_Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
{
    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++;
    }
}
Ejemplo n.º 6
0
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_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);
        }
}
Ejemplo n.º 7
0
void
_Py_bytes_title(char *result, char *s, Py_ssize_t len)
{
    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;
    }
}
Ejemplo n.º 8
0
void
_Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
{
    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++;
    }
}
Ejemplo n.º 9
0
/* Fill in the digit parts of a numbers's string representation,
   as determined in calc_number_widths().
   Return -1 on error, or 0 on success. */
static int
fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
            PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
            PyObject *prefix, Py_ssize_t p_start,
            Py_UCS4 fill_char,
            LocaleInfo *locale, int toupper)
{
    /* Used to keep track of digits, decimal, and remainder. */
    Py_ssize_t d_pos = d_start;
    const unsigned int kind = writer->kind;
    const void *data = writer->data;
    Py_ssize_t r;

    if (spec->n_lpadding) {
        _PyUnicode_FastFill(writer->buffer,
                            writer->pos, spec->n_lpadding, fill_char);
        writer->pos += spec->n_lpadding;
    }
    if (spec->n_sign == 1) {
        PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
        writer->pos++;
    }
    if (spec->n_prefix) {
        _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
                                      prefix, p_start,
                                      spec->n_prefix);
        if (toupper) {
            Py_ssize_t t;
            for (t = 0; t < spec->n_prefix; t++) {
                Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
                c = Py_TOUPPER(c);
                assert (c <= 127);
                PyUnicode_WRITE(kind, data, writer->pos + t, c);
            }
        }
        writer->pos += spec->n_prefix;
    }
    if (spec->n_spadding) {
        _PyUnicode_FastFill(writer->buffer,
                            writer->pos, spec->n_spadding, fill_char);
        writer->pos += spec->n_spadding;
    }

    /* Only for type 'c' special case, it has no digits. */
    if (spec->n_digits != 0) {
        /* Fill the digits with InsertThousandsGrouping. */
        char *pdigits;
        if (PyUnicode_READY(digits))
            return -1;
        pdigits = PyUnicode_DATA(digits);
        if (PyUnicode_KIND(digits) < kind) {
            pdigits = _PyUnicode_AsKind(digits, kind);
            if (pdigits == NULL)
                return -1;
        }
        r = _PyUnicode_InsertThousandsGrouping(
                writer->buffer, writer->pos,
                spec->n_grouped_digits,
                pdigits + kind * d_pos,
                spec->n_digits, spec->n_min_width,
                locale->grouping, locale->thousands_sep, NULL);
        if (r == -1)
            return -1;
        assert(r == spec->n_grouped_digits);
        if (PyUnicode_KIND(digits) < kind)
            PyMem_Free(pdigits);
        d_pos += spec->n_digits;
    }
    if (toupper) {
        Py_ssize_t t;
        for (t = 0; t < spec->n_grouped_digits; t++) {
            Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
            c = Py_TOUPPER(c);
            if (c > 127) {
                PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
                return -1;
            }
            PyUnicode_WRITE(kind, data, writer->pos + t, c);
        }
    }
    writer->pos += spec->n_grouped_digits;

    if (spec->n_decimal) {
        _PyUnicode_FastCopyCharacters(
            writer->buffer, writer->pos,
            locale->decimal_point, 0, spec->n_decimal);
        writer->pos += spec->n_decimal;
        d_pos += 1;
    }

    if (spec->n_remainder) {
        _PyUnicode_FastCopyCharacters(
            writer->buffer, writer->pos,
            digits, d_pos, spec->n_remainder);
        writer->pos += spec->n_remainder;
        /* d_pos += spec->n_remainder; */
    }

    if (spec->n_rpadding) {
        _PyUnicode_FastFill(writer->buffer,
                            writer->pos, spec->n_rpadding,
                            fill_char);
        writer->pos += spec->n_rpadding;
    }
    return 0;
}