Bram,

The other day in #vim, someone was wondering why :s/SOMEWORD/\L\u&/
was resulting in sOMEWORD instead of title-case (Someword).

That seemed like a reasonable expectation and is how PCRE would treat
that substitution, so I came up with the attached patch to make Vim work
like that.  Also included are tests.

Cheers,
-- 
James
GPG Key: 4096R/331BA3DB 2011-12-05 James McCoy <[email protected]>
commit d78b9bcff1de7f97d4fd8f233ddefc13688acebd
Author: James McCoy <[email protected]>
Date:   Sat Jan 19 23:41:17 2013 -0500

    \L and \U now persist until a \e or \E, despite an intervening \u or \l
    
    The existing behavior would cause s/fOo/\L\u&/ to change fOo to FOo,
    instead of the expected Foo.  The \l immediately overrode the effect of
    the \U.
    
    The one-off (\u/\l) and persistent (\U/\L) flags are now tracked
    distinctly so the persistent ones are only permanently disabled by
    another persistent flag or an \E.

diff --git a/src/regexp.c b/src/regexp.c
index d85ded8..9b496af 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -7178,7 +7178,7 @@ vim_regsub_both(source, dest, copy, magic, backslash)
     int		c;
     int		cc;
     int		no = -1;
-    fptr_T	func = (fptr_T)NULL;
+    fptr_T	func_all = (fptr_T)NULL, func_one = (fptr_T)NULL;
     linenr_T	clnum = 0;	/* init for GCC */
     int		len = 0;	/* init for GCC */
 #ifdef FEAT_EVAL
@@ -7311,16 +7311,16 @@ vim_regsub_both(source, dest, copy, magic, backslash)
 	    {
 		switch (*src++)
 		{
-		case 'u':   func = (fptr_T)do_upper;
+		case 'u':   func_one = (fptr_T)do_upper;
 			    continue;
-		case 'U':   func = (fptr_T)do_Upper;
+		case 'U':   func_all = (fptr_T)do_Upper;
 			    continue;
-		case 'l':   func = (fptr_T)do_lower;
+		case 'l':   func_one = (fptr_T)do_lower;
 			    continue;
-		case 'L':   func = (fptr_T)do_Lower;
+		case 'L':   func_all = (fptr_T)do_Lower;
 			    continue;
 		case 'e':
-		case 'E':   func = (fptr_T)NULL;
+		case 'E':   func_one = func_all = (fptr_T)NULL;
 			    continue;
 		}
 	    }
@@ -7373,11 +7373,14 @@ vim_regsub_both(source, dest, copy, magic, backslash)
 #endif
 
 	    /* Write to buffer, if copy is set. */
-	    if (func == (fptr_T)NULL)	/* just copy */
-		cc = c;
-	    else
+	    if (func_one != (fptr_T)NULL)
 		/* Turbo C complains without the typecast */
-		func = (fptr_T)(func(&cc, c));
+		func_one = (fptr_T)(func_one(&cc, c));
+	    else if (func_all != (fptr_T)NULL)
+		/* Turbo C complains without the typecast */
+		func_all = (fptr_T)(func_all(&cc, c));
+	    else /* just copy */
+		cc = c;
 
 #ifdef FEAT_MBYTE
 	    if (has_mbyte)
@@ -7488,11 +7491,14 @@ vim_regsub_both(source, dest, copy, magic, backslash)
 #endif
 				c = *s;
 
-			    if (func == (fptr_T)NULL)	/* just copy */
-				cc = c;
-			    else
+			    if (func_one != (fptr_T)NULL)
 				/* Turbo C complains without the typecast */
-				func = (fptr_T)(func(&cc, c));
+				func_one = (fptr_T)(func_one(&cc, c));
+			    else if (func_all != (fptr_T)NULL)
+				/* Turbo C complains without the typecast */
+				func_all = (fptr_T)(func_all(&cc, c));
+			    else /* just copy */
+				cc = c;
 
 #ifdef FEAT_MBYTE
 			    if (has_mbyte)
diff --git a/src/testdir/test79.in b/src/testdir/test79.in
index f83b6b6..c50b92f 100644
--- a/src/testdir/test79.in
+++ b/src/testdir/test79.in
@@ -32,6 +32,8 @@ j:s/Q/\t/
 j:s/R/\\/
 j:s/S/\c/
 j:s/T//
+j:s/U/\L\uuUu\l\EU/
+j:s/V/\U\lVvV\u\Ev/
 ENDTEST
 
 TEST_1:
@@ -55,6 +57,8 @@ qQq
 rRr
 sSs
 tTt
+U
+V
 
 STARTTEST
 :set nomagic
@@ -80,6 +84,8 @@ j:s/Q/\t/
 j:s/R/\\/
 j:s/S/\c/
 j:s/T//
+j:s/U/\L\uuUu\l\EU/
+j:s/V/\U\lVvV\u\Ev/
 ENDTEST
 
 TEST_2:
@@ -103,6 +109,8 @@ qQq
 rRr
 sSs
 tTt
+U
+V
 
 STARTTEST
 :set magic&
diff --git a/src/testdir/test79.ok b/src/testdir/test79.ok
index 31ad3a4..bb30d14 100644
--- a/src/testdir/test79.ok
+++ b/src/testdir/test79.ok
@@ -24,6 +24,8 @@ q	q
 r\r
 scs
 tt
+UuuU
+vVVv
 
 
 TEST_2:
@@ -49,6 +51,8 @@ q	q
 r\r
 scs
 tt
+UuuU
+vVVv
 
 
 TEST_3:
diff --git a/src/testdir/test80.in b/src/testdir/test80.in
index df4afbb..7f6ecfc 100644
--- a/src/testdir/test80.in
+++ b/src/testdir/test80.in
@@ -35,6 +35,8 @@ STARTTEST
 :$put =substitute('vVv', 'V', \"\b\", '')
 :$put =substitute('wWw', 'W', \"\\\", '')
 :$put =substitute('xXx', 'X', \"\r\", '')
+:$put =substitute('Y', 'Y', '\L\uyYy\l\EY', '')
+:$put =substitute('Z', 'Z', '\U\lZzZ\u\Ez', '')
 /^TEST_2
 ENDTEST
 
@@ -67,6 +69,8 @@ STARTTEST
 :$put =substitute('uUu', 'U', \"\n\", '')
 :$put =substitute('vVv', 'V', \"\b\", '')
 :$put =substitute('wWw', 'W', \"\\\", '')
+:$put =substitute('X', 'X', '\L\uxXx\l\EX', '')
+:$put =substitute('Y', 'Y', '\U\lYyY\u\Ey', '')
 /^TEST_3
 ENDTEST
 
diff --git a/src/testdir/test80.ok b/src/testdir/test80.ok
index b08d303..45b1d1d 100644
--- a/src/testdir/test80.ok
+++ b/src/testdir/test80.ok
@@ -27,6 +27,8 @@ u
 vv
 w\w
 x
x
+YyyY
+zZZz
 
 
 TEST_2:
@@ -55,6 +57,8 @@ u
 u
 vv
 w\w
+XxxX
+yYYy
 
 
 TEST_3:

Attachment: signature.asc
Description: Digital signature

Raspunde prin e-mail lui