chset.ipp
来自「著名的Parser库Spirit在VC6上的Port」· IPP 代码 · 共 951 行 · 第 1/2 页
IPP
951 行
/*=============================================================================
Character set implementation
Spirit V1.3.1
Copyright (c) 2001, Joel de Guzman
This software is provided 'as-is', without any express or implied
warranty. In no event will the copyright holder be held liable for
any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Acknowledgements:
Special thanks to Dan Nuffer, John (EBo) David, Chris Uzdavinis,
and Doug Gregor. These people are most instrumental in steering
Spirit in the right direction.
Special thanks also to people who have contributed to the code base
and sample code, ported Spirit to various platforms and compilers,
gave suggestions, reported and provided bug fixes. Alexander
Hirner, Andy Elvey, Bogdan Kushnir, Brett Calcott, Bruce Florman,
Changzhe Han, Colin McPhail, Hakki Dogusan, Jan Bares, Joseph
Smith, Martijn W. van der Lee, Raghavendra Satish, Remi Delcos, Tom
Spilman, Vladimir Prus, W. Scott Dillman, David A. Greene, Bob
Bailey, Hartmut Kaiser.
Finally special thanks also to people who gave feedback and
valuable comments, particularly members of Spirit's Source Forge
mailing list and boost.org.
URL: http://spirit.sourceforge.net/
=============================================================================*/
#ifndef SPIRIT_CHSET_IPP
#define SPIRIT_CHSET_IPP
///////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <vector>
#include <algorithm>
#include <functional>
#include "boost/limits.hpp"
#include "boost/spirit/MSVC/chset.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
namespace impl {
///////////////////////////////////////////////////////////////////////////////
struct chset_converter;
///////////////////////////////////////////////////////////////////////////////
//
// range class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline range<CharT>::range(CharT first_, CharT last_)
: first(first_), last(last_) {}
//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::is_valid() const
{
return first <= last;
}
//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::includes(range const& r) const
{
return (first <= r.first) && (last >= r.last);
}
//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::includes(CharT v) const
{
return (first <= v) && (last >= v);
}
//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::is_adjacent(range const& r) const
{
CharT decr_first = first == std::numeric_limits<CharT>::min()
? first : first-1;
CharT incr_last = last == std::numeric_limits<CharT>::max()
? last : last+1;
return ((decr_first <= r.first) && (incr_last >= r.first))
|| ((decr_first <= r.last) && (incr_last >= r.last));
}
//////////////////////////////////
template <typename CharT>
inline void
range<CharT>::merge(range const& r)
{
first = std::min(first, r.first);
last = std::max(last, r.last);
}
//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::operator < (range const& r) const
{
return first < r.first;
}
//////////////////////////////////
template <typename CharT>
inline bool
range<CharT>::operator < (CharT v) const
{
return first < v;
}
template <typename CharT>
struct range_char_compare : public std::binary_function<range<CharT>,CharT,bool>
{
bool operator()(const range<CharT>& x, const CharT y) const
{
return x.first < y;
}
};
///////////////////////////////////////////////////////////////////////////////
//
// range_run class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline bool
range_run<CharT>::test(CharT v) const
{
if (!run.empty())
{
const_iterator iter = std::lower_bound(run.begin(), run.end(), v,
range_char_compare<CharT>());
if (iter != run.end() && iter->includes(v))
return true;
if (iter != run.begin())
return (--iter)->includes(v);
}
return false;
}
//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::merge(iterator iter, range<CharT> const& r)
{
iter->merge(r);
iterator i = iter + 1;
while (i != run.end() && iter->is_adjacent(*i))
iter->merge(*i++);
run.erase(iter+1, i);
}
//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::set(range<CharT> const& r)
{
assert(r.is_valid());
if (!run.empty())
{
iterator iter = std::lower_bound(run.begin(), run.end(), r);
if (iter != run.end() && iter->includes(r) ||
((iter != run.begin()) && (iter - 1)->includes(r)))
return;
if (iter != run.begin() && (iter - 1)->is_adjacent(r))
merge(--iter, r);
else if (iter != run.end() && iter->is_adjacent(r))
merge(iter, r);
else
run.insert(iter, r);
}
else
{
run.push_back(r);
}
}
//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::clear(range<CharT> const& r)
{
assert(r.is_valid());
if (!run.empty())
{
iterator iter = std::lower_bound(run.begin(), run.end(), r);
iterator left_iter;
if ((iter != run.begin()) &&
(left_iter = (iter - 1))->includes(r.first))
if (left_iter->last > r.last)
{
CharT save_last = left_iter->last;
left_iter->last = r.first-1;
run.insert(iter, range<CharT>(r.last+1, save_last));
return;
}
else
{
left_iter->last = r.first-1;
}
iterator i = iter;
while (i != run.end() && r.includes(*i))
i++;
if (i != run.end() && i->includes(r.last))
i->first = r.last+1;
run.erase(iter, i);
}
}
//////////////////////////////////
template <typename CharT>
void
range_run<CharT>::clear()
{
run.clear();
}
//////////////////////////////////
template <typename CharT>
inline typename range_run<CharT>::const_iterator
range_run<CharT>::begin() const
{
return run.begin();
}
//////////////////////////////////
template <typename CharT>
inline typename range_run<CharT>::const_iterator
range_run<CharT>::end() const
{
return run.end();
}
///////////////////////////////////////////////////////////////////////////////
//
// utility functions
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline range<CharT> const&
full_range()
{
static range<CharT> full(std::numeric_limits<CharT>::min(),
std::numeric_limits<CharT>::max());
return full;
}
///////////////////////////////////////////////////////////////////////////////
} // namespace spirit::impl
///////////////////////////////////////////////////////////////////////////////
//
// chset class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline chset<CharT>::chset()
: ptr(new rep)
{
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>::chset(chset const& arg)
: char_parser<chset<CharT> >()
, ptr(rep::ref(arg.ptr))
{
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>::chset(CharT arg)
: ptr(new rep)
{
ptr->set(impl::range<CharT>(arg, arg));
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>::chset(anychar_)
: ptr(new rep)
{
ptr->set(impl::full_range<CharT>());
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>::chset(nothing_)
: ptr(new rep)
{
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>::chset(chlit<CharT> const& arg)
: ptr(new rep)
{
ptr->set(impl::range<CharT>(arg.ch, arg.ch));
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>::chset(range<CharT> const& arg)
: ptr(new rep)
{
ptr->set(impl::range<CharT>(arg.first, arg.last));
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>::~chset()
{
rep::deref(ptr);
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>&
chset<CharT>::operator=(chset const& rhs)
{
if (this != &rhs)
{
rep* tp = rep::ref(rhs.ptr);
rep::deref(ptr);
ptr = tp;
}
return *this;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>&
chset<CharT>::operator=(CharT rhs)
{
rep::detach_clear(ptr);
ptr->set(impl::range<CharT>(rhs, rhs));
return *this;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>&
chset<CharT>::operator=(anychar_ rhs)
{
rep::detach_clear(ptr);
ptr->set(impl::full_range<CharT>());
return *this;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>&
chset<CharT>::operator=(nothing_ rhs)
{
rep::detach_clear(ptr);
return *this;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>&
chset<CharT>::operator=(chlit<CharT> const& rhs)
{
rep::detach_clear(ptr);
ptr->set(impl::range<CharT>(rhs.ch, rhs.ch));
return *this;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>&
chset<CharT>::operator=(range<CharT> const& rhs)
{
rep::detach_clear(ptr);
ptr->set(impl::range<CharT>(rhs.first, rhs.last));
return *this;
}
////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::set(range<CharT> const& arg)
{
rep::detach(ptr);
ptr->set(impl::range<CharT>(arg.first, arg.last));
}
////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::add(CharT ch)
{
ptr->set(impl::range<CharT>(ch, ch));
}
//////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::clear(range<CharT> const& arg)
{
rep::detach(ptr);
ptr->clear(impl::range<CharT>(arg.first, arg.last));
}
////////////////////////////////
template <typename CharT>
inline bool
chset<CharT>::test(CharT ch) const
{
return ptr->test(ch);
}
///////////////////////////////////////////////////////////////////////////////
//
// chset free operators implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT>
inline chset<CharT>
operator|(chset<CharT> const& a, chset<CharT> const& b)
{
typedef typename chset<CharT>::rep rep;
typedef typename rep::const_iterator citerator;
chset<CharT> a_(a);
rep*& ap = rep::rep_of(a_);
rep const* bp = rep::rep_of(b);
rep::detach(ap);
for (citerator iter = bp->begin(); iter != bp->end(); ++iter)
ap->set(*iter);
return a_;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>
operator-(chset<CharT> const& a, chset<CharT> const& b)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?