Best way to count character spaces.

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 30 22:25:51 PDT 2015


On Tue, Jun 30, 2015 at 06:33:32PM +0000, Taylor Hillegeist via Digitalmars-d-learn wrote:
> So I am aware that Unicode is not simple... I have been working on a boxes
> like project http://boxes.thomasjensen.com/
> 
> it basically puts a pretty border around stdin characters. like so:
>  ________________________
> /\                       \
> \_|Different all twisty a|
>   |of in maze are you,   |
>   |passages little.      |
>   |   ___________________|_
>    \_/_____________________/
> 
> but I find that I need to know a bit more than the length of the string
> because of encoding differences
[...]

Use std.uni.byGrapheme. That's the only reliable way to count anything
remotely resembling the display length of the string, which is not to be
confused with the number of code points, which is also different from
the length of the string in bytes or the number of code units.

Note that even with byGrapheme, you may still need some post-processing,
because certain terminals may output Asian block characters in double
width, meaning that 1 grapheme takes up two columns on the screen. But
byGrapheme should get you started on the right footing.


T

-- 
If the comments and the code disagree, it's likely that *both* are wrong. -- Christopher


More information about the Digitalmars-d-learn mailing list