weird formattedRead
Oleg B
code.viator at gmail.com
Fri Apr 9 16:11:26 UTC 2021
Hello, I have some doubts about working `formattedRead` with
space chars.
Example:
```d
import std : formattedRead, DateTime, stderr, each;
DateTime parseDT(string str)
{
int d,mo,y, h,m,s;
formattedRead!"%d/%d/%d %d:%d:%d"(str, d,mo,y, h,m,s);
return DateTime(y,mo,d, h,m,s);
}
void tryParse(string s)
{
try
{
auto dt = parseDT(s);
stderr.writefln!"valid '%s': %s"(s, dt);
}
catch (Exception e)
stderr.writefln!"INVALID '%s': %s"(s, e.msg);
}
void main()
{
auto vs = [
"",
"1",
"1/1",
"1/1/1",
"1/1/1 1",
"1/1/1 1:1",
"1/1/1 1:1:1",
];
vs.each!tryParse;
}
```
outputs:
```
INVALID '': 0 is not a valid month of the year.
INVALID '1': parseToFormatSpec: Cannot find character '/' in the
input string.
INVALID '1/1': parseToFormatSpec: Cannot find character '/' in
the input string.
valid '1/1/1': 0001-Jan-01 00:00:00 <<< see here
INVALID '1/1/1 1': parseToFormatSpec: Cannot find character ':'
in the input string.
INVALID '1/1/1 1:1': parseToFormatSpec: Cannot find character ':'
in the input string.
valid '1/1/1 1:1:1': 0001-Jan-01 01:01:01
```
Is space a special char for `formattedRead` and it simple stop
parse without throwing exception if not found space (that
represented in fmt string)?
Have `formattedRead` any other special chars?
Or it's bug?
More information about the Digitalmars-d-learn
mailing list