digitalmars.D.learn - Separate IP parts
- brocolis (10/10) Dec 09 2016 How do I separate IP parts with dlang?
- Anonymouse (18/28) Dec 09 2016 Not much of a trick, but:
- biozic (8/18) Dec 10 2016 This would do the same. I wouldn't say it's a trick.
- notna (27/34) Dec 10 2016 Well, you know, that's one of the not so great things about
- Nicholas Wilson (2/8) Dec 10 2016 Those statements need to be inside a function.
- Era Scarecrow (2/5) Dec 11 2016 Heh, I'd prefer to use sscanf vs using the streams.
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
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
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
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
On Saturday, 10 December 2016 at 13:21:40 UTC, notna wrote:On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:Those statements need to be inside a function.[...]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: [...]
Dec 10 2016
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:I guess that's the reputation complaints here a building up :)On Saturday, 10 December 2016 at 08:03:00 UTC, biozic wrote:Those statements need to be inside a function.[...]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: [...]
Dec 10 2016
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
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/5328190Heh, I'd prefer to use sscanf vs using the streams.
Dec 11 2016