| 1 |
|
| /* |
| 2 |
|
| * |
| 3 |
|
| * Copyright (c) 2019, cPanel, LLC. |
| 4 |
|
| * All rights reserved. |
| 5 |
|
| * http://cpanel.net |
| 6 |
|
| * |
| 7 |
|
| * This is free software; you can redistribute it and/or modify it under the |
| 8 |
|
| * same terms as Perl itself. |
| 9 |
|
| * |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #include <EXTERN.h> |
| 13 |
|
| #include <perl.h> |
| 14 |
|
| #include <XSUB.h> |
| 15 |
|
| #include <embed.h> |
| 16 |
|
| #include <string.h> |
| 17 |
|
| #include "ppport.h" |
| 18 |
|
| |
| 19 |
|
| /* stolen from B::cstring */ |
| 20 |
|
| static SV * |
| 21 |
1557 |
| cstring(pTHX_ SV *sv, bool perlstyle) |
| 22 |
|
| { |
| 23 |
|
| SV *sstr; |
| 24 |
|
| |
| 25 |
1557 |
▶ | if (!SvOK(sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv)) |
0 |
1557 |
1557 |
|
| 26 |
0 |
| return newSVpvs_flags("0", SVs_TEMP); |
| 27 |
|
| |
| 28 |
1557 |
| sstr = newSVpvs_flags("\"", SVs_TEMP); |
| 29 |
|
| |
| 30 |
2838 |
▶ | if (perlstyle && SvUTF8(sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (perlstyle && SvUTF8(sv)) { |
1419 |
138 |
1557 |
| if (perlstyle && SvUTF8(sv)) { |
1281 |
138 |
1419 |
|
| 31 |
1281 |
| SV *tmpsv = sv_newmortal(); /* Temporary SV to feed sv_uni_display */ |
| 32 |
1281 |
| const STRLEN len = SvCUR(sv); |
| 33 |
1281 |
| const char *s = sv_uni_display(tmpsv, sv, 8*len, UNI_DISPLAY_QQ); |
| 34 |
7619 |
▶ | while (*s) |
Branch: 1 branch
| Branch | True | False | Total |
| while (*s) |
6338 |
1281 |
7619 |
|
| 35 |
|
| { |
| 36 |
6338 |
▶ | if (*s == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '"') |
2 |
6336 |
6338 |
|
| 37 |
2 |
| sv_catpvs(sstr, "\\\""); |
| 38 |
6336 |
▶ | else if (*s == '$') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '$') |
2 |
6334 |
6336 |
|
| 39 |
2 |
| sv_catpvs(sstr, "\\$"); |
| 40 |
6334 |
▶ | else if (*s == '@') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '@') |
2 |
6332 |
6334 |
|
| 41 |
2 |
| sv_catpvs(sstr, "\\@"); |
| 42 |
6332 |
▶ | else if (*s == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\\') |
1093 |
5239 |
6332 |
|
| 43 |
|
| { |
| 44 |
1093 |
▶ | if (memCHRs("nrftaebx\\",*(s+1))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memCHRs("nrftaebx\\",*(s+1))) |
1093 |
0 |
1093 |
|
| 45 |
1093 |
| sv_catpvn(sstr, s++, 2); |
| 46 |
|
| else |
| 47 |
0 |
| sv_catpvs(sstr, "\\\\"); |
| 48 |
|
| } |
| 49 |
|
| else /* should always be printable */ |
| 50 |
5239 |
| sv_catpvn(sstr, s, 1); |
| 51 |
6338 |
| ++s; |
| 52 |
|
| } |
| 53 |
|
| } |
| 54 |
|
| else |
| 55 |
|
| { |
| 56 |
|
| /* XXX Optimise? */ |
| 57 |
|
| STRLEN len; |
| 58 |
276 |
| const char *s = SvPV(sv, len); |
| 59 |
692 |
▶ | for (; len; len--, s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (; len; len--, s++) |
416 |
276 |
692 |
|
| 60 |
|
| { |
| 61 |
|
| /* At least try a little for readability */ |
| 62 |
416 |
▶ | if (*s == '"') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '"') |
8 |
408 |
416 |
|
| 63 |
8 |
| sv_catpvs(sstr, "\\\""); |
| 64 |
408 |
▶ | else if (*s == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\\') |
2 |
406 |
408 |
|
| 65 |
2 |
| sv_catpvs(sstr, "\\\\"); |
| 66 |
|
| /* trigraphs - bleagh */ |
| 67 |
406 |
▶ | else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') { |
Branch: 4 branches
| Branch | True | False | Total |
| else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') { |
203 |
203 |
406 |
| else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') { |
1 |
202 |
203 |
| else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') { |
0 |
1 |
1 |
| else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?') { |
0 |
0 |
0 |
|
| 68 |
0 |
| Perl_sv_catpvf(aTHX_ sstr, "\\%03o", '?'); |
| 69 |
|
| } |
| 70 |
406 |
▶ | else if (perlstyle && *s == '$') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (perlstyle && *s == '$') |
203 |
203 |
406 |
| else if (perlstyle && *s == '$') |
1 |
202 |
203 |
|
| 71 |
1 |
| sv_catpvs(sstr, "\\$"); |
| 72 |
405 |
▶ | else if (perlstyle && *s == '@') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (perlstyle && *s == '@') |
202 |
203 |
405 |
| else if (perlstyle && *s == '@') |
1 |
201 |
202 |
|
| 73 |
1 |
| sv_catpvs(sstr, "\\@"); |
| 74 |
404 |
▶ | else if (isPRINT(*s)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (isPRINT(*s)) |
328 |
76 |
404 |
|
| 75 |
328 |
| sv_catpvn(sstr, s, 1); |
| 76 |
76 |
▶ | else if (*s == '\n') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\n') |
6 |
70 |
76 |
|
| 77 |
6 |
| sv_catpvs(sstr, "\\n"); |
| 78 |
70 |
▶ | else if (*s == '\r') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\r') |
2 |
68 |
70 |
|
| 79 |
2 |
| sv_catpvs(sstr, "\\r"); |
| 80 |
68 |
▶ | else if (*s == '\t') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\t') |
2 |
66 |
68 |
|
| 81 |
2 |
| sv_catpvs(sstr, "\\t"); |
| 82 |
66 |
▶ | else if (*s == '\a') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\a') |
4 |
62 |
66 |
|
| 83 |
4 |
| sv_catpvs(sstr, "\\a"); |
| 84 |
62 |
▶ | else if (*s == '\b') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\b') |
2 |
60 |
62 |
|
| 85 |
2 |
| sv_catpvs(sstr, "\\b"); |
| 86 |
60 |
▶ | else if (*s == '\f') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == '\f') |
2 |
58 |
60 |
|
| 87 |
2 |
| sv_catpvs(sstr, "\\f"); |
| 88 |
58 |
▶ | else if (!perlstyle && *s == '\v') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (!perlstyle && *s == '\v') |
29 |
29 |
58 |
| else if (!perlstyle && *s == '\v') |
1 |
28 |
29 |
|
| 89 |
1 |
| sv_catpvs(sstr, "\\v"); |
| 90 |
|
| else |
| 91 |
|
| { |
| 92 |
|
| /* Don't want promotion of a signed -1 char in sprintf args */ |
| 93 |
57 |
| const unsigned char c = (unsigned char) *s; |
| 94 |
57 |
| Perl_sv_catpvf(aTHX_ sstr, "\\%03o", c); |
| 95 |
|
| } |
| 96 |
|
| /* XXX Add line breaks if string is long */ |
| 97 |
|
| } |
| 98 |
|
| } |
| 99 |
1557 |
| sv_catpvs(sstr, "\""); |
| 100 |
1557 |
| return sstr; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| MODULE = XString PACKAGE = XString |
| 104 |
|
| |
| 105 |
|
| void |
| 106 |
|
| cstring(sv) |
| 107 |
|
| SV * sv |
| 108 |
|
| ALIAS: |
| 109 |
|
| perlstring = 1 |
| 110 |
|
| PPCODE: |
| 111 |
1557 |
| PUSHs( cstring(aTHX_ sv, (bool)ix) ); |