MCPcopy Create free account
hub / github.com/IBM/Project_CodeNet / XML_escape

Function XML_escape

tools/tokenizer/pytokenize.c:763–798  ·  view source on GitHub ↗

Escape token for output as XML text.

Source from the content-addressed store, hash-verified

761
762// Escape token for output as XML text.
763static void XML_escape(FILE *out, const char *token)
764{
765#if 1
766 // Alternative: escape every <, >, and &:
767 const char *p;
768 for (p = token; *p; p++) {
769 if (*p == '<')
770 fputs("&lt;", out);
771 else
772 if (*p == '>')
773 fputs("&gt;", out);
774 else
775 if (*p == '&')
776 fputs("&amp;", out);
777 else
778 fputc(*p, out);
779 }
780#else
781 // User CDATA construct for escaping.
782 // Impossible to escape ]]> occurring in token!
783 // Must chop up the substring ]]> in ]] and >.
784 const char *p;
785 const char *q = token;
786 // "abc]]>hello" => <![CDATA["abc]]]]><![CDATA[>hello"]]>
787 // "]]>]]>" => <![CDATA[]]]]><!CDATA[>]]]]><![CDATA[>"]]>
788 while ((p = strstr(q, "]]>"))) {
789 int len = p - q; // always > 0
790 fputs("<![CDATA[", out);
791 fwrite(q, 1, len, out);
792 fputs("]]]]>", out);
793 q = p+2; // q start at >...
794 }
795 if (q < token+strlen(token))
796 fprintf(out, "<![CDATA[%s]]>", q);
797#endif
798}
799
800int main(int argc, char *argv[])
801{

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected