astropy/utils/xml/src/iterparse.c
/*
 * Returns a copy of the given string (8-bit or Unicode) with the XML
 * control characters converted to XML character entities.
 *
 * If an 8-bit string is passed in, an 8-bit string is returned.  If a
 * Unicode string is passed in, a Unicode string is returned.
 */
Returns a copy of the given string (8-bit or Unicode) with the XML
control characters converted to XML character entities.

If an 8-bit string is passed in, an 8-bit string is returned.  If a
Unicode string is passed in, a Unicode string is returned.
Returns a copy of the given string ( 8 - bit or Unicode ) with the XML control characters converted to XML character entities . If an 8 - bit string is passed in an 8 - bit string is returned . If a Unicode string is passed in a Unicode string is returned .
static PyObject*
_escape_xml(PyObject* self, PyObject *args, const char** escapes)
{
    PyObject* input_obj;
    PyObject* input_coerce = NULL;
    PyObject* output_obj;
    int count = 0;
    Py_UNICODE* uinput = NULL;
    char* input = NULL;
    Py_ssize_t input_len;
    Py_UNICODE* uoutput = NULL;
    char* output = NULL;
    Py_UNICODE* up = NULL;
    char* p = NULL;
    Py_ssize_t i;
    const char** esc;
    const char* ent;
    if (!PyArg_ParseTuple(args, "O:escape_xml", &input_obj)) {
        return NULL;
    }
    if (!PyBytes_Check(input_obj)) {
        input_coerce = PyObject_Str(input_obj);
    }
    if (input_coerce) {
        uinput = PyUnicode_AsUnicode(input_coerce);
        if (uinput == NULL) {
            Py_DECREF(input_coerce);
            return NULL;
        }
        input_len = PyUnicode_GetLength(input_coerce);
        for (i = 0; i < input_len; ++i) {
            for (esc = escapes; ; esc += 2) {
                if (uinput[i] > (Py_UNICODE)**esc) {
                    break;
                } else if (uinput[i] == (Py_UNICODE)**esc) {
                    ++count;
                    break;
                }
            }
        }
        if (count) {
            uoutput = malloc((input_len + 1 + count * 5) * sizeof(Py_UNICODE));
            if (uoutput == NULL) {
                Py_DECREF(input_coerce);
                PyErr_SetString(PyExc_MemoryError, "Out of memory");
                return NULL;
            }
            up = uoutput;
            for (i = 0; i < input_len; ++i) {
                for (esc = escapes; ; esc += 2) {
                    if (uinput[i] > (Py_UNICODE)**esc) {
                        *(up++) = uinput[i];
                        break;
                    } else if (uinput[i] == (Py_UNICODE)**esc) {
                        for (ent = *(esc + 1); *ent != '\0'; ++ent) {
                            *(up++) = (Py_UNICODE)*ent;
                        }
                        break;
                    }
                }
            }
            *up = 0;
            Py_DECREF(input_coerce);
            output_obj = PyUnicode_FromUnicode(uoutput, up - uoutput);
            free(uoutput);
            return output_obj;
        } else {
            return input_coerce;
        }
    }
    input_coerce = PyObject_Bytes(input_obj);
    if (input_coerce) {
        if (PyBytes_AsStringAndSize(input_coerce, &input, &input_len) == -1) {
            Py_DECREF(input_coerce);
            return NULL;
        }
        for (i = 0; i < input_len; ++i) {
            for (esc = escapes; ; esc += 2) {
                if (input[i] > **esc) {
                    break;
                } else if (input[i] == **esc) {
                    ++count;
                    break;
                }
            }
        }
        if (count) {
            output = malloc((input_len + 1 + count * 5) * sizeof(char));
            if (output == NULL) {
                Py_DECREF(input_coerce);
                PyErr_SetString(PyExc_MemoryError, "Out of memory");
                return NULL;
            }
            p = output;
            for (i = 0; i < input_len; ++i) {
                for (esc = escapes; ; esc += 2) {
                    if (input[i] > **esc) {
                        *(p++) = input[i];
                        break;
                    } else if (input[i] == **esc) {
                        for (ent = *(esc + 1); *ent != '\0'; ++ent) {
                            *(p++) = *ent;
                        }
                        break;
                    }
                }
            }
            *p = 0;
            Py_DECREF(input_coerce);
            output_obj = PyBytes_FromStringAndSize(output, p - output);
            free(output);
            return output_obj;
        } else {
            return input_coerce;
        }
    }
    PyErr_SetString(PyExc_TypeError, "must be convertible to str or bytes");
    return NULL;
}
static PyObject * _escape_xml ( PyObject * self , PyObject * args , const char * * escapes ) { PyObject * input_obj ; PyObject * input_coerce = NULL ; PyObject * output_obj ; int count = 0 ; Py_UNICODE * uinput = NULL ; char * input = NULL ; Py_ssize_t input_len ; Py_UNICODE * uoutput = NULL ; char * output = NULL ; Py_UNICODE * up = NULL ; char * p = NULL ; Py_ssize_t i ; const char * * esc ; const char * ent ; if ( ! PyArg_ParseTuple ( args , " " , & input_obj ) ) { return NULL ; } if ( ! PyBytes_Check ( input_obj ) ) { input_coerce = PyObject_Str ( input_obj ) ; } if ( input_coerce ) { uinput = PyUnicode_AsUnicode ( input_coerce ) ; if ( uinput == NULL ) { Py_DECREF ( input_coerce ) ; return NULL ; } input_len = PyUnicode_GetLength ( input_coerce ) ; for ( i = 0 ; i < input_len ; ++ i ) { for ( esc = escapes ; ; esc += 2 ) { if ( uinput [ i ] > ( Py_UNICODE ) * * esc ) { break ; } else if ( uinput [ i ] == ( Py_UNICODE ) * * esc ) { ++ count ; break ; } } } if ( count ) { uoutput = malloc ( ( input_len + 1 + count * 5 ) * sizeof ( Py_UNICODE ) ) ; if ( uoutput == NULL ) { Py_DECREF ( input_coerce ) ; PyErr_SetString ( PyExc_MemoryError , " " ) ; return NULL ; } up = uoutput ; for ( i = 0 ; i < input_len ; ++ i ) { for ( esc = escapes ; ; esc += 2 ) { if ( uinput [ i ] > ( Py_UNICODE ) * * esc ) { * ( up ++ ) = uinput [ i ] ; break ; } else if ( uinput [ i ] == ( Py_UNICODE ) * * esc ) { for ( ent = * ( esc + 1 ) ; * ent != ' \0 ' ; ++ ent ) { * ( up ++ ) = ( Py_UNICODE ) * ent ; } break ; } } } * up = 0 ; Py_DECREF ( input_coerce ) ; output_obj = PyUnicode_FromUnicode ( uoutput , up - uoutput ) ; free ( uoutput ) ; return output_obj ; } else { return input_coerce ; } } input_coerce = PyObject_Bytes ( input_obj ) ; if ( input_coerce ) { if ( PyBytes_AsStringAndSize ( input_coerce , & input , & input_len ) == -1 ) { Py_DECREF ( input_coerce ) ; return NULL ; } for ( i = 0 ; i < input_len ; ++ i ) { for ( esc = escapes ; ; esc += 2 ) { if ( input [ i ] > * * esc ) { break ; } else if ( input [ i ] == * * esc ) { ++ count ; break ; } } } if ( count ) { output = malloc ( ( input_len + 1 + count * 5 ) * sizeof ( char ) ) ; if ( output == NULL ) { Py_DECREF ( input_coerce ) ; PyErr_SetString ( PyExc_MemoryError , " " ) ; return NULL ; } p = output ; for ( i = 0 ; i < input_len ; ++ i ) { for ( esc = escapes ; ; esc += 2 ) { if ( input [ i ] > * * esc ) { * ( p ++ ) = input [ i ] ; break ; } else if ( input [ i ] == * * esc ) { for ( ent = * ( esc + 1 ) ; * ent != ' \0 ' ; ++ ent ) { * ( p ++ ) = * ent ; } break ; } } } * p = 0 ; Py_DECREF ( input_coerce ) ; output_obj = PyBytes_FromStringAndSize ( output , p - output ) ; free ( output ) ; return output_obj ; } else { return input_coerce ; } } PyErr_SetString ( PyExc_TypeError , " " ) ; return NULL ; }
Returns a copy of the given string (8-bit or Unicode) with the XML
 control characters converted to XML character entities.
Returns a copy of the given string ( 8 - bit or Unicode ) with the XML control characters converted to XML character entities .
/* First, try as Unicode */ /* Now try as bytes */