Did you know; we need this operator!
Salih Dincer
salihdb at hotmail.com
Wed Sep 18 09:40:26 UTC 2024
On Saturday, 14 September 2024 at 01:01:06 UTC, Paul Backus wrote:
> This could also be solved by allowing `with()` to be used as an
> expression, not just a statement:
In fact, if the D parser is not sufficient, you can make your own
parser using a simple regular expression! Dconf'24 brought me
such a perspective:
```D
import std.regex, std.stdio;
enum Days
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};
void main()
{
auto jsonStr = `{
"Pazar": Sunday,
"Pazartesi": Monday,
"Salı": Tuesday,
"Çarşamba": Wednesday,
"Perşembe": Thursday,
"Cuma": Friday,
"Cumartesi": Saturday,
}`;
Days[string] Item;
enum regex = `"(\w+)": (\w+),`;
foreach (matches; jsonStr.matchAll(regex))
{
string key = matches.captures[1];
string value = matches.captures[2];
writefln("\"%s\": %s", key, value);
Item[key] = value.indexOf!Days;
}
Item.writeln;
}
auto indexOf(E)(string value)
{
import std.conv : to;
E count;
while (count < count.max)
{
if (count.to!string == value) break;
++count;
}
return count;
}
```
***Prints:***
```D
/*
"Pazar": Sunday
"Pazartesi": Monday
"Salı": Tuesday
"Çarşamba": Wednesday
"Perşembe": Thursday
"Cuma": Friday
"Cumartesi": Saturday
["Cumartesi":Saturday, "Pazartesi":Monday, "Pazar":Sunday,
"Salı":Tuesday, "Çarşamba":Wednesday, "Perşembe":Thursday,
"Cuma":Friday]
Process finished.
*/
```
SDB at 79
More information about the Digitalmars-d
mailing list