Comment #0 by jrdemail2000-dlang — 2017-09-06T05:15:14Z
Enhancement request: Add the equivalents of the POSIX wcwidth/wcswidth functions to std.uni.
These functions calculate the expected cell width of a wchar_t or wchar string. This is the normal character width in fixed width fonts. It is useful in command-line apps and other terminal displays where fixed width fonts are commonly used. Generally, CJK characters are two cells wide, most other characters are one cell wide. Cell width properties are part of the unicode standard.
Comment #1 by issues.dlang — 2017-09-09T00:09:36Z
Given that this is tied to the terminal, I wouldn't really expect this in std.uni, which is focused on implementing the Unicode standard, not on system-specific stuff like this or really anything with how the characters will actually look like when dispayed. I also don't know if an equivalent is available on Windows, and in general, we try to have the stuff in Phobos be crossplatform. So, I'm inclined to think that that these are not appropriate for std.uni. But regardless of all of that, these functions should have bindings in druntime so that they can easily be called on the systems that do have them, and those seem to missing right now, which should be fixed. So, I think that the appropriate thing would be to just call them directly rather than doing something in Phobos, but obviously the bindings will have to be added for that to work.
Comment #2 by jrdemail2000-dlang — 2017-09-09T04:40:54Z
(In reply to Jonathan M Davis from comment #1)
I didn't intend the "POSIX" part of the enhancement request as literally as it came out. The 'width' characteristic part of the unicode standard, and comes with the unicode character database. See: http://unicode.org/reports/tr11/. It is not tied to the terminal or Windows. Exposing the system implementation of 'wcwidth' would of course be environment specific, but implementing it based on the unicode tables would not be.
For these reasons, I do think it is legitimate functionality to support in std.uni.
At the same time, there is a strong argument that this be considered a low-priority enhancement. My searches didn't find other D users requesting this. And, it's only useful in fixed-width fonts. I'd certainly use it in the code I'm writing, but I'm finding I can get acceptable estimates with the cheap trick of assuming all CJK characters have width two.
Comment #3 by alphaglosined — 2017-09-09T04:47:18Z
We have the full Unicode database in Phobos. It needs updating and exposing (since this request is asking for one more part of it).
Comment #4 by hsteoh — 2018-01-13T13:59:40Z
We don't need to rely on the local OS implementation of this function; std.uni has enough tools to provide a native implementation. Also, this is independent of Posix, even though wcwidth/wcswidth are the names under which the Posix standard provides this functionality; Unicode TR11 specifies an East Asian Width property for characters. The data file can be obtained here:
ftp://ftp.unicode.org/Public/UNIDATA/EastAsianWidth.txt
Here's a tentative implementation that I made using std.uni's Trie facilities:
https://github.com/quickfur/strwidth
I hope to get this into shape to add to std.uni at some point.
Comment #5 by robert.schadek — 2024-12-01T16:30:47Z