#23943 closed enhancement (fixed)
[PATCH] Mapcss "substring" function: allow negative end index to remove from the end
Reported by: | Famlam | Owned by: | team |
---|---|---|---|
Priority: | normal | Milestone: | 24.11 |
Component: | Core validator | Version: | |
Keywords: | mapcss substring | Cc: |
Description (last modified by )
The problem
Recently I had to use the following mapcss line:
fixAdd: concat("alt_name=", join_list(";", trim_list(split(";", replace(concat(";", join_list(";", trim_list(split(";", tag("alt_name")))), ";"), concat(";", tag("name"), ";"), ";")))));
This consists of three parts:
- the outer
concat
to just merge key and value (this is fine) - the inner
replace(...)
which effectively removes the value ofname
from a;
-separated value ofalt_name
(this is fine) - the in-between part
join_list(";", trim_list(split(";", ...)))
which effectively only serves to remove the;
at the start and end of the string.
Part 3 is obviously not elegant. Although there are other theoretical solutions, e.g.:
get(regexp_match("^;(.+);$", ...), 1)
substring(..., 1, length(...)-1)
where ...
should be replaced by the part under number 2 (the replace(...)
), they're obviously not the most elegant solutions either (especially the second which needs the lengthy replace(...)
-part twice.
Proposed feature
A much nicer solution would be to permit negative end indices in the substring(str, start, end)
function, which should mean to count the final index from the length of the string, e.g.: substring("foo bar baz", 1, -1)
would return oo bar ba
. This is especially useful for variable-length strings in mapcss.
A possible patch:
public static String substring(String s, float begin, float end) { return s == null ? null : s.substring((int) begin, (int) (end >= 0 ? end : s.length() + end)); }
(Possibly with a safeguard that start <= (end > 0 ? end : s.length() + end)
, but this safeguard currently also doesn't exist for the positive-indices-only implementation)
Current code: here
On a side note, the current behavior is that JOSM crashes with java.lang.StringIndexOutOfBoundsException: Range [1, -1) out of bounds for length X
when you use a negative end index (or when the start index is larger than the end index).
Revision:19207 Build-Date:2024-09-03 10:31:55
Attachments (0)
Change History (4)
comment:1 by , 7 weeks ago
Description: | modified (diff) |
---|---|
Summary: | Mapcss "substring" function: allow negative end index to remove from the end → [PATCH] Mapcss "substring" function: allow negative end index to remove from the end |
comment:4 by , 10 hours ago
Milestone: | → 24.11 |
---|
Patch here: https://github.com/JOSM/josm/pull/143