Read a unicode character from the terminal

Ali Çehreli acehreli at yahoo.com
Sun Apr 1 07:02:17 PDT 2012


On 04/01/2012 05:00 AM, Jacob Carlborg wrote:
> On 2012-04-01 01:17, Ali Çehreli wrote:
>> On 03/31/2012 11:53 AM, Ali Çehreli wrote:
>>
>> > The solution is to use ranges when pulling Unicode characters out of
>> > strings. std.stdin does not provide this yet, but it will eventually
>> > happen (so I've heard :)).
>>
>> Here is a Unicode character range, which is unfortunately pretty
>> inefficient because it relies on an exception that is thrown from
>> isValidDchar! :p
>
> Ok, what's the differences compared to the example in your first post:
>
> void main()
> {
> string line = readln();
>
> foreach (dchar c; line) {
> writeln(c);
> }
> }
>

No difference in that example because it consumes the entire input as 
dchars.

But in general, with that inefficient range, it is possible to pull just 
one dchar from the input and leave the rest of the stream untouched. For 
example, it would be possible to readf() an int right after that:

     auto u = byUnicode();

     dchar d = u.front;  // <-- reads just one dchar from the range

     int i;
     readf("%s", &i);    // <-- continues with std.stdio functions
     writeln(i);

With the getline() method, the int must be looked up in the line first, 
then from the input.

Ali


More information about the Digitalmars-d-learn mailing list