Modify

Opened 7 weeks ago

Closed 10 hours ago

Last modified 10 hours ago

#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 Famlam)

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:

  1. the outer concat to just merge key and value (this is fine)
  2. the inner replace(...) which effectively removes the value of name from a ;-separated value of alt_name (this is fine)
  3. 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 Famlam, 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:2 by Famlam, 17 hours ago

Just checking in, is this a change that can possibly be considered?

comment:3 by stoecker, 10 hours ago

Resolution: fixed
Status: newclosed

In 19259/josm:

fix #23943 - patch by Famlam - allow negative values for substring to count from the end of the string

comment:4 by stoecker, 10 hours ago

Milestone: 24.11

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain team.
as The resolution will be set.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.