Tcl Source Code

Artifact [010627fd57]
Login

Artifact 010627fd571eb3cf6be0522ecc11cc1fde35ca1f:

Attachment "regsubfix.patch" to ticket [755335ffff] added by vincentdarley 2003-06-17 16:39:44.
Index: tclCmdMZ.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclCmdMZ.c,v
retrieving revision 1.90
diff -b -u -r1.90 tclCmdMZ.c
--- tclCmdMZ.c	10 May 2003 23:54:37 -0000	1.90
+++ tclCmdMZ.c	17 Jun 2003 09:36:02 -0000
@@ -368,7 +368,10 @@
 
     while (1) {
 	match = Tcl_RegExpExecObj(interp, regExpr, objPtr,
-		offset /* offset */, numMatchesSaved, eflags);
+		offset /* offset */, numMatchesSaved, eflags 
+		| ((offset > 0 &&
+		   (Tcl_GetUniChar(objPtr,offset-1) != (Tcl_UniChar)'\n'))
+		   ? TCL_REG_NOTBOL : 0));
 
 	if (match < 0) {
 	    return TCL_ERROR;
@@ -719,11 +722,14 @@
      * 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 gets executed once.
+     * then the loop body only gets executed once.  We must use
+     * 'offset <= wlen' in particular for the case where the regexp
+     * pattern can match the empty string - this is useful when
+     * doing, say, 'regsub -- ^ $str ...' when $str might be empty.
      */
 
     numMatches = 0;
-    for ( ; offset < wlen; ) {
+    for ( ; offset <= wlen; ) {
 
 	/*
 	 * The flags argument is set if string is part of a larger string,
@@ -731,7 +737,9 @@
 	 */
 
 	match = Tcl_RegExpExecObj(interp, regExpr, objPtr, offset,
-		10 /* matches */, ((offset > 0) ? TCL_REG_NOTBOL : 0));
+		10 /* matches */, ((offset > 0 &&
+		   (Tcl_GetUniChar(objPtr,offset-1) != (Tcl_UniChar)'\n'))
+		   ? TCL_REG_NOTBOL : 0));
 
 	if (match < 0) {
 	    result = TCL_ERROR;