Best way to count character spaces.

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 1 05:36:30 PDT 2015


On 7/1/15 1:25 AM, H. S. Teoh via Digitalmars-d-learn wrote:
> 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.
>
>

BTW, this exercise would make an EXCELLENT blog post highlighting both 
the power of D's unicode support and the hairy issues of unicode.

I like the ascii er... unicode art concept :)

-Steve



More information about the Digitalmars-d-learn mailing list