tera icon indicating copy to clipboard operation
tera copied to clipboard

split filter generates an empty list element if the string to be split ends or beings with the separator

Open yacoob opened this issue 5 years ago • 3 comments

I'm not sure whether this is an expected or desired behaviour - I've noticed this as a change compared to whatever version of tera zola 0.9 was using :)

{% set path = "/a/b/c/" %}
{{ path|split(pat='/') }}

returns

[a, b, c, ]

It's either a bug or undocumented behaviour :) Please pick one and adjust code or documentation as necessary :D

yacoob avatar Oct 27 '20 21:10 yacoob

I just found a bug related to this - the empty (?) element throws off slicing:

{% set path="/a/b/c/" %}
{{ path|safe }}
{{ path|split(pat='/') }}
{{ path|split(pat='/')|slice(end=2) }}                                                                                                                                                                                                                

{% set path="a/b/c" %}
{{ path|safe }}
{{ path|split(pat='/') }}                                                                                                                                                                                                                             {{ path|split(pat='/')|slice(end=2) }}

results in:

a/b/c
[a, b, c]
[a, b]


/a/b/c/
[a, b, c, ]
[a]

The results of those slices should be identical in first and second case.

yacoob avatar Oct 27 '20 22:10 yacoob

Actually, the case where string starts with a separator is even more insidious - the element is not visible after stringification but affects the count. Here's a full set of tests:

{% set path="a/b/c/" %}
{{ path|safe  }}
{{ path|split(pat='/')  }}
{{ path|split(pat='/')|slice(end=2)  }}

{% set path="/a/b/c" %}
{{ path|safe  }}
{{ path|split(pat='/')  }}
{{ path|split(pat='/')|slice(end=2)  }}

{% set path="/a/b/c/" %}
{{ path|safe  }}
{{ path|split(pat='/')  }}
{{ path|split(pat='/')|slice(end=2)  }}

{% set path="a/b/c" %}
{{ path|safe  }}
{{ path|split(pat='/')  }}
{{ path|split(pat='/')|slice(end=2)  }}

results in:

a/b/c/
[a, b, c, ]
[a, b]


/a/b/c
[a, b, c]
[a]


/a/b/c/
[a, b, c, ]
[a]


a/b/c
[a, b, c]
[a, b]

yacoob avatar Oct 27 '20 22:10 yacoob

The split filter is just the Rust split fn :/ I'm not sure what's the deal with the leading element missing.

Keats avatar Oct 28 '20 10:10 Keats