Skip to content

Commit 1cff524

Browse files
authored
Shove intRanges unpacking into commons. (#313)
1 parent 7988e1b commit 1cff524

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

ParserHelpers.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,32 @@ ullongList::ullongList(std::istream& theStream)
235235
parseStream(theStream);
236236
}
237237

238+
intRange::intRange(std::istream& theStream)
239+
{
240+
registerRegex(integerRegex, [this](const std::string& theInt, [[maybe_unused]] std::istream& unused) {
241+
integers.push_back(stringToInteger<int>(theInt));
242+
});
243+
registerRegex(quotedIntegerRegex, [this](const std::string& theInt, [[maybe_unused]] std::istream& unused) {
244+
const auto newInt = theInt.substr(1, theInt.size() - 2);
245+
integers.push_back(stringToInteger<int>(newInt));
246+
});
247+
248+
parseStream(theStream);
249+
if ((integers.size() % 2) != 0)
250+
{
251+
throw std::runtime_error("IntRange has an odd number of items. We cannot parse it.");
252+
}
253+
for (unsigned int i = 0; i < integers.size(); i += 2U)
254+
{
255+
const int startingInt = integers[i];
256+
const int additionalInts = integers[i + 1U];
257+
for (int j = 0; j <= additionalInts; j++)
258+
{
259+
rangedIntegers.emplace(startingInt + j);
260+
}
261+
}
262+
}
263+
238264

239265
singleInt::singleInt(std::istream& theStream)
240266
{

ParserHelpers.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ class ullongList: parser
8787
std::vector<unsigned long long> ullongs;
8888
};
8989

90+
class intRange: parser
91+
{
92+
public:
93+
explicit intRange(std::istream& theStream);
94+
95+
[[nodiscard]] std::set<int> getInts() const { return rangedIntegers; }
96+
97+
private:
98+
std::vector<int> integers;
99+
std::set<int> rangedIntegers;
100+
};
90101

91102
class singleInt: parser
92103
{

tests/ParserHelperTests.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,4 +1286,21 @@ TEST(ParserHelper_Tests, getStringWrapperWorks)
12861286
const auto str = " = 69.420";
12871287
std::stringstream input{str}, input2{str};
12881288
ASSERT_EQ(commonItems::singleString(input).getString(), commonItems::getString(input2));
1289-
}
1289+
}
1290+
1291+
TEST(ParserHelper_Tests, IntRangeCanBeInputAndUnpacked)
1292+
{
1293+
std::stringstream input;
1294+
input << "={ 37330 1 37333 9 37348 1 }\n";
1295+
const auto theInts = commonItems::intRange(input).getInts();
1296+
1297+
EXPECT_THAT(theInts, testing::UnorderedElementsAre(37330, 37331, 37333, 37334, 37335, 37336, 37337, 37338, 37339, 37340, 37341, 37342, 37348, 37349));
1298+
}
1299+
1300+
TEST(ParserHelper_Tests, IntRangeThrowsExceptionForOddNumberOfItems)
1301+
{
1302+
std::stringstream input;
1303+
input << "={37330 1 37333 9 37348 }\n";
1304+
1305+
EXPECT_THROW(const auto theInts = commonItems::intRange(input), std::runtime_error);
1306+
}

0 commit comments

Comments
 (0)