In perl.git, the branch blead has been updated <https://perl5.git.perl.org/perl.git/commitdiff/b1f1599cf3d4ae0cb1f1f3c9d379d89dca1873a0?hp=840d136c2c4af2a91d93c448456a52a1dda730b2>
- Log ----------------------------------------------------------------- commit b1f1599cf3d4ae0cb1f1f3c9d379d89dca1873a0 Merge: 840d136c2c d503fd62fb Author: David Mitchell <[email protected]> Date: Fri Jan 19 14:08:28 2018 +0000 [MERGE] various tr/// fixups, esp for /c and /d This branch does the following: Fixes an issue with tr/non_utf8/long_non_utf8/c, where length(long_non_utf8) > 0x7fff. Fixes an issue with tr/non_utf8/non_utf8/cd: basically, the implicit \x{100}-\x{7fffffff} added to the searchlist by /c wasn't being added. Adds a lot of code comments to the various tr/// functions. Adds tr///c tests - basically /c was almost completely untested. Changes the layout of the op_pv transliteration table: it used to be roughly 256 x short - basic table 1 x short - length of extended table (n) n x short - extended table where the 2 and 3rd items were only present under /c. Its now 1 x Size_t - length of table (256+n) (256+n) x short - table - both basic and extended where n == 0 apart from under /c. The new table format also allowed the tr/non_utf8/non_utf8/ code branches to be considerably simplified. op_dump() now dumps the contents of the (non-utf8 variant) transliteration table. Removes I32's from the tr/non_utf8/non_utf8/ code paths, making it fully 64-bit clean. Improves the pod for tr///. commit d503fd62fb1d3844fa51a15604fdceab850bc688 Author: David Mitchell <[email protected]> Date: Fri Jan 19 13:33:14 2018 +0000 perlop: improve tr/// documentation Specifically, explain more clearly what the /csd modifiers do. commit e4ee4c1bbc2315d30a73e94a1d62abf0107f5170 Author: David Mitchell <[email protected]> Date: Fri Jan 19 12:56:33 2018 +0000 tr///: eliminate I32 from the do_trans*() fns Replace each with a more appropriate type commit f0fd0980104d50d0db555dcafb3ff329e87cc3c7 Author: David Mitchell <[email protected]> Date: Fri Jan 19 12:45:37 2018 +0000 tr///: return Size_t count rather than I32 Change the signature of all the internal do_trans*() functions to return Size_t rather than I32, so that the count returned by tr//// can cope with strings longer than 2Gb. commit 2b6a74a5a093ee46c821b978c1ca79a6a85722b1 Author: David Mitchell <[email protected]> Date: Fri Jan 19 12:18:39 2018 +0000 tr///: remove some I32 from S_pmtrans() I32 to hold char counts etc is generally a bug. I've replaced with Size_t. I've left the swash part of the code alone. commit aae634e7e53e95647c40734059a045c1e0a25c7b Author: David Mitchell <[email protected]> Date: Fri Jan 19 12:01:56 2018 +0000 tr/nonutf8/nonutf8/c: simplify GROW calc When, for each slot, deciding whether to set OPpTRANS_GROWS, the calculation is only done in one of 4 possible branches. It turns out that in the other branches, the condition can never be true; but determining that is subtle, and the assumption might break for future changes. Move the test outside the if/else tree so it can be seen to always apply. So in theory this commit makes no function difference. commit a11063347569002659e0731085217d0bc0347971 Author: David Mitchell <[email protected]> Date: Thu Jan 4 16:27:46 2018 +0000 op_dump(): dump tr/// translation table previously it just displayed its address. Also, when the table is in fact a swash, don't display its address on threaded builds, as its actually just a padix. commit 0b9a13c37566388c6489a7e2d45b4e92ed36819d Author: David Mitchell <[email protected]> Date: Mon Jan 15 15:29:27 2018 +0000 tr///; simplify $utf8 =~ tr/nonutf8/nonutf8/ The run-time code to handle a non-utf8 tr/// against a utf8 string is complex, with many variants of similar code repeated depending on the presence of the /s and /c flags. Simplify them all into a single code block by changing how the translation table is stored. Formerly, the tr struct contained possibly two tables: the basic 0-255 slot one, plus in the presence of /c, a second one to map the implicit search range (\x{100}...) against any residual replacement chars not consumed by the first table. This commit merges the two tables into a single unified whole. For example tr/\x00-\xfe/abcd/c is equivalent to tr/xff-\x{7fffffff}/abcd/ which generates a 259-entry translation table consisting of: 0x00 => -1 0x01 => -1 ... 0xfe => -1 0xff => a 0x100 => b 0x101 => c 0x102 => d In addition we store: 1) the size of the translation table (0x103 in the example above); 2) an extra 'wildcard' entry stored 1 slot beyond the main table, which specifies the action for any codepoints outside the range of the table (i.e. chars 0x103..0x7fffffff). This can be either: a) a character, when the last replacement char is repeated; b) -1 when /c isn't in effect; c) -2 when /d is in effect; c) -3 identity: when the replacement list is empty but not /d. In the example above, this would be 0x103 => d The addition of -3 as a valid slot value is new. This makes the main runtime code for the utf8 string with non-utf8 tr// case look like, at its core: size = tbl->size; mapped_ch = tbl->map[ch >= size ? size : ch]; which then processes mapped_ch based on whether its >=0, or -1/-2/-3. This is a lot simpler than the old scheme, and should generally be faster too. commit 6d63cc8e88a2b96ed80956f16c0978d790bf4411 Author: David Mitchell <[email protected]> Date: Fri Jan 12 16:21:48 2018 +0000 tr///c: handle len(replacement charlist) > 32767 RT #132608 In the non-utf8 case, the /c (complement) flag to tr adds an implied \x{100}-\x{7fffffff} range to the search charlist. If the replacement list contains more chars than are paired with the 0-255 part of the search list, then the excess chars are stored in an extended part of the table. The excess char count was being stored as a short, which caused problems if the replacement list contained more than 32767 excess chars: either substituting the wrong char, or substituting for a char located up to 0xffff bytes in memory before the real translation table. So change it to SSize_t. Note that this is only a problem when the search and replacement charlists are non-utf8, the replacement list contains around 0x8000+ entries, and where the string being translated is utf8 with at least one codepoint >= U+8000. commit c923a6996655868e1b5140e8e47c2514e006902b Author: David Mitchell <[email protected]> Date: Fri Jan 12 14:35:03 2018 +0000 B, Deparse fixups for tr///c Recent commits slightly changed the layout of the extended map table: it now always stores a repeat count, and there are now two structs defined, rather than treating certain slots, like tbl[0x101], specially. Update B and Deparse to reflect this. commit c1048fcffff9a2b16ece33136c56febc7e5e4396 Author: David Mitchell <[email protected]> Date: Fri Jan 12 12:00:30 2018 +0000 add two structs for OP_TRANS Originally, the op_pv of an OP_TRANS op pointed to a 256-slot array of shorts, which contained the translations. However, in the presence of tr///c, extra information needs to be stored to handle utf8 strings. The 256 slot array was extended, with slot 0x100 holding a length, and slots 0x101 holding some extra chars. This has made things a bit messy, so this commit adds two structs, one being an array of 256 shorts, and the other being the same but with some extra fields. So for example tbl->[0x100] has been replaced with tbl->excess_len. This commit should make no functional difference, but will allow us shortly to fix a bug by changing the type of the excess_len field from short to something bigger, for example. commit 5f63644886c09045a5d86d169abdfea9e804bce7 Author: David Mitchell <[email protected]> Date: Thu Jan 11 14:41:33 2018 +0000 S_do_trans_complex(): re-indent outdent a code block following previous commit. commit f146bab680e5a22cf4657df42193385ddd2b538c Author: David Mitchell <[email protected]> Date: Thu Jan 11 11:45:49 2018 +0000 fix "\x{100}..." =~ tr/.../.../cd In transliterations where the search and replacement charlists are non-utf8, but where the string being modified contains codepoints >= 0x100, then tr/.../.../cd would always delete all such codepoints, rather than potentially mapping some of them. In more detail: in the presence of /c (complement), an implicit 0x100..0x7fffffff is added to a non-utf8 search charlist. If the replacement list is longer than the < 0x100 part of the search list, then the last few replacement chars should in principle be paired off against the first few of (\x100, \x101, ...). However, this wasn't happening. For example, tr/\x00-\xfd/ABCD/cd should be equivalent to tr/\xfe-\x{7fffffff}/ABCD/d which should map: \xfe => A, \xff => B, \x{100} => C, \x{101} => D, and delete \x{102} onwards. But instead, it behaved like tr/\xfe-\x{7fffffff}/AB/d and deleted all codepoints >= 0x100. This commit fixes that by using the extended mapping table format for all /c variants (formerly it excluded /cd). I also changed a variable holding the mapped char from being I32 to UV: principally to avoid a casting mess in the fixed code. This may (or may not), as a side-effect, have fixed possible issues with very large codepoints. commit ea088e559c9bca8e7337d3d6236f06deb5afda32 Author: David Mitchell <[email protected]> Date: Tue Jan 9 10:05:33 2018 +0000 OP_TRANS: change extended table format For non-utf8, OP_TRANS(R) ops have a translation table consisting of an array of 256 shorts attached. For tr///c, this table is extended to hold information about chars in the replacement list which aren't paired with chars in the search list. For example, tr/\x00-AE-\xff/bcdefg/c is equivalent to tr/BCD\x{100}-\x{7fffffff}/bcdefg/ which is equivalent to tr/BCD\x{100}-\x{7fffffff}/bcdefggggggggg..../ Only the BCD => bcd mappings can be stored in the basic 256-slot table, so potentially the following extra information needs recording in an extended table to handle codepoints > 0xff in the string being modified: 1) the extra replacement chars ("efg"); 2) the number of extra replacement chars (3); 3) the "repeat" char ('g'). Currently 2) and 3) are combined: the repeat char is found as the last extra char, and if there are no extra chars, the repeat char is treated as an extra char list of length 1. Similarly, an 'extra chars' length value of 1 can imply either one extra char, or no extra chars with the repeat char being faked as an extra char. An 'extra chars' length of 0 implies an empty replacement list, i.e. tr/....//c. This commit changes it so that the repeat char is *always* stored (in slot 0x101), with the extra chars stored beginning at slot 0x102. The 'extra chars' length value (located at slot 0x0100) has changed its meaning slightly: now -1 implies tr/....//c 0 implies no more replacement chars than search chars 1+ the number of excess replacement chars. This (should) make no function difference, but the extra information stored will make it easier to fix some bugs shortly. commit ddcffd84df3b0cf7b2325bed1db38b89bf19fa53 Author: David Mitchell <[email protected]> Date: Mon Jan 8 16:14:17 2018 +0000 S_pmtrans(): add assert and simplify conditional in tr/search/replace/c, the number of 'paired' replacement chars will always be <= length(replace). Assert this, and thus simplify a couple of conditionals from >= to ==. It should make no difference to execution, but reduces the cognitive load. commit 3325280992ff265f86b809c2dc177658f00b4414 Author: David Mitchell <[email protected]> Date: Thu Jan 4 13:20:59 2018 +0000 t/op/tr.t: add tr///c tests The /c (complement) flag is almost completely untested. Indeed, for the all non-utf8 case, nothing in core exercises a plain tr///c. So this commit adds reasonably comprehensive tests for tr//c and variants (/cs, /cd, /csd) where the search and replacement ranges are non-utf8, and the string being matched may or may not be utf8. A few tests are TODO for now as I've exposed some bugs - to be fixed shortly. commit b70edc9d489a5c35fa1082bd8d3bf971c06bf762 Author: David Mitchell <[email protected]> Date: Mon Jan 8 15:42:23 2018 +0000 S_pmtrans(): always use op_private flag variables Various flag vars are set early on, such as: const I32 complement = o->op_private & OPpTRANS_COMPLEMENT; but sometimes these vars weren't being used, and op_private was being tested again. commit 82e3a3828dedea0e9162b046b45c56394740d72e Author: David Mitchell <[email protected]> Date: Tue Dec 26 16:43:31 2017 +0000 remove fossil debugging statement from do_trans() This: DEBUG_t( Perl_deb(aTHX_ "2.TBL\n")); has been around in one form or another since perl1, but it makes no sense since perl5,000, where -Dt now shows the name of the op being executed. commit 48bc77752eb4d6a5e5161767ee11bf36c89b3e7c Author: David Mitchell <[email protected]> Date: Tue Dec 26 17:11:01 2017 +0000 S_pmtrans(): remove some whitespace Removal of MAD a long time ago left a couple of lines with very weird indentation. commit 334c6444f99a7a99b8470da2c82ad066563f162a Author: David Mitchell <[email protected]> Date: Tue Dec 26 16:40:14 2017 +0000 tr/// functions: add some basic code comments For the various C functions which implement the compile-time and run-time aspects of OP_TRANS, add some basic code comments at the top of each function explaining what its purpose is. Also add lots of code comments to the body of S_pmtrans() (which compiles a tr///). Also comment what the OPpTRANS_ private flag bits mean. No functional changes. ----------------------------------------------------------------------- Summary of changes: doop.c | 218 +++++++++++++++------------- dump.c | 38 ++++- embed.fnc | 14 +- ext/B/B.xs | 26 ++-- lib/B/Deparse.pm | 9 +- lib/B/Deparse.t | 14 ++ op.c | 201 +++++++++++++++++++------- op.h | 9 ++ pod/perlop.pod | 26 +++- pp.c | 4 +- proto.h | 14 +- regen/op_private | 13 +- t/op/tr.t | 428 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 13 files changed, 811 insertions(+), 203 deletions(-) diff --git a/doop.c b/doop.c index 22942818f1..0e2a8da040 100644 --- a/doop.c +++ b/doop.c @@ -27,14 +27,22 @@ #include <signal.h> #endif -STATIC I32 + +/* Helper function for do_trans(). + * Handles non-utf8 cases(*) not involving the /c, /d, /s flags, + * and where search and replacement charlists aren't identical. + * (*) i.e. where the search and replacement charlists are non-utf8. sv may + * or may not be utf8. + */ + +STATIC Size_t S_do_trans_simple(pTHX_ SV * const sv) { - I32 matches = 0; + Size_t matches = 0; STRLEN len; U8 *s = (U8*)SvPV_nomg(sv,len); U8 * const send = s+len; - const short * const tbl = (short*)cPVOP->op_pv; + const OPtrans_map * const tbl = (OPtrans_map*)cPVOP->op_pv; PERL_ARGS_ASSERT_DO_TRANS_SIMPLE; @@ -44,7 +52,7 @@ S_do_trans_simple(pTHX_ SV * const sv) /* First, take care of non-UTF-8 input strings, because they're easy */ if (!SvUTF8(sv)) { while (s < send) { - const I32 ch = tbl[*s]; + const short ch = tbl->map[*s]; if (ch >= 0) { matches++; *s = (U8)ch; @@ -54,7 +62,7 @@ S_do_trans_simple(pTHX_ SV * const sv) SvSETMAGIC(sv); } else { - const I32 grows = PL_op->op_private & OPpTRANS_GROWS; + const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS); U8 *d; U8 *dstart; @@ -66,13 +74,13 @@ S_do_trans_simple(pTHX_ SV * const sv) dstart = d; while (s < send) { STRLEN ulen; - I32 ch; + short ch; /* Need to check this, otherwise 128..255 won't match */ const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT); - if (c < 0x100 && (ch = tbl[c]) >= 0) { + if (c < 0x100 && (ch = tbl->map[c]) >= 0) { matches++; - d = uvchr_to_utf8(d, ch); + d = uvchr_to_utf8(d, (UV)ch); s += ulen; } else { /* No match -> copy */ @@ -95,14 +103,25 @@ S_do_trans_simple(pTHX_ SV * const sv) return matches; } -STATIC I32 + +/* Helper function for do_trans(). + * Handles non-utf8 cases(*) where search and replacement charlists are + * identical: so the string isn't modified, and only a count of modifiable + * chars is needed. + * Note that it doesn't handle /d or /s, since these modify the string + * even if the replacement list is empty. + * (*) i.e. where the search and replacement charlists are non-utf8. sv may + * or may not be utf8. + */ + +STATIC Size_t S_do_trans_count(pTHX_ SV * const sv) { STRLEN len; const U8 *s = (const U8*)SvPV_nomg_const(sv, len); const U8 * const send = s + len; - I32 matches = 0; - const short * const tbl = (short*)cPVOP->op_pv; + Size_t matches = 0; + const OPtrans_map * const tbl = (OPtrans_map*)cPVOP->op_pv; PERL_ARGS_ASSERT_DO_TRANS_COUNT; @@ -111,17 +130,17 @@ S_do_trans_count(pTHX_ SV * const sv) if (!SvUTF8(sv)) { while (s < send) { - if (tbl[*s++] >= 0) + if (tbl->map[*s++] >= 0) matches++; } } else { - const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT; + const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT); while (s < send) { STRLEN ulen; const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT); if (c < 0x100) { - if (tbl[c] >= 0) + if (tbl->map[c] >= 0) matches++; } else if (complement) matches++; @@ -132,14 +151,22 @@ S_do_trans_count(pTHX_ SV * const sv) return matches; } -STATIC I32 + +/* Helper function for do_trans(). + * Handles non-utf8 cases(*) involving the /c, /d, /s flags, + * and where search and replacement charlists aren't identical. + * (*) i.e. where the search and replacement charlists are non-utf8. sv may + * or may not be utf8. + */ + +STATIC Size_t S_do_trans_complex(pTHX_ SV * const sv) { STRLEN len; U8 *s = (U8*)SvPV_nomg(sv, len); U8 * const send = s+len; - I32 matches = 0; - const short * const tbl = (short*)cPVOP->op_pv; + Size_t matches = 0; + const OPtrans_map * const tbl = (OPtrans_map*)cPVOP->op_pv; PERL_ARGS_ASSERT_DO_TRANS_COMPLEX; @@ -153,7 +180,7 @@ S_do_trans_complex(pTHX_ SV * const sv) if (PL_op->op_private & OPpTRANS_SQUASH) { const U8* p = send; while (s < send) { - const I32 ch = tbl[*s]; + const short ch = tbl->map[*s]; if (ch >= 0) { *d = (U8)ch; matches++; @@ -169,7 +196,7 @@ S_do_trans_complex(pTHX_ SV * const sv) } else { while (s < send) { - const I32 ch = tbl[*s]; + const short ch = tbl->map[*s]; if (ch >= 0) { matches++; *d++ = (U8)ch; @@ -185,102 +212,55 @@ S_do_trans_complex(pTHX_ SV * const sv) SvCUR_set(sv, d - dstart); } else { /* is utf8 */ - const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT; - const I32 grows = PL_op->op_private & OPpTRANS_GROWS; - const I32 del = PL_op->op_private & OPpTRANS_DELETE; + const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH); + const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS); U8 *d; U8 *dstart; - STRLEN rlen = 0; + Size_t size = tbl->size; + UV pch = 0xfeedface; if (grows) Newx(d, len*2+1, U8); else d = s; dstart = d; - if (complement && !del) - rlen = tbl[0x100]; - if (PL_op->op_private & OPpTRANS_SQUASH) { - UV pch = 0xfeedface; while (s < send) { STRLEN len; const UV comp = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT); - I32 ch; + UV ch; + short sch; - if (comp > 0xff) { - if (!complement) { - Move(s, d, len, U8); - d += len; - } - else { - matches++; - if (!del) { - ch = (rlen == 0) ? (I32)comp : - (comp - 0x100 < rlen) ? - tbl[comp+1] : tbl[0x100+rlen]; - if ((UV)ch != pch) { - d = uvchr_to_utf8(d, ch); - pch = (UV)ch; - } - s += len; - continue; - } - } - } - else if ((ch = tbl[comp]) >= 0) { + sch = tbl->map[comp >= size ? size : comp]; + + if (sch >= 0) { + ch = (UV)sch; + replace: matches++; - if ((UV)ch != pch) { + if (LIKELY(!squash || ch != pch)) { d = uvchr_to_utf8(d, ch); - pch = (UV)ch; + pch = ch; } s += len; continue; } - else if (ch == -1) { /* -1 is unmapped character */ + else if (sch == -1) { /* -1 is unmapped character */ Move(s, d, len, U8); d += len; } - else if (ch == -2) /* -2 is delete character */ + else if (sch == -2) /* -2 is delete character */ matches++; + else { + assert(sch == -3); /* -3 is empty replacement */ + ch = comp; + goto replace; + } + s += len; pch = 0xfeedface; } - } - else { - while (s < send) { - STRLEN len; - const UV comp = utf8n_to_uvchr(s, send - s, &len, - UTF8_ALLOW_DEFAULT); - I32 ch; - if (comp > 0xff) { - if (!complement) { - Move(s, d, len, U8); - d += len; - } - else { - matches++; - if (!del) { - if (comp - 0x100 < rlen) - d = uvchr_to_utf8(d, tbl[comp+1]); - else - d = uvchr_to_utf8(d, tbl[0x100+rlen]); - } - } - } - else if ((ch = tbl[comp]) >= 0) { - d = uvchr_to_utf8(d, ch); - matches++; - } - else if (ch == -1) { /* -1 is unmapped character */ - Move(s, d, len, U8); - d += len; - } - else if (ch == -2) /* -2 is delete character */ - matches++; - s += len; - } - } + if (grows) { sv_setpvn(sv, (char*)dstart, d - dstart); Safefree(dstart); @@ -295,7 +275,15 @@ S_do_trans_complex(pTHX_ SV * const sv) return matches; } -STATIC I32 + +/* Helper function for do_trans(). + * Handles utf8 cases(*) not involving the /c, /d, /s flags, + * and where search and replacement charlists aren't identical. + * (*) i.e. where the search or replacement charlists are utf8. sv may + * or may not be utf8. + */ + +STATIC Size_t S_do_trans_simple_utf8(pTHX_ SV * const sv) { U8 *s; @@ -303,8 +291,8 @@ S_do_trans_simple_utf8(pTHX_ SV * const sv) U8 *d; U8 *start; U8 *dstart, *dend; - I32 matches = 0; - const I32 grows = PL_op->op_private & OPpTRANS_GROWS; + Size_t matches = 0; + const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS); STRLEN len; SV* const rv = #ifdef USE_ITHREADS @@ -393,13 +381,24 @@ S_do_trans_simple_utf8(pTHX_ SV * const sv) return matches; } -STATIC I32 + +/* Helper function for do_trans(). + * Handles utf8 cases(*) where search and replacement charlists are + * identical: so the string isn't modified, and only a count of modifiable + * chars is needed. + * Note that it doesn't handle /d or /s, since these modify the string + * even if the replacement charlist is empty. + * (*) i.e. where the search or replacement charlists are utf8. sv may + * or may not be utf8. + */ + +STATIC Size_t S_do_trans_count_utf8(pTHX_ SV * const sv) { const U8 *s; const U8 *start = NULL; const U8 *send; - I32 matches = 0; + Size_t matches = 0; STRLEN len; SV* const rv = #ifdef USE_ITHREADS @@ -436,15 +435,23 @@ S_do_trans_count_utf8(pTHX_ SV * const sv) return matches; } -STATIC I32 + +/* Helper function for do_trans(). + * Handles utf8 cases(*) involving the /c, /d, /s flags, + * and where search and replacement charlists aren't identical. + * (*) i.e. where the search or replacement charlists are utf8. sv may + * or may not be utf8. + */ + +STATIC Size_t S_do_trans_complex_utf8(pTHX_ SV * const sv) { U8 *start, *send; U8 *d; - I32 matches = 0; - const I32 squash = PL_op->op_private & OPpTRANS_SQUASH; - const I32 del = PL_op->op_private & OPpTRANS_DELETE; - const I32 grows = PL_op->op_private & OPpTRANS_GROWS; + Size_t matches = 0; + const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH); + const bool del = cBOOL(PL_op->op_private & OPpTRANS_DELETE); + const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS); SV* const rv = #ifdef USE_ITHREADS PAD_SVl(cPADOP->op_padix); @@ -597,12 +604,19 @@ S_do_trans_complex_utf8(pTHX_ SV * const sv) return matches; } -I32 + +/* Execute a tr//. sv is the value to be translated, while PL_op + * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a + * translation table or whose op_sv field contains a swash. + * Returns a count of number of characters translated + */ + +Size_t Perl_do_trans(pTHX_ SV *sv) { STRLEN len; - const I32 flags = PL_op->op_private; - const I32 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF); + const U8 flags = PL_op->op_private; + const U8 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF); PERL_ARGS_ASSERT_DO_TRANS; @@ -618,8 +632,6 @@ Perl_do_trans(pTHX_ SV *sv) (void)SvPOK_only_UTF8(sv); } - DEBUG_t( Perl_deb(aTHX_ "2.TBL\n")); - /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check, * we must also rely on it to choose the readonly strategy. */ diff --git a/dump.c b/dump.c index b2f0fc5ef2..bdf285303d 100644 --- a/dump.c +++ b/dump.c @@ -1263,10 +1263,42 @@ S_do_op_dump_bar(pTHX_ I32 level, UV bar, PerlIO *file, const OP *o) case OP_TRANS: case OP_TRANSR: + if (o->op_private & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF)) { + /* utf8: table stored as a swash */ +#ifndef USE_ITHREADS + /* with ITHREADS, swash is stored in the pad, and the right pad + * may not be active here, so skip */ S_opdump_indent(aTHX_ o, level, bar, file, - "PV = 0x%" UVxf "\n", - PTR2UV(cPVOPo->op_pv)); - break; + "SWASH = 0x%" UVxf "\n", + PTR2UV(MUTABLE_SV(cSVOPo->op_sv))); +#endif + } + else { + const OPtrans_map * const tbl = (OPtrans_map*)cPVOPo->op_pv; + SSize_t i, size = tbl->size; + + S_opdump_indent(aTHX_ o, level, bar, file, + "TABLE = 0x%" UVxf "\n", + PTR2UV(tbl)); + S_opdump_indent(aTHX_ o, level, bar, file, + " SIZE: 0x%" UVxf "\n", (UV)size); + + /* dump size+1 values, to include the extra slot at the end */ + for (i = 0; i <= size; i++) { + short val = tbl->map[i]; + if ((i & 0xf) == 0) + S_opdump_indent(aTHX_ o, level, bar, file, + " %4" UVxf ":", (UV)i); + if (val < 0) + PerlIO_printf(file, " %2" IVdf, (IV)val); + else + PerlIO_printf(file, " %02" UVxf, (UV)val); + + if ( i == size || (i & 0xf) == 0xf) + PerlIO_printf(file, "\n"); + } + } + break; default: diff --git a/embed.fnc b/embed.fnc index cd654dd1e7..b9351be60f 100644 --- a/embed.fnc +++ b/embed.fnc @@ -521,7 +521,7 @@ p |Off_t |do_sysseek |NN GV* gv|Off_t pos|int whence : Defined in doio.c, used only in pp_sys.c pR |Off_t |do_tell |NN GV* gv : Defined in doop.c, used only in pp.c -p |I32 |do_trans |NN SV* sv +p |Size_t |do_trans |NN SV* sv : Used in my.c and pp.c p |UV |do_vecget |NN SV* sv|STRLEN offset|int size : Defined in doop.c, used only in mg.c (with /* XXX slurp this routine */) @@ -2091,12 +2091,12 @@ Adp |int |nothreadhook p |void |init_constants #if defined(PERL_IN_DOOP_C) -sR |I32 |do_trans_simple |NN SV * const sv -sR |I32 |do_trans_count |NN SV * const sv -sR |I32 |do_trans_complex |NN SV * const sv -sR |I32 |do_trans_simple_utf8 |NN SV * const sv -sR |I32 |do_trans_count_utf8 |NN SV * const sv -sR |I32 |do_trans_complex_utf8 |NN SV * const sv +sR |Size_t |do_trans_simple |NN SV * const sv +sR |Size_t |do_trans_count |NN SV * const sv +sR |Size_t |do_trans_complex |NN SV * const sv +sR |Size_t |do_trans_simple_utf8 |NN SV * const sv +sR |Size_t |do_trans_count_utf8 |NN SV * const sv +sR |Size_t |do_trans_complex_utf8 |NN SV * const sv #endif #if defined(PERL_IN_GV_C) diff --git a/ext/B/B.xs b/ext/B/B.xs index 6211225e22..d9d77157c6 100644 --- a/ext/B/B.xs +++ b/ext/B/B.xs @@ -1022,20 +1022,18 @@ next(o) ret = make_sv_object(aTHX_ NULL); break; case 41: /* B::PVOP::pv */ - /* OP_TRANS uses op_pv to point to a table of 256 or >=258 - * shorts whereas other PVOPs point to a null terminated - * string. */ - if ( (cPVOPo->op_type == OP_TRANS - || cPVOPo->op_type == OP_TRANSR) && - (cPVOPo->op_private & OPpTRANS_COMPLEMENT) && - !(cPVOPo->op_private & OPpTRANS_DELETE)) - { - const short* const tbl = (short*)cPVOPo->op_pv; - const short entries = 257 + tbl[256]; - ret = newSVpvn_flags(cPVOPo->op_pv, entries * sizeof(short), SVs_TEMP); - } - else if (cPVOPo->op_type == OP_TRANS || cPVOPo->op_type == OP_TRANSR) { - ret = newSVpvn_flags(cPVOPo->op_pv, 256 * sizeof(short), SVs_TEMP); + /* OP_TRANS uses op_pv to point to a OPtrans_map struct, + * whereas other PVOPs point to a null terminated string. + * For trans, for now just return the whole struct as a + * string and let the caller unpack() it */ + if ( cPVOPo->op_type == OP_TRANS + || cPVOPo->op_type == OP_TRANSR) + { + const OPtrans_map *const tbl = (OPtrans_map*)cPVOPo->op_pv; + ret = newSVpvn_flags(cPVOPo->op_pv, + (char*)(&tbl->map[tbl->size + 1]) + - (char*)tbl, + SVs_TEMP); } else ret = newSVpvn_flags(cPVOPo->op_pv, strlen(cPVOPo->op_pv), SVs_TEMP); diff --git a/lib/B/Deparse.pm b/lib/B/Deparse.pm index 86f262acf1..ab691c2491 100644 --- a/lib/B/Deparse.pm +++ b/lib/B/Deparse.pm @@ -58,6 +58,8 @@ our $AUTOLOAD; use warnings (); require feature; +use Config; + BEGIN { # List version-specific constants here. # Easiest way to keep this code portable between version looks to @@ -5607,8 +5609,11 @@ sub collapse { sub tr_decode_byte { my($table, $flags) = @_; - my(@table) = unpack("s*", $table); - splice @table, 0x100, 1; # Number of subsequent elements + my $ssize_t = $Config{ptrsize} == 8 ? 'q' : 'l'; + my ($size, @table) = unpack("${ssize_t}s*", $table); + printf "XXX len=%d size=%d scalar\@table=%d\n", length($table), $size, scalar@table; + pop @table; # remove the wildcard final entry + my($c, $tr, @from, @to, @delfrom, $delhyphen); if ($table[ord "-"] != -1 and $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1) diff --git a/lib/B/Deparse.t b/lib/B/Deparse.t index 00fbb01cf8..cbee5427a8 100644 --- a/lib/B/Deparse.t +++ b/lib/B/Deparse.t @@ -1444,6 +1444,20 @@ tr/X//d; tr/X//s; tr/X//r; #### +# tr with extended table (/c) +tr/\000-\375/AB/c; +tr/\000-\375/A-C/c; +tr/\000-\375/A-D/c; +tr/\000-\375/A-Z/c; +tr/\000-\375/AB/cd; +tr/\000-\375/A-C/cd; +tr/\000-\375/A-D/cd; +tr/\000-\375/A-Z/cd; +tr/\000-\375/AB/cds; +tr/\000-\375/A-C/cds; +tr/\000-\375/A-D/cds; +tr/\000-\375/A-Z/cds; +#### # [perl #119807] s//\(3)/ge should not warn when deparsed (\3 warns) s/foo/\(3);/eg; #### diff --git a/op.c b/op.c index ace79ad14c..2b87f9c781 100644 --- a/op.c +++ b/op.c @@ -6294,6 +6294,10 @@ Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) return fold_constants(op_integerize(op_std_init((OP *)binop))); } +/* Helper function for S_pmtrans(): comparison function to sort an array + * of codepoint range pairs. Sorts by start point, or if equal, by end + * point */ + static int uvcompare(const void *a, const void *b) __attribute__nonnull__(1) __attribute__nonnull__(2) @@ -6311,24 +6315,39 @@ static int uvcompare(const void *a, const void *b) return 0; } +/* Given an OP_TRANS / OP_TRANSR op o, plus OP_CONST ops expr and repl + * containing the search and replacement strings, assemble into + * a translation table attached as o->op_pv. + * Free expr and repl. + * It expects the toker to have already set the + * OPpTRANS_COMPLEMENT + * OPpTRANS_SQUASH + * OPpTRANS_DELETE + * flags as appropriate; this function may add + * OPpTRANS_FROM_UTF + * OPpTRANS_TO_UTF + * OPpTRANS_IDENTICAL + * OPpTRANS_GROWS + * flags + */ + static OP * S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) { SV * const tstr = ((SVOP*)expr)->op_sv; - SV * const rstr = - ((SVOP*)repl)->op_sv; + SV * const rstr = ((SVOP*)repl)->op_sv; STRLEN tlen; STRLEN rlen; const U8 *t = (U8*)SvPV_const(tstr, tlen); const U8 *r = (U8*)SvPV_const(rstr, rlen); - I32 i; - I32 j; - I32 grows = 0; - short *tbl; - - const I32 complement = o->op_private & OPpTRANS_COMPLEMENT; - const I32 squash = o->op_private & OPpTRANS_SQUASH; - I32 del = o->op_private & OPpTRANS_DELETE; + Size_t i, j; + bool grows = FALSE; + OPtrans_map *tbl; + SSize_t struct_size; /* malloced size of table struct */ + + const bool complement = cBOOL(o->op_private & OPpTRANS_COMPLEMENT); + const bool squash = cBOOL(o->op_private & OPpTRANS_SQUASH); + const bool del = cBOOL(o->op_private & OPpTRANS_DELETE); SV* swash; PERL_ARGS_ASSERT_PMTRANS; @@ -6342,6 +6361,14 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) o->op_private |= OPpTRANS_TO_UTF; if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { + + /* for utf8 translations, op_sv will be set to point to a swash + * containing codepoint ranges. This is done by first assembling + * a textual representation of the ranges in listsv then compiling + * it using swash_init(). For more details of the textual format, + * see L<perlunicode.pod/"User-Defined Character Properties"> . + */ + SV* const listsv = newSVpvs("# comment\n"); SV* transv = NULL; const U8* tend = t + tlen; @@ -6383,15 +6410,24 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) * odd. */ if (complement) { + /* utf8 and /c: + * replace t/tlen/tend with a version that has the ranges + * complemented + */ U8 tmpbuf[UTF8_MAXBYTES+1]; UV *cp; UV nextmin = 0; Newx(cp, 2*tlen, UV); i = 0; transv = newSVpvs(""); + + /* convert search string into array of (start,end) range + * codepoint pairs stored in cp[]. Most "ranges" will start + * and end at the same char */ while (t < tend) { cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags); t += ulen; + /* the toker converts X-Y into (X, ILLEGAL_UTF8_BYTE, Y) */ if (t < tend && *t == ILLEGAL_UTF8_BYTE) { t++; cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags); @@ -6402,7 +6438,19 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) } i++; } + + /* sort the ranges */ qsort(cp, i, 2*sizeof(UV), uvcompare); + + /* Create a utf8 string containing the complement of the + * codepoint ranges. For example if cp[] contains [A,B], [C,D], + * then transv will contain the equivalent of: + * join '', map chr, 0, ILLEGAL_UTF8_BYTE, A - 1, + * B + 1, ILLEGAL_UTF8_BYTE, C - 1, + * D + 1, ILLEGAL_UTF8_BYTE, 0x7fffffff; + * A range of a single char skips the ILLEGAL_UTF8_BYTE and + * end cp. + */ for (j = 0; j < i; j++) { UV val = cp[2*j]; diff = val - nextmin; @@ -6420,6 +6468,7 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) if (val >= nextmin) nextmin = val + 1; } + t = uvchr_to_utf8(tmpbuf,nextmin); sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); { @@ -6436,6 +6485,7 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) else if (!rlen && !del) { r = t; rlen = tlen; rend = tend; } + if (!squash) { if ((!rlen && !del) || t == r || (tlen == rlen && memEQ((char *)t, (char *)r, tlen))) @@ -6444,6 +6494,8 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) } } + /* extract char ranges from t and r and append them to listsv */ + while (t < tend || tfirst <= tlast) { /* see if we need more "t" chars */ if (tfirst > tlast) { @@ -6516,9 +6568,11 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) tfirst += diff + 1; } + /* compile listsv into a swash and attach to o */ + none = ++max; if (del) - del = ++max; + ++max; if (max > 0xffff) bits = 32; @@ -6557,50 +6611,88 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) goto warnins; } - tbl = (short*)PerlMemShared_calloc( - (o->op_private & OPpTRANS_COMPLEMENT) && - !(o->op_private & OPpTRANS_DELETE) ? 258 : 256, - sizeof(short)); + /* Non-utf8 case: set o->op_pv to point to a simple 256+ entry lookup + * table. Entries with the value -1 indicate chars not to be + * translated, while -2 indicates a search char without a + * corresponding replacement char under /d. + * + * Normally, the table has 256 slots. However, in the presence of + * /c, the search charlist has an implicit \x{100}-\x{7fffffff} + * added, and if there are enough replacement chars to start pairing + * with the \x{100},... search chars, then a larger (> 256) table + * is allocated. + * + * In addition, regardless of whether under /c, an extra slot at the + * end is used to store the final repeating char, or -3 under an empty + * replacement list, or -2 under /d; which makes the runtime code + * easier. + * + * The toker will have already expanded char ranges in t and r. + */ + + /* Initially allocate 257-slot table: 256 for basic (non /c) usage, + * plus final slot for repeat/-2/-3. Later we realloc if excess > * 0. + * The OPtrans_map struct already contains one slot; hence the -1. + */ + struct_size = sizeof(OPtrans_map) + (256 - 1 + 1)*sizeof(short); + tbl = (OPtrans_map*)PerlMemShared_calloc(struct_size, 1); + tbl->size = 256; cPVOPo->op_pv = (char*)tbl; + if (complement) { - for (i = 0; i < (I32)tlen; i++) - tbl[t[i]] = -1; + Size_t excess; + + /* in this branch, j is a count of 'consumed' (i.e. paired off + * with a search char) replacement chars (so j <= rlen always) + */ + for (i = 0; i < tlen; i++) + tbl->map[t[i]] = -1; + for (i = 0, j = 0; i < 256; i++) { - if (!tbl[i]) { - if (j >= (I32)rlen) { + if (!tbl->map[i]) { + if (j == rlen) { if (del) - tbl[i] = -2; + tbl->map[i] = -2; else if (rlen) - tbl[i] = r[j-1]; + tbl->map[i] = r[j-1]; else - tbl[i] = (short)i; + tbl->map[i] = (short)i; } else { - if (UVCHR_IS_INVARIANT(i) && ! UVCHR_IS_INVARIANT(r[j])) - grows = 1; - tbl[i] = r[j++]; + tbl->map[i] = r[j++]; } + if ( tbl->map[i] >= 0 + && UVCHR_IS_INVARIANT((UV)i) + && !UVCHR_IS_INVARIANT((UV)(tbl->map[i])) + ) + grows = TRUE; } } - if (!del) { - if (!rlen) { - j = rlen; - if (!squash) - o->op_private |= OPpTRANS_IDENTICAL; - } - else if (j >= (I32)rlen) - j = rlen - 1; - else { - tbl = - (short *) - PerlMemShared_realloc(tbl, - (0x101+rlen-j) * sizeof(short)); - cPVOPo->op_pv = (char*)tbl; - } - tbl[0x100] = (short)(rlen - j); - for (i=0; i < (I32)rlen - j; i++) - tbl[0x101+i] = r[j+i]; - } + + ASSUME(j <= rlen); + excess = rlen - j; + + if (excess) { + /* More replacement chars than search chars: + * store excess replacement chars at end of main table. + */ + + struct_size += excess; + tbl = (OPtrans_map*)PerlMemShared_realloc(tbl, + struct_size + excess * sizeof(short)); + tbl->size += excess; + cPVOPo->op_pv = (char*)tbl; + + for (i = 0; i < excess; i++) + tbl->map[i + 256] = r[j+i]; + } + else { + /* no more replacement chars than search chars */ + if (!rlen && !del && !squash) + o->op_private |= OPpTRANS_IDENTICAL; + } + + tbl->map[tbl->size] = del ? -2 : rlen ? r[rlen - 1] : -3; } else { if (!rlen && !del) { @@ -6611,26 +6703,30 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) { o->op_private |= OPpTRANS_IDENTICAL; } + for (i = 0; i < 256; i++) - tbl[i] = -1; - for (i = 0, j = 0; i < (I32)tlen; i++,j++) { - if (j >= (I32)rlen) { + tbl->map[i] = -1; + for (i = 0, j = 0; i < tlen; i++,j++) { + if (j >= rlen) { if (del) { - if (tbl[t[i]] == -1) - tbl[t[i]] = -2; + if (tbl->map[t[i]] == -1) + tbl->map[t[i]] = -2; continue; } --j; } - if (tbl[t[i]] == -1) { + if (tbl->map[t[i]] == -1) { if ( UVCHR_IS_INVARIANT(t[i]) && ! UVCHR_IS_INVARIANT(r[j])) - grows = 1; - tbl[t[i]] = r[j]; + grows = TRUE; + tbl->map[t[i]] = r[j]; } } + tbl->map[tbl->size] = del ? -1 : rlen ? -1 : -3; } + /* both non-utf8 and utf8 code paths end up here */ + warnins: if(del && rlen == tlen) { Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); @@ -6646,6 +6742,7 @@ S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) return o; } + /* =for apidoc Am|OP *|newPMOP|I32 type|I32 flags diff --git a/op.h b/op.h index eb62c946fc..ed4ff9d1a7 100644 --- a/op.h +++ b/op.h @@ -627,6 +627,15 @@ typedef enum { #define ref(o, type) doref(o, type, TRUE) #endif + +/* translation table attached to OP_TRANS/OP_TRANSR ops */ + +typedef struct { + Size_t size; /* number of entries in map[], not including final slot */ + short map[1]; /* Unwarranted chumminess */ +} OPtrans_map; + + /* =head1 Optree Manipulation Functions diff --git a/pod/perlop.pod b/pod/perlop.pod index ceeb97fc47..4b8d7e2d65 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -2489,22 +2489,36 @@ Options: untouched. If the C</c> modifier is specified, the I<SEARCHLIST> character set -is complemented. If the C</d> modifier is specified, any characters +is complemented. So for example these two are equivalent (the exact +maximum number will depend on your platform): + + tr/\x00-\xfd/ABCD/c + tr/\xfe-\x{7fffffff}/ABCD/ + +If the C</d> modifier is specified, any characters specified by I<SEARCHLIST> not found in I<REPLACEMENTLIST> are deleted. (Note that this is slightly more flexible than the behavior of some B<tr> programs, which delete anything they find in the I<SEARCHLIST>, -period.) If the C</s> modifier is specified, sequences of characters -that were transliterated to the same character are squashed down -to a single instance of the character. +period.) + +If the C</s> modifier is specified, runs of the same character in the +result, where each those characters were substituted by the +transliteration, are squashed down to a single instance of the character. If the C</d> modifier is used, the I<REPLACEMENTLIST> is always interpreted exactly as specified. Otherwise, if the I<REPLACEMENTLIST> is shorter than the I<SEARCHLIST>, the final character is replicated till it is long enough. If the I<REPLACEMENTLIST> is empty, the I<SEARCHLIST> is replicated. This latter is useful for counting characters in a class or for -squashing character sequences in a class. +squashing character sequences in a class. For example, each of these pairs +are equivalent: -Examples: + tr/abcd// tr/abcd/abcd/ + tr/abcd/AB/ tr/abcd/ABBB/ + tr/abcd//d s/[abcd]//g + tr/abcd/AB/d (tr/ab/AB/ + s/[cd]//g) - but run together + +Some examples: $ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case ASCII diff --git a/pp.c b/pp.c index 4b1ccbba80..d50ad7ddbf 100644 --- a/pp.c +++ b/pp.c @@ -689,8 +689,8 @@ PP(pp_trans) PUSHs(newsv); } else { - I32 i = do_trans(sv); - mPUSHi(i); + Size_t i = do_trans(sv); + mPUSHi((UV)i); } RETURN; } diff --git a/proto.h b/proto.h index 8e0c669db7..042a4ee084 100644 --- a/proto.h +++ b/proto.h @@ -822,7 +822,7 @@ PERL_CALLCONV Off_t Perl_do_tell(pTHX_ GV* gv) #define PERL_ARGS_ASSERT_DO_TELL \ assert(gv) -PERL_CALLCONV I32 Perl_do_trans(pTHX_ SV* sv); +PERL_CALLCONV Size_t Perl_do_trans(pTHX_ SV* sv); #define PERL_ARGS_ASSERT_DO_TRANS \ assert(sv) PERL_CALLCONV UV Perl_do_vecget(pTHX_ SV* sv, STRLEN offset, int size); @@ -4542,32 +4542,32 @@ STATIC IO * S_openn_setup(pTHX_ GV *gv, char *mode, PerlIO **saveifp, PerlIO **s assert(gv); assert(mode); assert(saveifp); assert(saveofp); assert(savefd); assert(savetype) #endif #if defined(PERL_IN_DOOP_C) -STATIC I32 S_do_trans_complex(pTHX_ SV * const sv) +STATIC Size_t S_do_trans_complex(pTHX_ SV * const sv) __attribute__warn_unused_result__; #define PERL_ARGS_ASSERT_DO_TRANS_COMPLEX \ assert(sv) -STATIC I32 S_do_trans_complex_utf8(pTHX_ SV * const sv) +STATIC Size_t S_do_trans_complex_utf8(pTHX_ SV * const sv) __attribute__warn_unused_result__; #define PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8 \ assert(sv) -STATIC I32 S_do_trans_count(pTHX_ SV * const sv) +STATIC Size_t S_do_trans_count(pTHX_ SV * const sv) __attribute__warn_unused_result__; #define PERL_ARGS_ASSERT_DO_TRANS_COUNT \ assert(sv) -STATIC I32 S_do_trans_count_utf8(pTHX_ SV * const sv) +STATIC Size_t S_do_trans_count_utf8(pTHX_ SV * const sv) __attribute__warn_unused_result__; #define PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8 \ assert(sv) -STATIC I32 S_do_trans_simple(pTHX_ SV * const sv) +STATIC Size_t S_do_trans_simple(pTHX_ SV * const sv) __attribute__warn_unused_result__; #define PERL_ARGS_ASSERT_DO_TRANS_SIMPLE \ assert(sv) -STATIC I32 S_do_trans_simple_utf8(pTHX_ SV * const sv) +STATIC Size_t S_do_trans_simple_utf8(pTHX_ SV * const sv) __attribute__warn_unused_result__; #define PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8 \ assert(sv) diff --git a/regen/op_private b/regen/op_private index eb53edf43a..49cb4bc035 100644 --- a/regen/op_private +++ b/regen/op_private @@ -513,14 +513,15 @@ addbits('sassign', for (qw(trans transr)) { addbits($_, - 0 => qw(OPpTRANS_FROM_UTF <UTF), - 1 => qw(OPpTRANS_TO_UTF >UTF), + 0 => qw(OPpTRANS_FROM_UTF <UTF), # search chars are utf8 + 1 => qw(OPpTRANS_TO_UTF >UTF), # replacement chars are utf8 2 => qw(OPpTRANS_IDENTICAL IDENT), # right side is same as left - 3 => qw(OPpTRANS_SQUASH SQUASH), + 3 => qw(OPpTRANS_SQUASH SQUASH), # /s # 4 is used for OPpTARGET_MY - 5 => qw(OPpTRANS_COMPLEMENT COMPL), - 6 => qw(OPpTRANS_GROWS GROWS), - 7 => qw(OPpTRANS_DELETE DEL), + 5 => qw(OPpTRANS_COMPLEMENT COMPL), # /c + 6 => qw(OPpTRANS_GROWS GROWS), # replacement chars longer than + # src chars + 7 => qw(OPpTRANS_DELETE DEL), # /d ); } diff --git a/t/op/tr.t b/t/op/tr.t index 323a5c3fa7..0f74936fdb 100644 --- a/t/op/tr.t +++ b/t/op/tr.t @@ -13,7 +13,7 @@ BEGIN { use utf8; -plan tests => 216; +plan tests => 300; # Test this first before we extend the stack with other operations. # This caused an asan failure due to a bad write past the end of the stack. @@ -45,6 +45,432 @@ like $@, qr/Invalid range "\\x\{0101\}-\\x\{0100\}" in transliteration operator/, "UTF-8 range with min > max"; + +# Test /c and variants, with all the search and replace chars being +# non-utf8, but with both non-utf8 and utf8 strings. + +{ + my $all255 = join '', map chr, 0..0xff; + my $all255_twice = join '', map chr, map { ($_, $_) } 0..0xff; + my $all255_plus = join '', map chr, 0..0x11f; + my $all255_twice_plus = join '', map chr, map { ($_, $_) } 0..0x11f; + my ($c, $s); + + + # length(replacement) == 0 + # non-utf8 string + + $s = $all255; + $c = $s =~ tr/\x40-\xbf//c; + is $s, $all255, "/c ==0"; + is $c, 0x80, "/c ==0 count"; + + $s = $all255; + $c = $s =~ tr/\x40-\xbf//cd; + is $s, join('', map chr, 0x40..0xbf), "/cd ==0"; + is $c, 0x80, "/cd ==0 count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf//cs; + is $s, join('', map chr, + 0x00..0x3f, + (map { ($_, $_) } 0x40..0xbf), + 0xc0..0xff, + ), + "/cs ==0"; + is $c, 0x100, "/cs ==0 count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf//csd; + is $s, join('', map chr, (map { ($_, $_) } 0x40..0xbf)), "/csd ==0"; + is $c, 0x100, "/csd ==0 count"; + + + # length(search) > length(replacement) + # non-utf8 string + + $s = $all255; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/c; + is $s, join('', map chr, + 0x80..0xbf, + 0x40..0xbf, + 0x00..0x2f, + ((0x2f) x 16), + ), + "/c >"; + is $c, 0x80, "/c > count"; + + $s = $all255; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/cd; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x2f), + "/cd >"; + is $c, 0x80, "/cd > count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + ), + "/cs >"; + is $c, 0x100, "/cs > count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/csd; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + ), + "/csd >"; + is $c, 0x100, "/csd > count"; + + + # length(search) == length(replacement) + # non-utf8 string + + $s = $all255; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/c; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x3f), "/c =="; + is $c, 0x80, "/c == count"; + + $s = $all255; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/cd; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x3f), "/cd =="; + is $c, 0x80, "/cd == count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x3f, + ), + "/cs =="; + is $c, 0x100, "/cs == count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/csd; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x3f, + ), + "/csd =="; + is $c, 0x100, "/csd == count"; + + # length(search) == length(replacement) - 1 + # non-utf8 string + + + $s = $all255; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x30/c; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x2f, 0xf0..0xff), + "/c =-"; + is $c, 0x70, "/c =- count"; + + $s = $all255; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x30/cd; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x2f, 0xf0..0xff), + "/cd =-"; + is $c, 0x70, "/cd =- count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x30/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + (map { ($_, $_) } 0xf0..0xff), + ), + "/cs =-"; + is $c, 0xe0, "/cs =- count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x30/csd; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + (map { ($_, $_) } 0xf0..0xff), + ), + "/csd =-"; + is $c, 0xe0, "/csd =- count"; + + # length(search) < length(replacement) + # non-utf8 string + + $s = $all255; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/c; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x2f, 0xf0..0xff), + "/c <"; + is $c, 0x70, "/c < count"; + + $s = $all255; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/cd; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x2f, 0xf0..0xff), + "/cd <"; + is $c, 0x70, "/cd < count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + (map { ($_, $_) } 0xf0..0xff), + ), + "/cs <"; + is $c, 0xe0, "/cs < count"; + + $s = $all255_twice; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/csd; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + (map { ($_, $_) } 0xf0..0xff), + ), + "/csd <"; + is $c, 0xe0, "/csd < count"; + + + # length(replacement) == 0 + # with some >= 0x100 utf8 chars in the string to be modified + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf//c; + is $s, $all255_plus, "/c ==0U"; + is $c, 0xa0, "/c ==0U count"; + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf//cd; + is $s, join('', map chr, 0x40..0xbf), "/cd ==0U"; + is $c, 0xa0, "/cd ==0U count"; + + $s = $all255_twice_plus; + $c = $s =~ tr/\x40-\xbf//cs; + is $s, join('', map chr, + 0x00..0x3f, + (map { ($_, $_) } 0x40..0xbf), + 0xc0..0x11f, + ), + "/cs ==0U"; + is $c, 0x140, "/cs ==0U count"; + + $s = $all255_twice_plus; + $c = $s =~ tr/\x40-\xbf//csd; + is $s, join('', map chr, (map { ($_, $_) } 0x40..0xbf)), "/csd ==0U"; + is $c, 0x140, "/csd ==0U count"; + + # length(search) > length(replacement) + # with some >= 0x100 utf8 chars in the string to be modified + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/c; + is $s, join('', map chr, + 0x80..0xbf, + 0x40..0xbf, + 0x00..0x2f, + ((0x2f) x 48), + ), + "/c >U"; + is $c, 0xa0, "/c >U count"; + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/cd; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x2f), + "/cd >U"; + is $c, 0xa0, "/cd >U count"; + + $s = $all255_twice_plus . "\x3f\x3f\x{200}\x{300}"; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + 0xbf, + 0x2f, + ), + "/cs >U"; + is $c, 0x144, "/cs >U count"; + + $s = $all255_twice_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x2f/csd; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + ), + "/csd >U"; + is $c, 0x140, "/csd >U count"; + + # length(search) == length(replacement) + # with some >= 0x100 utf8 chars in the string to be modified + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/c; + is $s, join('', map chr, + 0x80..0xbf, + 0x40..0xbf, + 0x00..0x3f, + ((0x3f) x 32), + ), + "/c ==U"; + is $c, 0xa0, "/c ==U count"; + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/cd; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x3f), "/cd ==U"; + is $c, 0xa0, "/cd ==U count"; + + $s = $all255_twice_plus . "\x3f\x3f\x{200}\x{300}"; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x3f, + 0xbf, + 0x3f, + ), + "/cs ==U"; + is $c, 0x144, "/cs ==U count"; + + $s = $all255_twice_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x3f/csd; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x3f, + ), + "/csd ==U"; + is $c, 0x140, "/csd ==U count"; + + + # length(search) == length(replacement) - 1 + # with some >= 0x100 utf8 chars in the string to be modified + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x40/c; + is $s, join('', map chr, + 0x80..0xbf, + 0x40..0xbf, + 0x00..0x40, + ((0x40) x 31), + ), + "/c =-U"; + is $c, 0xa0, "/c =-U count"; + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x40/cd; + is $s, join('', map chr, 0x80..0xbf, 0x40..0xbf, 0x00..0x40), "/cd =-U"; + is $c, 0xa0, "/cd =-U count"; + + $s = $all255_twice_plus . "\x3f\x3f\x{200}\x{300}"; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x40/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x40, + 0xbf, + 0x40, + ), + "/cs =-U"; + is $c, 0x144, "/cs =-U count"; + + $s = $all255_twice_plus; + $c = $s =~ tr/\x40-\xbf/\x80-\xbf\x00-\x40/csd; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x40, + ), + "/csd =-U"; + is $c, 0x140, "/csd =-U count"; + + + + # length(search) < length(replacement), + # with some >= 0x100 utf8 chars in the string to be modified + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/c; + is $s, join('', map chr, + 0x80..0xbf, + 0x40..0xbf, + 0x00..0x2f, + 0xf0..0xff, + 0x30..0x3f, + ((0x3f)x 16), + ), + "/c <U"; + is $c, 0x90, "/c <U count"; + + $s = $all255_plus; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/cd; + is $s, join('', map chr, + 0x80..0xbf, + 0x40..0xbf, + 0x00..0x2f, + 0xf0..0xff, + 0x30..0x3f, + ), + "/cd <U"; + is $c, 0x90, "/cd <U count"; + + $s = $all255_twice_plus . "\x3f\x3f\x{200}\x{300}"; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/cs; + is $s, join('', map chr, + 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + (map { ($_, $_) } 0xf0..0xff), + 0x30..0x3f, + 0xbf, + 0x3f, + ), + "/cs <U"; + is $c, 0x124, "/cs <U count"; + + $s = $all255_twice_plus; + $c = $s =~ tr/\x40-\xbf\xf0-\xff/\x80-\xbf\x00-\x3f/csd; + is $s, join('', map chr, 0x80..0xbf, + (map { ($_, $_) } 0x40..0xbf), + 0x00..0x2f, + (map { ($_, $_) } 0xf0..0xff), + 0x30..0x3f, + ), + "/csd <U"; + is $c, 0x120, "/csd <U count"; +} + +{ + # RT #132608 + # the 'extra length' for tr///c was stored as a short, so if the + # replacement string had more than 0x7fff chars not paired with + # search chars, bad things could happen + + my ($c, $e, $s); + + $s = "\x{9000}\x{9001}\x{9002}"; + $e = "\$c = \$s =~ tr/\\x00-\\xff/" + . ("ABCDEFGHIJKLMNO" x (0xa000 / 15)) + . "/c; 1; "; + eval $e or die $@; + is $s, "IJK", "RT #132608 len=0xa000"; + is $c, 3, "RT #132608 len=0xa000 count"; + + $s = "\x{9003}\x{9004}\x{9005}"; + $e = "\$c = \$s =~ tr/\\x00-\\xff/" + . ("ABCDEFGHIJKLMNO" x (0x12000 / 15)) + . "/c; 1; "; + eval $e or die $@; + is $s, "LMN", "RT #132608 len=0x12000"; + is $c, 3, "RT #132608 len=0x12000 count"; +} + + SKIP: { # Test literal range end point special handling unless ($::IS_EBCDIC) { skip "Valid only for EBCDIC", 24; -- Perl5 Master Repository
