April 20, 2015
On Monday, 20 April 2015 at 18:03:50 UTC, John Colvin wrote:
> On Monday, 20 April 2015 at 17:48:17 UTC, Panke wrote:
>> To measure the columns needed to print a string, you'll need the number of graphemes. (d|)?string.length gives you the number of code units.
>
> Even that's not really true.

Why? Doesn't string.length give you the byte count?
April 20, 2015
On Monday, 20 April 2015 at 19:24:01 UTC, Panke wrote:
> On Monday, 20 April 2015 at 18:03:50 UTC, John Colvin wrote:
>> On Monday, 20 April 2015 at 17:48:17 UTC, Panke wrote:
>>> To measure the columns needed to print a string, you'll need the number of graphemes. (d|)?string.length gives you the number of code units.
>>
>> Even that's not really true.
>
> Why? Doesn't string.length give you the byte count?

You'll also need the unicode character display width:
Even if the font is monospaced, there are characters (Katakana, Hangul and even in Latin script) with variable width.


ABCDEFGH
ABCDEFGH (unicode 0xff21 through 0xff27).

If the text above is not correctly displayed on your computer, a Korean console can be viewed here:

http://upload.wikimedia.org/wikipedia/commons/1/14/KoreanDOSPrompt.png



April 21, 2015
On Monday, 20 April 2015 at 19:24:01 UTC, Panke wrote:
> On Monday, 20 April 2015 at 18:03:50 UTC, John Colvin wrote:
>> On Monday, 20 April 2015 at 17:48:17 UTC, Panke wrote:
>>> To measure the columns needed to print a string, you'll need the number of graphemes. (d|)?string.length gives you the number of code units.
>>
>> Even that's not really true.
>
> Why? Doesn't string.length give you the byte count?

I think what you are looking for is string.sizeof?

From the D reference

.sizeof	Returns the array length multiplied by the number of bytes per array element.
.length	Returns the number of elements in the array. This is a fixed quantity for static arrays. It is of type size_t.


Isn't a string type an array of characters (char[] string UTF-8, wchar[] string UTF-16, and dchar[] string UTF-32) and not arbitrary bytes?
April 21, 2015
On Tuesday, 21 April 2015 at 13:06:22 UTC, JohnnyK wrote:
> On Monday, 20 April 2015 at 19:24:01 UTC, Panke wrote:
>> On Monday, 20 April 2015 at 18:03:50 UTC, John Colvin wrote:
>>> On Monday, 20 April 2015 at 17:48:17 UTC, Panke wrote:
>>>> To measure the columns needed to print a string, you'll need the number of graphemes. (d|)?string.length gives you the number of code units.
>>>
>>> Even that's not really true.
>>
>> Why? Doesn't string.length give you the byte count?

I was talking about the "you'll need the number of graphemes". s.length returns the number of elements in the slice, which in the case of D's string types gives is the same as the number of code units.

> I think what you are looking for is string.sizeof?
>
> From the D reference
>
> .sizeof	Returns the array length multiplied by the number of bytes per array element.
> .length	Returns the number of elements in the array. This is a fixed quantity for static arrays. It is of type size_t.

That is for static arrays only. .sizeof for slices is just size_t.sizeof + T*.sizeof i.e. 8 on 32 bit, 16 on 64 bit.
1 2 3 4 5 6 7
Next ›   Last »