Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 10, 2016 Separate IP parts | ||||
---|---|---|---|---|
| ||||
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. |
December 10, 2016 Re: Separate IP parts | ||||
---|---|---|---|---|
| ||||
Posted in reply to brocolis | 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.
|
December 10, 2016 Re: Separate IP parts | ||||
---|---|---|---|---|
| ||||
Posted in reply to brocolis | 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);
|
December 10, 2016 Re: Separate IP parts | ||||
---|---|---|---|---|
| ||||
Posted in reply to biozic | 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'
|
December 10, 2016 Re: Separate IP parts | ||||
---|---|---|---|---|
| ||||
Posted in reply to notna | 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.
|
December 10, 2016 Re: Separate IP parts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | 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 :)
|
December 11, 2016 Re: Separate IP parts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | 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 ;)
|
December 11, 2016 Re: Separate IP parts | ||||
---|---|---|---|---|
| ||||
Posted in reply to brocolis | 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.
|
Copyright © 1999-2021 by the D Language Foundation