Hi

name=* & highway=* {set name='${name|subst:^(Doctor|Dokter) ~>Dr }'}

The attached patch makes this possible, and the pre-built jar here:
  http://files.mkgmap.org.uk/download/212/mkgmap.jar

The line must be written with a quoted argument:

   name=* & highway=* {set name='${name|subst:"^(Doctor|Dokter) ~>Dr "}'}

You must use the opposite kind of quote to the surrounding quotes.

I will tidy the patch up later.

..Steve
Index: test/resources/rules/qvar.test
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- test/resources/rules/qvar.test	(revision )
+++ test/resources/rules/qvar.test	(revision )
@@ -0,0 +1,14 @@
+#
+# Test variable substitution with filters that have quoted arguments.
+#
+
+WAY
+highway=primary
+name=Doctor Who
+
+<<<lines>>>
+
+highway=primary {name '${name|subst:"^(Doctor|Dokter) ~>Dr "}' } [0x1]
+
+<<<results>>>
+WAY 1: Line 0x1, labels=[Dr Who, null, null, null], res=24-24 (1/1),(2/2),
Index: src/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilder.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilder.java	(revision 3284)
+++ src/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilder.java	(revision )
@@ -146,45 +146,86 @@
 			items.add(new ValueItem(text.toString()));
 	}
 
+	private final Pattern[] PATS = {
+			Pattern.compile("[ \t]*([^: \\t|]+:\"[^\"]+\")[ \t]*"),
+			Pattern.compile("[ \t]*([^: \\t|]+:'[^']+')[ \t]*"),
+
+			// This must be last
+			Pattern.compile("([ \t]*[^: \\t|]+:[^|]+)"),
+	};
 	private void addTagValue(String tagname, boolean is_local) {
 		ValueItem item = new ValueItem();
 		if (tagname.contains("|")) {
-			String[] parts = tagname.split("\\|");
+			String[] parts = tagname.split("[ \t]*\\|", 2);
 			assert parts.length > 1;
+
 			item.setTagname(parts[0], is_local);
-			for (int i = 1; i < parts.length; i++)
-				addFilter(item, parts[i]);
+
+			String s = parts[1];
+
+			int start = 0;
+			int end = s.length();
+			while (start < end) {
+				Matcher matcher = null;
+				for (Pattern p : PATS) {
+					matcher = p.matcher(s);
+					matcher.region(start, end);
+					if (matcher.lookingAt())
+						break;
+				}
+
+				if (matcher != null && matcher.lookingAt()) {
+					start = matcher.end() + 1;
+					//System.out.printf("start to %d, end at %d\n", start, end);
+					//System.out.printf("adding>%s<\n", matcher.group(1));
+					addFilter(item, matcher.group(1));
-		} else {
+				} else {
+					//System.out.println("nomatch: " + s.substring(start));
+					start = end;
+				}
+			}
+		} else {
 			item.setTagname(tagname, is_local);
 		}
 		items.add(item);
 	}
 
 	private void addFilter(ValueItem item, String expr) {
-		Pattern pattern = Pattern.compile("([^:]+):(.*)");
+		Pattern pattern = Pattern.compile("([^:]+):[\"']?(.*?)[\"']?");
 		//pattern.
 		Matcher matcher = pattern.matcher(expr);
-		matcher.find();
+		matcher.matches();
 		String cmd = matcher.group(1);
 		String arg = matcher.group(2);
-		if (cmd.equals("def")) {
+
+		switch (cmd) {
+		case "def":
 			item.addFilter(new DefaultFilter(arg));
-		} else if (cmd.equals("conv")) {
+			break;
+		case "conv":
 			item.addFilter(new ConvertFilter(arg));
-		} else if (cmd.equals("subst")) {
+			break;
+		case "subst":
 			item.addFilter(new SubstitutionFilter(arg));
-		} else if (cmd.equals("prefix")) {
+			break;
+		case "prefix":
 			item.addFilter(new PrependFilter(arg));
-		} else if (cmd.equals("highway-symbol")) {
+			break;
+		case "highway-symbol":
 			item.addFilter(new HighwaySymbolFilter(arg));
-		} else if (cmd.equals("height")) {
+			break;
+		case "height":
 			item.addFilter(new HeightFilter(arg));
-		} else if (cmd.equals("not-equal")) {
+			break;
+		case "not-equal":
 			item.addFilter(new NotEqualFilter(arg));
-		} else if (cmd.equals("substring")) {
+			break;
+		case "substring":
 			item.addFilter(new SubstringFilter(arg));
-		}  else if (cmd.equals("part")) {
+			break;
+		case "part":
 			item.addFilter(new PartFilter(arg));
+			break;
 		}
 	}
 
Index: test/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilderTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- test/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilderTest.java	(revision 3285)
+++ test/uk/me/parabola/mkgmap/osmstyle/actions/ValueBuilderTest.java	(revision )
@@ -28,7 +28,17 @@
  * Test substitutions when building values with ValueBuilder.
  */
 public class ValueBuilderTest {
+	@Test
+	public void testVariable() {
+		ValueBuilder vb = new ValueBuilder("${name} road");
 
+		Element el = new Way(1);
+		el.addTag("name", "abc abc");
+
+		String s = vb.build(el, null);
+		assertEquals("abc abc road", s);
+	}
+
 	@Test
 	public void testSimpleSubst() {
 		ValueBuilder vb = new ValueBuilder("init ${name|subst:abc=>xyz} final");
@@ -65,6 +75,70 @@
 
 		String s = vb.build(el, null);
 		assertEquals("Tx y z !", s);
+	}
+
+	@Test
+	public void testQuotedArg() {
+		ValueBuilder vb = new ValueBuilder("${name|subst:'abc=>x y z '}!");
+
+		Element el = new Way(1);
+		el.addTag("name", "Tabc");
+
+		String s = vb.build(el, null);
+		assertEquals("Tx y z !", s);
+	}
+
+	@Test
+	public void testDQuotedArg() {
+		ValueBuilder vb = new ValueBuilder("${name|subst:\"abc=>x y z \"}!");
+
+		Element el = new Way(1);
+		el.addTag("name", "Tabc");
+
+		String s = vb.build(el, null);
+		assertEquals("Tx y z !", s);
+	}
+
+	@Test
+	public void testQuotedArgs() {
+		ValueBuilder vb = new ValueBuilder("${name|subst:'abc=>x|y'|subst:'defg=>w|w\"w'|def:'unset string' }");
+
+		Element el = new Way(1);
+
+		// No tags set, so default value will be applied.
+		String s = vb.build(el, null);
+		assertEquals("name not set, so default is applied", "unset string", s);
+
+		// Name tag is set, so substitutions are made
+		el.addTag("name", "abc defg");
+		s = vb.build(el, null);
+		assertEquals("substitutions in name", "x|y w|w\"w", s);
+	}
+
+	@Test
+	public void testSpacedQuotedArgs() {
+		ValueBuilder vb = new ValueBuilder("${name | subst:'abc=>x|y' | subst:'defg=>w|w' | def:'unset string' }");
+		Element el = new Way(1);
+
+		// No tags set, so default value will be applied.
+		String s = vb.build(el, null);
+		assertEquals("name not set, so default is applied", "unset string", s);
+
+		// Name tag is set, so substitutions are made
+		el.addTag("name", "abc defg");
+		s = vb.build(el, null);
+		assertEquals("substitutions in name", "x|y w|w", s);
+	}
+
+	@Test
+	public void testExample() {
+		ValueBuilder vb = new ValueBuilder("${name|subst:'^(Doctor|Dokter) ~>Dr '}");
+
+		Element el = new Way(1);
+		el.addTag("name", "Doctor Who");
+
+		String s = vb.build(el, null);
+		assertEquals("Dr Who", s);
 	}
 
 	@Test
_______________________________________________
mkgmap-dev mailing list
mkgmap-dev@lists.mkgmap.org.uk
http://www.mkgmap.org.uk/mailman/listinfo/mkgmap-dev

Reply via email to