mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
CmdIDs
- Migrated util.cpp compressIds to CmdIDs::compressIds, as this is the only place it is used.
This commit is contained in:
parent
8a31ccabdc
commit
dd6399aba7
2 changed files with 58 additions and 1 deletions
|
@ -31,7 +31,6 @@
|
||||||
#include <Filter.h>
|
#include <Filter.h>
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
#include <text.h>
|
#include <text.h>
|
||||||
#include <util.h>
|
|
||||||
#include <i18n.h>
|
#include <i18n.h>
|
||||||
#include <CmdIDs.h>
|
#include <CmdIDs.h>
|
||||||
|
|
||||||
|
@ -73,6 +72,61 @@ int CmdIDs::execute (std::string& output)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// The vector must be sorted first. This is a modified version of the run-
|
||||||
|
// length encoding algorithm.
|
||||||
|
//
|
||||||
|
// This function converts the vector:
|
||||||
|
//
|
||||||
|
// [1, 3, 4, 6, 7, 8, 9, 11]
|
||||||
|
//
|
||||||
|
// to ths string:
|
||||||
|
//
|
||||||
|
// 1,3-4,6-9,11
|
||||||
|
//
|
||||||
|
std::string CmdIDs::compressIds (const std::vector <int>& ids)
|
||||||
|
{
|
||||||
|
std::stringstream result;
|
||||||
|
|
||||||
|
int range_start = 0;
|
||||||
|
int range_end = 0;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < ids.size (); ++i)
|
||||||
|
{
|
||||||
|
if (i + 1 == ids.size ())
|
||||||
|
{
|
||||||
|
if (result.str ().length ())
|
||||||
|
result << ",";
|
||||||
|
|
||||||
|
if (range_start < range_end)
|
||||||
|
result << ids[range_start] << "-" << ids[range_end];
|
||||||
|
else
|
||||||
|
result << ids[range_start];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ids[range_end] + 1 == ids[i + 1])
|
||||||
|
{
|
||||||
|
++range_end;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (result.str ().length ())
|
||||||
|
result << ",";
|
||||||
|
|
||||||
|
if (range_start < range_end)
|
||||||
|
result << ids[range_start] << "-" << ids[range_end];
|
||||||
|
else
|
||||||
|
result << ids[range_start];
|
||||||
|
|
||||||
|
range_start = range_end = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.str ();
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
CmdCompletionIds::CmdCompletionIds ()
|
CmdCompletionIds::CmdCompletionIds ()
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,6 +35,9 @@ class CmdIDs : public Command
|
||||||
public:
|
public:
|
||||||
CmdIDs ();
|
CmdIDs ();
|
||||||
int execute (std::string&);
|
int execute (std::string&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string compressIds (const std::vector <int>&);
|
||||||
};
|
};
|
||||||
|
|
||||||
class CmdCompletionIds : public Command
|
class CmdCompletionIds : public Command
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue