logo separator

[mkgmap-dev] patch for "part" filter

From Andrzej Popowski popej at poczta.onet.pl on Wed Mar 30 19:31:26 BST 2016

Hi,

I have tried to use substitution filter "part" and have found, that it 
worked differently, than I expected. Some operation results in adding 
trailing separator while others don't. Some filters return null, others 
return "", which breaks evaluating formulas with "|".

I have attached a patch, that should change behavior of following filters:
"A1;A2;A3" "part:;<3"  -> old "A3;", patched "A3"
"A1;A2"    "part:;<3"  -> old null,  patched "A1;A2"
"A1"       "part:;>-2" -> old null,  patched "A1"
"A1;A2;A3" "part:;>3"  -> old "",    patched null
"A1;A2"    "part:;<-2" -> old "",    patched null

I have attached example data and style for testing the filter.

I'm not sure, what would be preferred result for following filters:
"A1"  "part:;<3"  -> result null or "A1"?
"A1"  "part:;>-3" -> result null or "A1"?

Both, current version and patched, return null. I think result "A1" 
could be useful.

-- 
Best regards,
Andrzej
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.7z
Type: application/octet-stream
Size: 1343 bytes
Desc: not available
URL: <http://www.mkgmap.org.uk/pipermail/mkgmap-dev/attachments/20160330/9e50a25a/attachment.obj>
-------------- next part --------------
--- src/uk/me/parabola/mkgmap/osmstyle/actions/PartFilter.java	Thu Dec 18 14:03:32 2014
+++ src/uk/me/parabola/mkgmap/osmstyle/actions/PartFilter.java	Wed Mar 30 20:07:56 2016
@@ -33,13 +33,14 @@
  * Example: if the value is "Aa#Bb#Cc#Dd#Ee"
  * part:#:1  returns Aa
  * part:#:-1 returns Ee
  * part:#:2  returns Bb
  * part:#:-2 returns Dd
- * part:#>1  returns Bb#Cc#Dd#Ee#
- * part:#<5  returns Aa#Bb#Cc#Dd#
- * part:#<-1 returns Aa#Bb#Cc#Dd#
+ * part:#>1  returns Bb#Cc#Dd#Ee
+ * part:#<5  returns Aa#Bb#Cc#Dd
+ * part:#<6  returns Aa#Bb#Cc#Dd#Ee
+ * part:#<-1 returns Aa#Bb#Cc#Dd
  *
  * @author Franco Bez
  * @author Enrico Liboni
  */
 public class PartFilter extends ValueFilter {
@@ -87,35 +88,42 @@
 	public String doFilter(String value, Element el) {
 		if (value == null || partnumber == 0) return null;
 		// split uses a regex we need to replace special characters
 		String[] temp = value.split(Pattern.quote(separator));
 
-		// check if partnumber is in range, if not return null
-		if (temp.length < Math.abs(partnumber) ) return null;
-
 		// get the index of the partnumber
 		// if the partnumber is negative the part is counted from the end of the split
 		int idx=(partnumber > 0)?(partnumber-1):(temp.length+partnumber);
 
 		// default operator ":": return the part
 		if ( !isLt && !isGt ) {
+            // check if idx (partnumber) is in range, if not return null
+            if (idx < 0 || idx >= temp.length) return null;
 			return temp[idx].trim();
 		} else {
 			StringBuffer returnValue= new StringBuffer();
 
 			// operator "<": collate all the parts before the partnumber
 			if ( isLt ) {
+                // check if idx (partnumber) is in range, if not return null
+                idx--;
+                if (idx < 0 || idx >= temp.length) return null;
 				for (int i=0;i<idx;i++) {
 					returnValue.append(temp[i]).append(separator);
 				}
+				returnValue.append(temp[idx]);
 			}
 
 			// operator ">": collate all the parts after the partnumber
 			if ( isGt ) {
-				for (int i=idx+1;i<temp.length;i++) {
+                // check if idx (partnumber) is in range, if not return null
+                idx++;
+                if (idx < 0 || idx >= temp.length) return null;
+				for (int i=idx;i<temp.length-1;i++) {
 					returnValue.append(temp[i]).append(separator);
 				}
+				returnValue.append(temp[temp.length-1]);
 			}
 
 			// return the result
 			return returnValue.toString();
 		}


More information about the mkgmap-dev mailing list