- Added a new date parse method that indicates the length of the parsed item,
  and does not require Nibbler::depletion.
This commit is contained in:
Paul Beckingham 2014-06-29 22:16:44 -04:00
parent 17ffe3d222
commit 0c0e36993d
3 changed files with 59 additions and 16 deletions

View file

@ -131,8 +131,7 @@ Date::Date (
const std::string& input, const std::string& input,
const std::string& format /* = "m/d/Y" */, const std::string& format /* = "m/d/Y" */,
const bool iso /* = true */, const bool iso /* = true */,
const bool epoch /* = true */, const bool epoch /* = true */)
const bool require_depletion /* = true */)
{ {
// Check first to see if this is supported as a named date. // Check first to see if this is supported as a named date.
Variant v; Variant v;
@ -146,13 +145,13 @@ Date::Date (
Nibbler n (input); Nibbler n (input);
n.save (); n.save ();
#ifdef NIBBLER_FEATURE_DATE #ifdef NIBBLER_FEATURE_DATE
if (n.getDate (format, _t) && (!require_depletion || n.depleted ())) if (n.getDate (format, _t) && n.depleted ())
return; return;
#endif #endif
// Parse an ISO date. // Parse an ISO date.
n.restore (); n.restore ();
if (iso && n.getDateISO (_t) && (!require_depletion || n.depleted ())) if (iso && n.getDateISO (_t) && n.depleted ())
return; return;
// Perhaps it is an epoch date, in string form? // Perhaps it is an epoch date, in string form?
@ -163,6 +162,53 @@ Date::Date (
throw ::format (STRING_DATE_INVALID_FORMAT, input, format); throw ::format (STRING_DATE_INVALID_FORMAT, input, format);
} }
////////////////////////////////////////////////////////////////////////////////
Date::Date (
const std::string& input,
std::string::size_type& i,
const std::string& format /* = "m/d/Y" */,
const bool iso /* = true */,
const bool epoch /* = true */)
{
// Check first to see if this is supported as a named date.
Variant v;
if (namedDates (input, v))
{
i = v.source ().length ();
_t = v.get_date ();
return;
}
// Parse a formatted date.
Nibbler n (input);
n.save ();
#ifdef NIBBLER_FEATURE_DATE
if (n.getDate (format, _t))
{
i = n.cursor ();
return;
}
#endif
// Parse an ISO date.
n.restore ();
if (iso && n.getDateISO (_t))
{
i = n.cursor ();
return;
}
// Perhaps it is an epoch date, in string form?
n.restore ();
if (epoch && isEpoch (input))
{
i = 10;
return;
}
throw ::format (STRING_DATE_INVALID_FORMAT, input, format);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Date::Date (const Date& rhs) Date::Date (const Date& rhs)
{ {

View file

@ -43,8 +43,12 @@ public:
Date (const std::string&, Date (const std::string&,
const std::string& format = "m/d/Y", const std::string& format = "m/d/Y",
const bool iso = true, const bool iso = true,
const bool epoch = true, const bool epoch = true);
const bool require_depletion = true); Date (const std::string&,
std::string::size_type&,
const std::string& format = "m/d/Y",
const bool iso = true,
const bool epoch = true);
Date (const Date&); Date (const Date&);
virtual ~Date (); virtual ~Date ();

View file

@ -660,17 +660,10 @@ bool Lexer::is_date (std::string& result)
{ {
try try
{ {
// TODO Why stop at the space? This seems wrong. std::string::size_type legacy_i = 0;
std::string::size_type legacy_i = _input.find (' ', _i); Date legacyDate (_input.substr (_shift_counter), legacy_i, Lexer::dateFormat, false, false);
if (legacy_i == std::string::npos) result = _input.substr (_shift_counter, legacy_i);
legacy_i = _input.length ();
std::string legacy_result = _input.substr (_shift_counter, legacy_i - _shift_counter);
Date legacyDate (legacy_result, Lexer::dateFormat, false, false, false);
legacy_i -= _shift_counter;
while (legacy_i--) shift (); while (legacy_i--) shift ();
result = legacy_result;
return true; return true;
} }