Skip to content

Commit

Permalink
Put back compareEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Sep 23, 2024
1 parent 74fe399 commit d30dddc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
18 changes: 12 additions & 6 deletions bytestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Bytestream Bytestream::peekBytestream(size_t len) const
return other;
}

bool Bytestream::nextString(const std::string& s)
bool Bytestream::nextString(const std::string& s, bool compareEqual)
{
size_t noOfNextBytes = s.length();

Expand All @@ -178,7 +178,8 @@ bool Bytestream::nextString(const std::string& s)
return false;
}

if(peekString(noOfNextBytes) == s)
bool res = peekString(noOfNextBytes) == s;
if(res == compareEqual)
{
_after(noOfNextBytes);
return true;
Expand All @@ -188,7 +189,8 @@ bool Bytestream::nextString(const std::string& s)
return false;
}
}
bool Bytestream::nextBytestream(const Bytestream& other)

bool Bytestream::nextBytestream(const Bytestream& other, bool compareEqual)
{
size_t noOfNextBytes = other.size();

Expand All @@ -198,11 +200,15 @@ bool Bytestream::nextBytestream(const Bytestream& other)
}

bool res = memcmp(&(_data[_pos]), other.raw(), noOfNextBytes) == 0;
if(res)
if(res == compareEqual)
{
*this += noOfNextBytes;
_after(noOfNextBytes);
return true;
}
else
{
return false;
}
return res;
}

void Bytestream::putString(const std::string& s)
Expand Down
4 changes: 2 additions & 2 deletions bytestream.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class Bytestream
}
}

bool nextString(const std::string& bts);
bool nextBytestream(const Bytestream& bts);
bool nextString(const std::string& bts, bool compareEqual=true);
bool nextBytestream(const Bytestream& bts, bool compareEqual=true);

template<typename T, typename std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
void put(T u)
Expand Down
20 changes: 19 additions & 1 deletion tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,25 @@ TEST(test_method)
ASSERT(bts.nextBytestream(bts2));
ASSERT_FALSE(bts.nextBytestream(bts2)); // already consumed

bts -= 10;
Bytestream bts3, bts4;
bts3 << "sooomeString";
bts4 << "someStrong";
// inverting the comparison result should not influence length mismatch
ASSERT_FALSE(bts.nextBytestream(bts3, false));
// ...only advance the read position if the bytestreams aren't equal
ASSERT(bts.nextBytestream(bts4, false));
ASSERT(bts.atEnd());
// inverting the comparison result should not influence length mismatch
ASSERT_FALSE(bts.nextBytestream(bts4, false));
bts -= 10;
// inverting the comparison result should not influence length mismatch
ASSERT_FALSE(bts.nextString("sooomeString", false));
// ...only advance the read position if the bytestreams aren't equal
ASSERT(bts.nextString("someStrong", false));
ASSERT(bts.atEnd());
// inverting the comparison result should not influence length mismatch
ASSERT_FALSE(bts.nextString("someStrong", false));
}

#define HIBIT(sign, suffix) \
Expand Down Expand Up @@ -313,7 +331,7 @@ TEST(constructors)
ASSERT(bts4 >>= "someString");
}

TEST(move_semanticcs)
TEST(move_semantics)
{
Bytestream bts1("some contents");
Bytestream bts2(std::move(bts1));
Expand Down

0 comments on commit d30dddc

Please sign in to comment.