view: add equality operators

This commit is contained in:
Danny Robson 2015-02-11 16:42:32 +11:00
parent 4764015f9b
commit da464968e6
2 changed files with 28 additions and 0 deletions

View File

@ -90,6 +90,23 @@ util::view::operator[] (size_t idx) const
}
//-----------------------------------------------------------------------------
bool
util::view::operator== (const char *restrict rhs) const
{
return strlen (rhs) == size () &&
0 == strncmp (rhs, m_begin, size ());
}
//-----------------------------------------------------------------------------
bool
util::view::operator== (view v) const
{
return std::equal (m_begin, m_end, v.begin ());
}
//-----------------------------------------------------------------------------
std::ostream&
util::operator<< (std::ostream &os, util::view s)
@ -98,3 +115,10 @@ util::operator<< (std::ostream &os, util::view s)
return os;
}
//-----------------------------------------------------------------------------
bool
util::operator== (const char *str, view v)
{
return v == str;
}

View File

@ -38,6 +38,9 @@ namespace util {
size_t size (void) const;
const char& operator[] (size_t) const;
bool operator== (const char *restrict str) const;
bool operator== (view) const;
private:
const char *m_begin;
const char *m_end;
@ -45,5 +48,6 @@ namespace util {
std::ostream& operator<< (std::ostream&, view);
bool operator== (const char*, view);
}
#endif