Bug 18205 – Format string with width specification doesn't work with multibyte characters

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-01-07T16:21:35Z
Last change time
2020-03-21T03:56:33Z
Keywords
pull
Assigned to
No Owner
Creator
dransic

Comments

Comment #0 by dransic — 2018-01-07T16:21:35Z
--- import std.format; void main() { assert("|%8s|".format("abc") == "| abc|"); assert("|%8s|".format("αβγ") == "| αβγ|"); // FAILS! -> | αβγ| } ---
Comment #1 by dransic — 2018-01-07T18:24:28Z
The current behaviour seems to be by design, according to this unittest assert from Phobos (std.format#L5779): assert(format("%8s", "b\u00e9ll\u00f4") == " b\u00e9ll\u00f4"); The length of the tested string is 8 (code points), but it prints 6 glyphs. I can't guess the rationale behind this, since the purpose is formatting.
Comment #2 by b2.temp — 2018-01-07T19:02:15Z
Actually the fix is quite easy and obvious. I reopen pull https://github.com/dlang/phobos/pull/6008 And let's see what will the reviewers think.
Comment #3 by hsteoh — 2018-01-08T15:52:49Z
*** This issue has been marked as a duplicate of issue 7054 ***
Comment #4 by hsteoh — 2018-01-08T15:53:58Z
This is a long-standing issue that has been known for a while now. It's not so easy to fix because the correct behaviour depends on byGrapheme, but the last time I checked, that breaks @safety and CTFE-ability.