My simple internet client made in Dlang.

Ali Çehreli acehreli at yahoo.com
Thu Feb 4 20:54:15 UTC 2021


On 2/3/21 8:44 AM, Marcone wrote:
> relevant opinion

I think the following would be improvements:

 >    char[8192] request;

I don't know the protocol but obviously 8192 must be sufficient.

 >    auto rq = c.receive(request);

So, what is actually received is request[0..rq]. I would call the first 
one 'buffer' and the actual part 'request':

   char[8192] buffer;
   const length = c.receive(buffer);
   enforce (length != Socket.ERROR, "Failed to read from socket.");
   auto request = buffer[0 .. length];

Each to!string is an extra copy below (and more in your code):

 >    string host = 
to!string(request)[to!string(request).indexOf("CONNECT")+7..to!string(request).indexOf(":")].strip();

I would make a variable first (you can find better variable names :) ):

   string str = to!string(request);
   const conn = str.indexOf("CONNECT") + 7;
   const colon = str.indexOf(":");
   const http = str.indexOf("HTTP/");

   string host = str[conn .. colon].strip();
   string port = str[colon + 1 .. http].strip();

Even though performance of multiple to!string calls is not detectable in 
this program that interacts with sockets, I still think reducing the 
calls to to!string helps with readability and is less error-prone. :)

Ali


More information about the Digitalmars-d-learn mailing list