Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Changes In Branch tip-463 Excluding Merge-Ins
This is equivalent to a diff from 1d11dc7a53 to d26f75a105
2017-06-22
| ||
21:46 | Implement TIP #463: Command-Driven Substitutions for regsub check-in: 9aaee27679 user: dkf tags: trunk | |
13:45 | merge trunk check-in: 8d97ed6516 user: dgp tags: tip-445 | |
13:41 | merge trunk check-in: 30c71bc195 user: dgp tags: novem | |
13:26 | merge trunk Closed-Leaf check-in: 0f132b7210 user: dgp tags: unwanted | |
12:25 | merge trunk Closed-Leaf check-in: d26f75a105 user: dgp tags: tip-463 | |
08:59 | merge core-8-6-branch check-in: 1d11dc7a53 user: jan.nijtmans tags: trunk | |
08:59 | Upgrade all internal character tables to Unicode 10 check-in: f78dec2d2f user: jan.nijtmans tags: core-8-6-branch | |
2017-06-18
| ||
18:02 | Factor out chunk of non-obvious code in the guts of [oo::define] into one place. check-in: bbaad94279 user: dkf tags: trunk | |
2017-02-18
| ||
18:38 | Add documentation of [regsub -command]. check-in: ca85c6e424 user: dkf tags: tip-463 | |
Changes to doc/regsub.n.
︙ | ︙ | |||
63 64 65 66 67 68 69 70 71 72 73 74 75 76 | matching range is found and substituted. If \fB\-all\fR is specified, then .QW & and .QW \e\fIn\fR sequences are handled for each substitution using the information from the corresponding match. .TP \fB\-expanded\fR . Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as specifying the \fB(?x)\fR embedded option (see the \fBre_syntax\fR manual page). .TP | > > > > > > > > > > > > > > > > > > > > > > > > > | 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | matching range is found and substituted. If \fB\-all\fR is specified, then .QW & and .QW \e\fIn\fR sequences are handled for each substitution using the information from the corresponding match. .TP \fB\-command\fR .VS 8.7 Changes the handling of the substitution string so that it no longer treats .QW & and .QW \e as special characters, but instead uses them as a non-empty list of words. Each time a substitution is processed, another complete Tcl word is appended to that list for each substitution value (the first such argument represents the overall matched substring, the subsequent arguments will be one per capturing sub-RE, much as are returned from \fBregexp\fR \fB\-inline\fR) and the overall list is then evaluated as a Tcl command call. If the command finishes successfully, the result of command call is substituted into the resulting string. .RS .PP If \fB\-all\fR is not also given, the command callback will be invoked at most once (exactly when the regular expression matches). If \fB\-all\fR is given, the command callback will be invoked for each matched location, in sequence. The exact location indices that matched are not made available to the script. .PP See \fBEXAMPLES\fR below for illustrative cases. .RE .VE 8.7 .TP \fB\-expanded\fR . Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as specifying the \fB(?x)\fR embedded option (see the \fBre_syntax\fR manual page). .TP |
︙ | ︙ | |||
179 180 181 182 183 184 185 186 187 188 189 190 191 192 | # Now we apply the substitution to get a subst-string that # will perform the computational parts of the conversion. Note # that newline is handled specially through \fBstring map\fR since # backslash-newline is a special sequence. set quoted [subst [string map {\en {\e\eu000a}} \e [\fBregsub\fR -all $RE $string $substitution]]] .CE .SH "SEE ALSO" regexp(n), re_syntax(n), subst(n), string(n) .SH KEYWORDS match, pattern, quoting, regular expression, substitution '\" Local Variables: '\" mode: nroff '\" End: | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | # Now we apply the substitution to get a subst-string that # will perform the computational parts of the conversion. Note # that newline is handled specially through \fBstring map\fR since # backslash-newline is a special sequence. set quoted [subst [string map {\en {\e\eu000a}} \e [\fBregsub\fR -all $RE $string $substitution]]] .CE .PP .VS 8.7 The above operation can be done using \fBregsub \-command\fR instead, which is often faster. (A full pre-computed \fBstring map\fR would be faster still, but the cost of computing the map for a transformation as complex as this can be quite large.) .PP .CS # This RE is just a character class for everything "bad" set RE {[][{};#\e\e\e$\es\eu0080-\euffff]} # This encodes what the RE described above matches proc encodeChar {ch} { # newline is handled specially since backslash-newline is a # special sequence. if {$ch eq "\en"} { return "\e\eu000a" } # No point in writing this as a one-liner scan $ch %c charNumber format "\e\eu%04x" $charNumber } set quoted [\fBregsub\fR -all -command $RE $string encodeChar] .CE .PP Decoding a URL-encoded string using \fBregsub \-command\fR, a lambda term and the \fBapply\fR command. .PP .CS # Match one of the sequences in a URL-encoded string that needs # fixing, converting + to space and %XX to the right character # (e.g., %7e becomes ~) set RE {(\e+)|%([0-9A-Fa-f]{2})} # Note that -command uses a command prefix, not a command name set decoded [\fBregsub\fR -all -command $RE $string {apply {{- p h} { # + is a special case; handle directly if {$p eq "+"} { return " " } # convert hex to a char scan $h %x charNumber format %c $charNumber }}}] .CE .VE 8.7 .SH "SEE ALSO" regexp(n), re_syntax(n), subst(n), string(n) .SH KEYWORDS match, pattern, quoting, regular expression, substitution '\" Local Variables: '\" mode: nroff '\" End: |
Changes to generic/tclCmdMZ.c.
︙ | ︙ | |||
483 484 485 486 487 488 489 | Tcl_RegsubObjCmd( ClientData dummy, /* Not used. */ Tcl_Interp *interp, /* Current interpreter. */ int objc, /* Number of arguments. */ Tcl_Obj *const objv[]) /* Argument objects. */ { int idx, result, cflags, all, wlen, wsublen, numMatches, offset; | | > | | | | > > > > | 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | Tcl_RegsubObjCmd( ClientData dummy, /* Not used. */ Tcl_Interp *interp, /* Current interpreter. */ int objc, /* Number of arguments. */ Tcl_Obj *const objv[]) /* Argument objects. */ { int idx, result, cflags, all, wlen, wsublen, numMatches, offset; int start, end, subStart, subEnd, match, command, numParts, numArgs; Tcl_RegExp regExpr; Tcl_RegExpInfo info; Tcl_Obj *resultPtr, *subPtr, *objPtr, *startIndex = NULL; Tcl_Obj **args = NULL, **parts; Tcl_UniChar ch, *wsrc, *wfirstChar, *wstring, *wsubspec, *wend; static const char *const options[] = { "-all", "-command", "-expanded", "-line", "-linestop", "-lineanchor", "-nocase", "-start", "--", NULL }; enum options { REGSUB_ALL, REGSUB_COMMAND, REGSUB_EXPANDED, REGSUB_LINE, REGSUB_LINESTOP, REGSUB_LINEANCHOR, REGSUB_NOCASE, REGSUB_START, REGSUB_LAST }; cflags = TCL_REG_ADVANCED; all = 0; offset = 0; command = 0; resultPtr = NULL; for (idx = 1; idx < objc; idx++) { const char *name; int index; name = TclGetString(objv[idx]); if (name[0] != '-') { break; } if (Tcl_GetIndexFromObj(interp, objv[idx], options, "option", TCL_EXACT, &index) != TCL_OK) { goto optionError; } switch ((enum options) index) { case REGSUB_ALL: all = 1; break; case REGSUB_NOCASE: cflags |= TCL_REG_NOCASE; break; case REGSUB_COMMAND: command = 1; break; case REGSUB_EXPANDED: cflags |= TCL_REG_EXPANDED; break; case REGSUB_LINE: cflags |= TCL_REG_NEWLINE; break; |
︙ | ︙ | |||
581 582 583 584 585 586 587 | TclGetIntForIndexM(NULL, startIndex, stringLength, &offset); Tcl_DecrRefCount(startIndex); if (offset < 0) { offset = 0; } } | | | 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | TclGetIntForIndexM(NULL, startIndex, stringLength, &offset); Tcl_DecrRefCount(startIndex); if (offset < 0) { offset = 0; } } if (all && (offset == 0) && (command == 0) && (strpbrk(TclGetString(objv[2]), "&\\") == NULL) && (strpbrk(TclGetString(objv[0]), "*+?{}()[].\\|^$") == NULL)) { /* * This is a simple one pair string map situation. We make use of a * slightly modified version of the one pair STR_MAP code. */ |
︙ | ︙ | |||
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | goto regsubDone; } regExpr = Tcl_GetRegExpFromObj(interp, objv[0], cflags); if (regExpr == NULL) { return TCL_ERROR; } /* * Make sure to avoid problems where the objects are shared. This can * cause RegExpObj <> UnicodeObj shimmering that causes data corruption. * [Bug #461322] */ if (objv[1] == objv[0]) { objPtr = Tcl_DuplicateObj(objv[1]); } else { objPtr = objv[1]; } wstring = Tcl_GetUnicodeFromObj(objPtr, &wlen); if (objv[2] == objv[0]) { subPtr = Tcl_DuplicateObj(objv[2]); } else { subPtr = objv[2]; } | > > > > > > > > > > > > > > > > > > > > > > > | > | 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | goto regsubDone; } regExpr = Tcl_GetRegExpFromObj(interp, objv[0], cflags); if (regExpr == NULL) { return TCL_ERROR; } if (command) { /* * In command-prefix mode, we require that the third non-option * argument be a list, so we enforce that here. Afterwards, we fetch * the RE compilation again in case objv[0] and objv[2] are the same * object. (If they aren't, that's cheap to do.) */ if (Tcl_ListObjLength(interp, objv[2], &numParts) != TCL_OK) { return TCL_ERROR; } if (numParts < 1) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "command prefix must be a list of at least one element", -1)); Tcl_SetErrorCode(interp, "TCL", "OPERATION", "REGSUB", "CMDEMPTY", NULL); return TCL_ERROR; } regExpr = Tcl_GetRegExpFromObj(interp, objv[0], cflags); } /* * Make sure to avoid problems where the objects are shared. This can * cause RegExpObj <> UnicodeObj shimmering that causes data corruption. * [Bug #461322] */ if (objv[1] == objv[0]) { objPtr = Tcl_DuplicateObj(objv[1]); } else { objPtr = objv[1]; } wstring = Tcl_GetUnicodeFromObj(objPtr, &wlen); if (objv[2] == objv[0]) { subPtr = Tcl_DuplicateObj(objv[2]); } else { subPtr = objv[2]; } if (!command) { wsubspec = Tcl_GetUnicodeFromObj(subPtr, &wsublen); } result = TCL_OK; /* * The following loop is to handle multiple matches within the same source * string; each iteration handles one match and its corresponding * substitution. If "-all" hasn't been specified then the loop body only |
︙ | ︙ | |||
731 732 733 734 735 736 737 738 739 740 741 742 743 744 | * result variable. */ Tcl_RegExpGetInfo(regExpr, &info); start = info.matches[0].start; end = info.matches[0].end; Tcl_AppendUnicodeToObj(resultPtr, wstring + offset, start); /* * Append the subSpec argument to the variable, making appropriate * substitutions. This code is a bit hairy because of the backslash * conventions and because the code saves up ranges of characters in * subSpec to reduce the number of calls to Tcl_SetVar. */ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 | * result variable. */ Tcl_RegExpGetInfo(regExpr, &info); start = info.matches[0].start; end = info.matches[0].end; Tcl_AppendUnicodeToObj(resultPtr, wstring + offset, start); /* * In command-prefix mode, the substitutions are added as quoted * arguments to the subSpec to form a command, that is then executed * and the result used as the string to substitute in. Actually, * everything is passed through Tcl_EvalObjv, as that's much faster. */ if (command) { if (args == NULL) { Tcl_ListObjGetElements(interp, subPtr, &numParts, &parts); numArgs = numParts + info.nsubs + 1; args = ckalloc(sizeof(Tcl_Obj*) * numArgs); memcpy(args, parts, sizeof(Tcl_Obj*) * numParts); } for (idx = 0 ; idx <= info.nsubs ; idx++) { subStart = info.matches[idx].start; subEnd = info.matches[idx].end; if ((subStart >= 0) && (subEnd >= 0)) { args[idx + numParts] = Tcl_NewUnicodeObj( wstring + offset + subStart, subEnd - subStart); } else { args[idx + numParts] = Tcl_NewObj(); } Tcl_IncrRefCount(args[idx + numParts]); } /* * At this point, we're locally holding the references to the * argument words we added for this time round the loop, and the * subPtr is holding the references to the words that the user * supplied directly. None are zero-refcount, which is important * because Tcl_EvalObjv is "hairy monster" in terms of refcount * handling, being able to optionally add references to any of its * argument words. We'll drop the local refs immediately * afterwarsds; subPtr is handled in the main exit stanza. */ result = Tcl_EvalObjv(interp, numArgs, args, 0); for (idx = 0 ; idx <= info.nsubs ; idx++) { TclDecrRefCount(args[idx + numParts]); } if (result != TCL_OK) { if (result == TCL_ERROR) { Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf( "\n (%s substitution computation script)", options[REGSUB_COMMAND])); } goto done; } Tcl_AppendObjToObj(resultPtr, Tcl_GetObjResult(interp)); Tcl_ResetResult(interp); /* * Refetch the unicode, in case the representation was smashed by * the user code. */ wstring = Tcl_GetUnicodeFromObj(objPtr, &wlen); offset += end; if (end == 0 || start == end) { /* * Always consume at least one character of the input string * in order to prevent infinite loops, even when we * technically matched the empty string; we must not match * again at the same spot. */ if (offset < wlen) { Tcl_AppendUnicodeToObj(resultPtr, wstring + offset, 1); } offset++; } if (all) { continue; } else { break; } } /* * Append the subSpec argument to the variable, making appropriate * substitutions. This code is a bit hairy because of the backslash * conventions and because the code saves up ranges of characters in * subSpec to reduce the number of calls to Tcl_SetVar. */ |
︙ | ︙ | |||
859 860 861 862 863 864 865 866 867 868 869 870 871 872 | done: if (objPtr && (objv[1] == objv[0])) { Tcl_DecrRefCount(objPtr); } if (subPtr && (objv[2] == objv[0])) { Tcl_DecrRefCount(subPtr); } if (resultPtr) { Tcl_DecrRefCount(resultPtr); } return result; } | > > > | 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 | done: if (objPtr && (objv[1] == objv[0])) { Tcl_DecrRefCount(objPtr); } if (subPtr && (objv[2] == objv[0])) { Tcl_DecrRefCount(subPtr); } if (args) { ckfree(args); } if (resultPtr) { Tcl_DecrRefCount(resultPtr); } return result; } |
︙ | ︙ |
Changes to tests/regexp.test.
︙ | ︙ | |||
15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package require tcltest 2 namespace import -force ::tcltest::* } unset -nocomplain foo testConstraint exec [llength [info commands exec]] test regexp-1.1 {basic regexp operation} { regexp ab*c abbbc } 1 test regexp-1.2 {basic regexp operation} { regexp ab*c ac } 1 | > > > > > > > > > > > > > > | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package require tcltest 2 namespace import -force ::tcltest::* } unset -nocomplain foo testConstraint exec [llength [info commands exec]] # Used for constraining memory leak tests testConstraint memory [llength [info commands memory]] if {[testConstraint memory]} { proc memtest script { set end [lindex [split [memory info] \n] 3 3] for {set i 0} {$i < 5} {incr i} { uplevel 1 $script set tmp $end set end [lindex [split [memory info] \n] 3 3] } expr {$end - $tmp} } } test regexp-1.1 {basic regexp operation} { regexp ab*c abbbc } 1 test regexp-1.2 {basic regexp operation} { regexp ab*c ac } 1 |
︙ | ︙ | |||
449 450 451 452 453 454 455 | list [catch {regsub -nocase -all a b} msg] $msg } {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}} test regexp-11.4 {regsub errors} { list [catch {regsub a b c d e f} msg] $msg } {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}} test regexp-11.5 {regsub errors} { list [catch {regsub -gorp a b c} msg] $msg | | | 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | list [catch {regsub -nocase -all a b} msg] $msg } {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}} test regexp-11.4 {regsub errors} { list [catch {regsub a b c d e f} msg] $msg } {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}} test regexp-11.5 {regsub errors} { list [catch {regsub -gorp a b c} msg] $msg } {1 {bad option "-gorp": must be -all, -command, -expanded, -line, -linestop, -lineanchor, -nocase, -start, or --}} test regexp-11.6 {regsub errors} { list [catch {regsub -nocase a( b c d} msg] $msg } {1 {couldn't compile regular expression pattern: parentheses () not balanced}} test regexp-11.7 {regsub errors} -setup { unset -nocomplain f1 } -body { set f1 44 |
︙ | ︙ | |||
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 | } {a {}} test regexp-26.12 {regexp with -line option} { regexp -all -inline -line -- {a*} "b\n" } {{} {}} test regexp-26.13 {regexp without -line option} { regexp -all -inline -- {a*} "b\n" } {{} {}} # cleanup ::tcltest::cleanupTests return # Local Variables: # mode: tcl # End: | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 | } {a {}} test regexp-26.12 {regexp with -line option} { regexp -all -inline -line -- {a*} "b\n" } {{} {}} test regexp-26.13 {regexp without -line option} { regexp -all -inline -- {a*} "b\n" } {{} {}} test regexp-27.1 {regsub -command} { regsub -command {.x.} {abcxdef} {string length} } ab3ef test regexp-27.2 {regsub -command} { regsub -command {.x.} {abcxdefxghi} {string length} } ab3efxghi test regexp-27.3 {regsub -command} { set x 0 regsub -all -command {(?=.)} abcde {apply {args {incr ::x}}} } 1a2b3c4d5e test regexp-27.4 {regsub -command} -body { regsub -command {.x.} {abcxdef} error } -returnCodes error -result cxd test regexp-27.5 {regsub -command} { regsub -command {(.)(.)} {abcdef} {list ,} } {, ab a bcdef} test regexp-27.6 {regsub -command} { regsub -command -all {(.)(.)} {abcdef} {list ,} } {, ab a b, cd c d, ef e f} test regexp-27.7 {regsub -command representation smash} { set ::s {123=456 789} regsub -command -all {\d+} $::s {apply {n { expr {[llength $::s] + $n} }}} } {125=458 791} test regexp-27.8 {regsub -command representation smash} { set ::t {apply {n { expr {[llength [lindex $::t 1 1 1]] + $n} }}} regsub -command -all {\d+} "123=456 789" $::t } {131=464 797} test regexp-27.9 {regsub -command memory leak testing} memory { set ::s "123=456 789" set ::t {apply {n { expr {[llength [lindex $::t 1 1 1]] + [llength $::s] + $n} }}} memtest { regsub -command -all {\d+} $::s $::t } } 0 test regexp-27.10 {regsub -command error cases} -returnCodes error -body { regsub -command . abc "def \{ghi" } -result {unmatched open brace in list} test regexp-27.11 {regsub -command error cases} -returnCodes error -body { regsub -command . abc {} } -result {command prefix must be a list of at least one element} # cleanup ::tcltest::cleanupTests return # Local Variables: # mode: tcl # End: |
Changes to tests/regexpComp.test.
︙ | ︙ | |||
583 584 585 586 587 588 589 | list [catch {regsub a b c d e f} msg] $msg } } {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}} test regexpComp-11.5 {regsub errors} { evalInProc { list [catch {regsub -gorp a b c} msg] $msg } | | | 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | list [catch {regsub a b c d e f} msg] $msg } } {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}} test regexpComp-11.5 {regsub errors} { evalInProc { list [catch {regsub -gorp a b c} msg] $msg } } {1 {bad option "-gorp": must be -all, -command, -expanded, -line, -linestop, -lineanchor, -nocase, -start, or --}} test regexpComp-11.6 {regsub errors} { evalInProc { list [catch {regsub -nocase a( b c d} msg] $msg } } {1 {couldn't compile regular expression pattern: parentheses () not balanced}} test regexpComp-11.7 {regsub errors} { evalInProc { |
︙ | ︙ |