Open
Description
It would be very useful to be able to truncate strings to a certain length and also pad left and right, currently only padding left appears to be supported. maybe upper case S could support traditional extended string formatting like
%10S - pad right
%-10S - pad left
%.10S - truncate right
%-.10S - truncate left
%10.10S - pad and truncate right
%-10.10S - pad and truncate left
I would consider creating a patch request if the idea is palatable
Activity
wdavidw commentedon Jan 23, 2018
I'm ok with the idea. Is there some standart directed by printf relative to right padding ?
revelt commentedon Apr 18, 2018
@wdavidw the spec does cover padding: http://www.cplusplus.com/reference/cstdio/printf/
Having said that, from example above, I couldn't understand what would
%-10S
for example mean from it. Because%
is always there,-
stands for left-justify within given field; now hard part -10
is width andS
would be specifier, but I can't find specifier with capitalS
, only lowercase...mscalora commentedon Apr 18, 2018
The C standard docs I have seen are a bit vague about how some features like precision apply to strings. Using Apple's clang/LLVM, not sure whose runtime, gnu maybe:
I get:
The only places my suggestion differs are
%-.10s
and%-10.10s
where clib truncates right. I tried negative precision like%.-10s
but clang produces an "invalid specifier" error though this could allow pad-left-truc-right or pad-right-truc-left like%-10.10s
and%10.-10s
while%-10.-10s
would then need to be used for pad-left-trunc-left but I don't think that is really very useful to mix left and right in the same format specifier.I think I suggested uppercase
S
to preserve current behaviour fors
. It is sometimes used as an extension for double byte strings where the C standard uses%ls
.For reference:
produces:
wdavidw commentedon Sep 3, 2018
Any suggestion on this issue ?
revelt commentedon Sep 4, 2018
@wdavidw If you meant me, sorry I'm completely out of touch with this issue; this library is working fine for my needs currently, I'm using it daily, all is fine. Thank you for great work, I should have written it myself if you haven't created it because I have to deliver code in both Nunjucks (Node) and Jinja (Python) and "out-of-box", this filter is missing outside Python. Thank you!