Files
taskwarrior-2.x/src/Nibbler.h
Paul Beckingham 7a45db4d0f Feature #891
- Added feature #891, which allows for leftmost partial matches of UUID values.
  Makes direct comparisons for full-length values, and regular expressions for
  partial values.  Note that there is a minimum length of 8 hex digits.
- Added safety parsing mechanism that fails a partial UUID if immediately
  followed by a hex digit.  This allows for numbers longer than 8 digits to not
  be misinterpreted as a UUID.
- Implemented Nibbler::getPartialUUID.
- Implemented unit tests.
@@ -1145,12 +1145,23 @@ const A3 A3::sequence (const A3& input) const

   for (unsigned int i = 0; i < uuids.size (); ++i)
   {
-    if (ids.size ())
+    if (ids.size () + i > 0)
       sequenced.push_back (Arg ("or", Arg::cat_op));

-    sequenced.push_back (Arg ("uuid",        Arg::type_string, Arg::cat_dom));
-    sequenced.push_back (Arg ("=",                             Arg::cat_op));
-    sequenced.push_back (Arg (uuids[i],      Arg::type_string, Arg::cat_literal));
+    // A full-length UUID requires a string comparison.
+    if (uuids[i].length () == 36)
+    {
+      sequenced.push_back (Arg ("uuid",         Arg::type_string, Arg::cat_dom));
+      sequenced.push_back (Arg ("=",                              Arg::cat_op));
+      sequenced.push_back (Arg (uuids[i],       Arg::type_string, Arg::cat_literal));
+    }
+    // A UUID fragment is a leftmost comparison.
+    else
+    {
+      sequenced.push_back (Arg ("uuid",         Arg::type_string, Arg::cat_dom));
+      sequenced.push_back (Arg ("~",                              Arg::cat_op));
+      sequenced.push_back (Arg ("^" + uuids[i], Arg::type_string, Arg::cat_rx));
+    }
   }

   sequenced.push_back (Arg (")", Arg::cat_op));
@@ -1674,11 +1685,11 @@ bool A3::is_uuid (Nibbler& n, std::string& result)
   n.save ();
   result = "";
   std::string uuid;
-  if (n.getUUID (uuid))
+  if (n.getPartialUUID (uuid))
   {
     result += uuid;
     while (n.skip (',') &&
-           n.getUUID (uuid))
+           n.getPartialUUID (uuid))
     {
       result += ',' + uuid;
     }
@@ -1997,13 +2008,13 @@ bool A3::extract_uuid (
   Nibbler n (input);

   std::string uuid;
-  if (n.getUUID (uuid))
+  if (n.getPartialUUID (uuid))
   {
     sequence.push_back (uuid);

     while (n.skip (','))
     {
-      if (!n.getUUID (uuid))
+      if (!n.getPartialUUID (uuid))
         throw std::string (STRING_A3_UUID_AFTER_COMMA);

       sequence.push_back (uuid);
2012-02-19 22:27:40 -05:00

118 lines
3.8 KiB
C++

////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2006-2012, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_NIBBLER
#define INCLUDED_NIBBLER
#define L10N // Localization complete.
#define NIBBLER_FEATURE_DATE
//#undef NIBBLER_FEATURE_DATE
#define NIBBLER_FEATURE_REGEX
//#undef NIBBLER_FEATURE_REGEX
#include <string>
#include <vector>
class Nibbler
{
public:
Nibbler (); // Default constructor
Nibbler (const std::string&); // Constructor
Nibbler (const Nibbler&); // Copy constructor
Nibbler& operator= (const Nibbler&); // Assignment operator
~Nibbler (); // Destructor
bool getUntil (char, std::string&);
bool getUntil (const std::string&, std::string&);
#ifdef NIBBLER_FEATURE_REGEX
bool getUntilRx (const std::string&, std::string&);
#endif
bool getUntilOneOf (const std::string&, std::string&);
bool getUntilWS (std::string&);
bool getUntilEOL (std::string&);
bool getUntilEOS (std::string&);
/*
bool getAllOneOf (const std::string&, std::string&);
*/
bool getN (const int, std::string&);
bool getQuoted (char, std::string&, bool quote = false);
bool getDigit (int&);
bool getInt (int&);
bool getHex (int&);
bool getUnsignedInt (int&);
bool getNumber (std::string&);
bool getNumber (double&);
bool getUnsignedNumber (double&);
bool getLiteral (const std::string&);
#ifdef NIBBLER_FEATURE_REGEX
bool getRx (const std::string&, std::string&);
#endif
bool getUUID (std::string&);
bool getPartialUUID (std::string&);
bool getDateISO (time_t&);
#ifdef NIBBLER_FEATURE_DATE
bool getDate (const std::string&, time_t&);
#endif
bool getOneOf (const std::vector <std::string>&, std::string&);
bool getName (std::string&);
bool getWord (std::string&);
bool skipN (const int quantity = 1);
bool skip (char);
bool skipAll (char);
bool skipAllOneOf (const std::string&);
bool skipWS ();
#ifdef NIBBLER_FEATURE_REGEX
bool skipRx (const std::string&);
#endif
char next ();
std::string next (const int quantity);
std::string::size_type cursor ();
std::string::size_type save ();
std::string::size_type restore ();
const std::string& str () const;
bool depleted ();
static bool isPunctuation (char);
std::string dump ();
private:
std::string _input;
std::string::size_type _length;
std::string::size_type _cursor;
std::string::size_type _saved;
};
#endif
////////////////////////////////////////////////////////////////////////////////