stdInputRange

Salih Dincer salihdb at hotmail.com
Sun Jul 20 08:46:08 UTC 2025


Hello D Language Forum!

I’m running the InputRange example below and it dutifully reads 
from stdin until it spots a tilde, printing each character in 
every loop iteration. I even tried to break out with F6 but 
couldn’t get it to stop. Curiously, swapping the tilde for the 
'\t' character makes everything work flawlessly. Any ideas why 
this is happening? I’d really appreciate your insights!

```d
import std;

struct StdinByChar
{
   @property bool empty()
   {
     if(isEmpty)
       return true;

     if(!hasChar)
     {
       auto buff = new char[1];
       stdin.rawRead(buff);

       if (buff[0] == 0x7E) // tilde (~) character
       {
         isEmpty = true;
         return true;
       }
       chr = buff[0];
       hasChar = true;
     }
     return false;
   }

   @property auto front() =>  chr;

   auto popFront()  =>  hasChar = false;

   private:
     char   chr;
     bool   hasChar, isEmpty;
}

void main()
{
     auto r = StdinByChar();

     while (!r.empty)
     {
         write(r.front);
         r.popFront();
     }

     writeln();

     write("Range is ");
     if(!r.empty) write("not");
     writeln("empty!");
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list