Skip to content
Myr

added macro for char* to Py_UNICODE conversion

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent c0d40d3 · Browse files · 2 files+13 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−14 removed

  • other:+13 added−14 removed

2 files+13 added−14 removed

modified:jinja2/_speedups.c+12 added-13 removed
11 unmodified lines
12 */1314#include <Python.h>1516static PyObject* markup;1716#define ESCAPED_CHARS_TABLE_SIZE 6317#define UNICHR(x) (((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))->str);1819static PyObject* markup;20static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE];21static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE];2223static int24init_constants(void)25{26 memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len));27 /* memset(escaped_chars_repl, 0, sizeof (escaped_chars_repl)); */2728 escaped_chars_delta_len['"'] = 5;30 escaped_chars_repl['"'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&quot;", 6, NULL))->str;29 escaped_chars_repl['"'] = UNICHR("&quot;");3031 escaped_chars_delta_len['&'] = 3;33 escaped_chars_repl['&'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&amp;", 5, NULL))->str;32 escaped_chars_repl['&'] = UNICHR("&amp;");33 34 escaped_chars_delta_len['<'] = 3;36 escaped_chars_repl['<'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&lt;", 4, NULL))->str;35 escaped_chars_repl['<'] = UNICHR("&lt;");36 37 escaped_chars_delta_len['>'] = 3;39 escaped_chars_repl['>'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&gt;", 4, NULL))->str;38 escaped_chars_repl['>'] = UNICHR("&gt;");39 40 PyObject *module = PyImport_ImportModule("jinja2.utils");41 if (!module)42 return 0;
34 unmodified lines
7778 outp = out->str;79 inp = in->str;80 while (erepl-- > 0) {82 /* look for the next sustitution */81 /* look for the next substitution */82 next_escp = inp;83 while (next_escp < inp_end) {85 if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && (delta_len = escaped_chars_delta_len[*next_escp])) {84 if (*next_escp < ESCAPED_CHARS_TABLE_SIZE &&85 (delta_len = escaped_chars_delta_len[*next_escp])) {86 ++delta_len;87 break;88 }89 ++next_escp;
10 unmodified lines
100 outp += delta_len;101102 inp = next_escp + 1;103 }104 if (inp < inp_end) {104 if (inp < inp_end)105 Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str));106 }106107 return (PyObject*)out;108}109
74 unmodified lines
184185186static PyMethodDef module_methods[] = {187 {"escape", (PyCFunction)escape, METH_O,189 "escape(s) -> string\n\n"188 "escape(s) -> markup\n\n"189 "Convert the characters &, <, >, and \" in string s to HTML-safe\n"190 "sequences. Use this if you need to display text that might contain\n"192 "such characters in HTML."},191 "such characters in HTML. Marks return value as markup string."},192 {"soft_unicode", (PyCFunction)soft_unicode, METH_O,193 "soft_unicode(object) -> string\n\n"194 "Make a string unicode if it isn't already. That way a markup\n"195 "string is not converted back to unicode."},
17 unmodified lines
modified:jinja2/utils.py+1 added-1 removed
478 unmodified lines
479except ImportError:480 def escape(s):481 """Convert the characters &, <, >, and " in string s to HTML-safe482 sequences. Use this if you need to display text that might contain483 such characters in HTML.483 such characters in HTML. Marks return value as markup string.484 """485 if hasattr(s, '__html__'):486 return s.__html__()487 return Markup(unicode(s)
27 unmodified lines