Delimited String Split into an Array

0 votes
asked Feb 1, 2022 in Wanted features by James Booth
When I attempt to render the following in a sequence diagram, the style is broken. Only the first is styled.

<font:monospaced><plain><size:10>item1\nitem2</size></plain></font>

I've search the forums and I can't seem to find an acceptable workaround. If I pass in a JSON array, I can loop those items and render them individually, which fixes the issue, but I don't want to have to create JSON in the diagram for every label in the diagram itself as I want this functionality abstracted away in a library. If I could simply pass in a pipe/comma/etc delimited string into a function, and then split on the pipe and loop through a resulting array in order to render each of the strings individually it would resolve this. This doesn't seem to be possible, right now, is it?
commented Feb 2, 2022 by The-Lu (63,920 points)
- Would you like to put all your message sequence text, with the same style?
commented Feb 2, 2022 by James Booth
I'm building a library for our Engineer to create diagrams in a standardized way. I want to abstract away as much of the "logic" and style as possible for simplicity. I want to render something like this. I'm sure that it's specific to sequence diagrams, though, as I mentioned.

I want them to simply use:

Item(MyItem, "my-label", "meta-1|meta-2|meta-3")

The library has the following:

!define Item(id, label, meta) rectangle "<size:18><b>Item</b></size>\n----\n<font:monospaced><size:15>label</size></font>\n$formatMeta(meta)" as id

meta is a potentially a pipe-delimited list. I did create this function, which works, but it not ideal and if there isn't a solution, I'll go with it.

!function $formatMeta($str, $output="")
    !$open = "<font:monospaced><plain><size:10>"
    !$close = "</size></plain></font>"
    !$newline = "\n"
    !$delPos = %strpos($str, "|")

    !if ($delPos >= 0)
        !$el = %substr($str, 0, $delPos)
        !$output = $output + $open + $el + $close + $newline
        !$str = %substr($str, $delPos + 1)
        !return $formatMeta($str, $output)
    !endif

    !$output = $output + $open + $str + $close
    !return $output
!endfunction

It would be great if my formatMeta function could just split the string and loop the array.
commented Feb 2, 2022 by plantuml (294,960 points)
We are ok to add some new functions in the preprocessor.

However, we try to follow existing standards, so it's better if you give us the name of some existing functions in any other programming language (for example, from https://www.educba.com/string-array-in-python/ ) so that we implement the very same function in PlantUML.

Does it help?
commented Feb 2, 2022 by James Booth
To follow the PlantUML existing pattern for %substr, I would suggest something along the following for consistency:

%splitstr($str, $delimiter)

Example:

!$list = %splitstr("abc~def", "~")

Results in ["abc", "def"]

And is accessed exactly like a JSON array

$list[0] = "abc"

$list[1] = "def"

And looped the same

!foreach $item in $list
  !log $item
!endfor
commented Feb 3, 2022 by plantuml (294,960 points)

Thanks for the idea!

So with last snapshot and on the online server you can have this.

Is this what you were expecting?

commented Feb 3, 2022 by James Booth
That works perfectly. I tried downloading the SNAPSHOT to test it out locally in VS Code and it wasn't working, as if it's not supported yet, however it does work in your link. Thank you. I'll watch for the next release.

Also, the anti-spam verification is constantly blocking me from commenting. I'm not sure why. I fill out my details, wait the countdown, enter the code and click add comment then it refreshes as if I hadn't done any of it and I have to wait again. Several times in a row.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

[Antispam2 Feature: please please wait 1 or 2 minutes (this message will disappear) before pressing the button otherwise it will fail](--------)
To avoid this verification in future, please log in or register.
...