www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - stdInputRange

reply Salih Dincer <salihdb hotmail.com> writes:
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 79
Jul 20
next sibling parent Dejan Lekic <dejan.lekic gmail.com> writes:
I've tested your code on the source code itself and it works as 
expected:

```
» ./salih2 < salih2.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 (
Range is empty!

```
Jul 20
prev sibling parent reply user1234 <user1234 12.de> writes:
On Sunday, 20 July 2025 at 08:46:08 UTC, Salih Dincer wrote:
 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!
No problem here either. Where are you running the program from (embedded terminal in an editor ? a terminal emulator ?). Are you on Windows or Linux ?
Jul 20
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Sunday, 20 July 2025 at 12:32:17 UTC, user1234 wrote:
 No problem here either. Where are you running the program from 
 (embedded terminal in an editor ? a terminal emulator ?). Are 
 you on Windows or Linux ?
I want the program to end with the F6 key on my keyboard instead of the tilde. I use Windows as the platform. SDB 79
Jul 20
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Sunday, 20 July 2025 at 20:26:02 UTC, Salih Dincer wrote:
 On Sunday, 20 July 2025 at 12:32:17 UTC, user1234 wrote:
 No problem here either. Where are you running the program from 
 (embedded terminal in an editor ? a terminal emulator ?). Are 
 you on Windows or Linux ?
I want the program to end with the F6 key on my keyboard instead of the tilde. I use Windows as the platform. SDB 79
There is no F6 Unicode character. Are you seeing tildes when you press F6 and thinking it’s an actual tilde in the stream? -Steve
Jul 20
parent Salih Dincer <salihdb hotmail.com> writes:
On Sunday, 20 July 2025 at 21:59:48 UTC, Steven Schveighoffer 
wrote:
 There is no F6 Unicode character. Are you seeing tildes when 
 you press F6 and thinking it’s an actual tilde in the stream?
When I press the F6 key, ^Z characters appear on the screen. I want the program to terminate using this key instead of the tilde character. Should I increase the buffer to 2 bytes? SDB 79
Jul 20
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Sunday, 20 July 2025 at 20:26:02 UTC, Salih Dincer wrote:
 I want the program to end with the F6 key on my keyboard 
 instead of the tilde. I use Windows as the platform.
Thank you for all your responses. I checked the ASCII table, and 0x1A is indeed the character I was looking for. I'm not sure what this would be for Linux, but it works on Windows: ```d struct StdinByChar { property bool empty() { if(isEmpty) return true; if(!hasChar) { auto buff = new char[1]; stdin.rawRead(buff); if (buff[0] == 0x1A) // F6 (^Z) 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; } ``` SDB 79
Jul 20