2015-02-11 16:18:43 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-02-11 16:18:43 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-11 16:18:43 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-02-11 16:18:43 +11:00
|
|
|
*
|
|
|
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "view.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
util::view::view ():
|
2015-02-11 16:41:09 +11:00
|
|
|
m_begin (nullptr),
|
|
|
|
m_end (nullptr)
|
2015-02-11 16:18:43 +11:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
util::view::view (const char *str):
|
2015-02-11 16:41:09 +11:00
|
|
|
m_begin (str),
|
|
|
|
m_end (str + strlen (str))
|
2015-02-11 16:18:43 +11:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-02-11 16:41:09 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
util::view::view (const char *_begin,
|
|
|
|
const char *_end):
|
|
|
|
m_begin (_begin),
|
|
|
|
m_end (_end)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const char*
|
|
|
|
util::view::begin (void) const
|
|
|
|
{
|
|
|
|
return m_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const char*
|
|
|
|
util::view::end (void) const
|
|
|
|
{
|
|
|
|
return m_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-11 16:18:43 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
util::view::view::empty (void) const
|
|
|
|
{
|
2015-02-11 16:41:09 +11:00
|
|
|
return m_begin == nullptr ||
|
|
|
|
m_end == nullptr ||
|
|
|
|
m_begin == m_end;
|
2015-02-11 16:18:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
util::view::size (void) const
|
|
|
|
{
|
2015-02-11 16:41:09 +11:00
|
|
|
return m_end - m_begin;
|
2015-02-11 16:18:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const char&
|
|
|
|
util::view::operator[] (size_t idx) const
|
|
|
|
{
|
2015-02-11 16:41:09 +11:00
|
|
|
CHECK_LT (m_begin + idx, m_end);
|
|
|
|
return m_begin[idx];
|
2015-02-11 16:18:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-11 16:42:32 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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 ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-11 16:18:43 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::ostream&
|
|
|
|
util::operator<< (std::ostream &os, util::view s)
|
|
|
|
{
|
2015-02-11 16:41:09 +11:00
|
|
|
std::copy (s.begin (), s.end (), std::ostream_iterator<char> (os));
|
2015-02-11 16:18:43 +11:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:42:32 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
util::operator== (const char *str, view v)
|
|
|
|
{
|
|
|
|
return v == str;
|
|
|
|
}
|