www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Separate IP parts

reply brocolis <brocolis deb.null> writes:
How do I separate IP parts with dlang?

I found this very cool trick, with C++: 
http://stackoverflow.com/a/5328190

std::string ip ="192.168.1.54";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << "  " << b << "  " << c << "  "<< d;

I wonder what's the equivalent D code.
Dec 09 2016
next sibling parent Anonymouse <asdf asdf.net> writes:
On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:
 How do I separate IP parts with dlang?

 I found this very cool trick, with C++: 
 http://stackoverflow.com/a/5328190

 std::string ip ="192.168.1.54";
 std::stringstream s(ip);
 int a,b,c,d; //to store the 4 ints
 char ch; //to temporarily store the '.'
 s >> a >> ch >> b >> ch >> c >> ch >> d;
 std::cout << a << "  " << b << "  " << c << "  "<< d;

 I wonder what's the equivalent D code.
Not much of a trick, but: import std.algorithm : splitter, map; import std.range : take; import std.conv : to; import std.array : array; string ip = "192.168.1.54"; auto parts = ip .splitter('.') .take(4) .map!((a) => a.to!int) .array; assert(parts[0] == 192); assert(parts[1] == 168); assert(parts[2] == 1); assert(parts[3] == 54); Remove the .array to keep it lazy, but then you can't index it for the values, only walk through them in a foreach.
Dec 09 2016
prev sibling next sibling parent reply biozic <dransic gmail.com> writes:
On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:
 How do I separate IP parts with dlang?

 I found this very cool trick, with C++: 
 http://stackoverflow.com/a/5328190

 std::string ip ="192.168.1.54";
 std::stringstream s(ip);
 int a,b,c,d; //to store the 4 ints
 char ch; //to temporarily store the '.'
 s >> a >> ch >> b >> ch >> c >> ch >> d;
 std::cout << a << "  " << b << "  " << c << "  "<< d;

 I wonder what's the equivalent D code.
This would do the same. I wouldn't say it's a trick. import std.format : formattedRead; import std.stdio : writefln; string ipAddr = "192.168.1.54"; int a, b, c, d; formattedRead(ipAddr, "%d.%d.%d.%d", &a, &b, &c, &d); writefln("%s %s %s %s", a, b, c, d);
Dec 10 2016
parent reply notna <notna.remove.this ist-einmalig.de> writes:
On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:
 This would do the same. I wouldn't say it's a trick.

     import std.format : formattedRead;
     import std.stdio : writefln;

     string ipAddr = "192.168.1.54";
     int a, b, c, d;
     formattedRead(ipAddr, "%d.%d.%d.%d", &a, &b, &c, &d);
     writefln("%s %s %s %s", a, b, c, d);
Well, you know, that's one of the not so great things about Dlang... you cannot even trust the provided examples, if there are any: C:\Temp\D>more formattedReadIps.d import std.format; string s = "hello!124:34.5"; string a; int b; double c; formattedRead(s, "%s!%s:%s", &a, &b, &c); assert(a == "hello" && b == 124 && c == 34.5); C:\Temp\D>dmd -v formattedReadIps.d binary C:\D\dmd2\windows\bin\dmd.exe version v2.072.1 config C:\D\dmd2\windows\bin\sc.ini parse formattedReadIps formattedReadIps.d(6): Error: unexpected ( in declarator formattedReadIps.d(6): Error: basic type expected, not "%s!%s:%s" formattedReadIps.d(6): Error: found '"%s!%s:%s"' when expecting ')' formattedReadIps.d(6): Error: no identifier for declarator formattedRead(s, _error_) formattedReadIps.d(6): Error: semicolon expected following function declaration formattedReadIps.d(6): Error: declaration expected, not ',' formattedReadIps.d(7): Error: declaration expected, not 'assert'
Dec 10 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:
 On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:
     [...]
Well, you know, that's one of the not so great things about Dlang... you cannot even trust the provided examples, if there are any: [...]
Those statements need to be inside a function.
Dec 10 2016
next sibling parent aberba <karabutaworld gmail.com> writes:
On Saturday, 10 December 2016 at 13:25:13 UTC, Nicholas Wilson 
wrote:
 On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:
 On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:
     [...]
Well, you know, that's one of the not so great things about Dlang... you cannot even trust the provided examples, if there are any: [...]
Those statements need to be inside a function.
I guess that's the reputation complaints here a building up :)
Dec 10 2016
prev sibling parent notna <notna.remove.this ist-einmalig.de> writes:
On Saturday, 10 December 2016 at 13:25:13 UTC, Nicholas Wilson 
wrote:
 On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:

 Those statements need to be inside a function.
Feel free to post a working example or, even better, a pull request with one ;)
Dec 11 2016
prev sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:
 How do I separate IP parts with dlang?

 I found this very cool trick, with C++: 
 http://stackoverflow.com/a/5328190
Heh, I'd prefer to use sscanf vs using the streams.
Dec 11 2016