📄 chset.ipp
字号:
//////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::rep::set(impl::range<CharT> const& r)
{
rr.set(r);
}
//////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::rep::clear(impl::range<CharT> const& r)
{
rr.clear(r);
}
//////////////////////////////////
template <typename CharT>
inline typename chset<CharT>::rep::const_iterator
chset<CharT>::rep::begin() const
{
return rr.begin();
}
//////////////////////////////////
template <typename CharT>
inline typename chset<CharT>::rep::const_iterator
chset<CharT>::rep::end() const
{
return rr.end();
}
//////////////////////////////////
template <typename CharT>
inline typename chset<CharT>::rep*
chset<CharT>::rep::ref(rep* ptr)
{
ptr->uc++;
return ptr;
}
//////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::rep::deref(rep* ptr)
{
if (--ptr->uc == 0)
delete ptr;
}
//////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::rep::detach(rep*& ptr)
{
if (ptr->uc > 1)
{
rep* t = new rep(*ptr);
--ptr->uc;
ptr = t;
}
}
//////////////////////////////////
template <typename CharT>
inline void
chset<CharT>::rep::detach_clear(rep*& ptr)
{
if (ptr->uc > 1)
{
rep* t = new rep;
--ptr->uc;
ptr = t;
}
else
{
ptr->rr.clear();
}
}
//////////////////////////////////
template <typename CharT>
inline typename chset<CharT>::rep*&
chset<CharT>::rep::rep_of(chset<CharT>& set)
{
return set.ptr;
}
//////////////////////////////////
template <typename CharT>
inline typename chset<CharT>::rep const*
chset<CharT>::rep::rep_of(chset<CharT> const& set)
{
return set.ptr;
}
///////////////////////////////////////////////////////////////////////////////
//
// 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>
template <typename CharTB>
chset<CharT>::chset(CharTB const* definition)
: ptr(new rep)
{
CharTB ch = *definition++;
while (ch)
{
CharTB next = *definition++;
if (next == '-')
{
next = *definition++;
if (next == 0)
{
ptr->set(impl::range<CharT>(ch, ch));
ptr->set(impl::range<CharT>('-', '-'));
break;
}
ptr->set(impl::range<CharT>(ch, next));
}
else
{
ptr->set(impl::range<CharT>(ch, ch));
}
ch = next;
}
}
//////////////////////////////////
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);
}
///////////////////////////////////////////////////////////////////////////////
namespace impl {
struct chset_converter {
template <typename CharTA, typename CharTB>
static void
convert(chset<CharTA>& dest, chset<CharTB> const& src)
{
typedef typename std::vector<impl::range<CharTA> > vector_a;
typedef typename std::vector<impl::range<CharTB> > const vector_b;
typedef typename chset<CharTA>::rep rep_a;
typedef typename chset<CharTB>::rep rep_b;
typedef typename impl::range<CharTA> range;
rep_b const* sp = rep_b::rep_of(src);
vector_a& ss = rep_a::rep_of(dest)->rr.run;
for (typename vector_b::const_iterator iter = sp->begin();
iter != sp->end(); ++iter)
ss.push_back(range(iter->first, iter->last));
}
};
}
////////////////////////////////
template <typename CharT>
template <typename CharTB>
inline chset<CharT>::chset(chset<CharTB> const& arg)
: ptr(new rep)
{
impl::chset_converter::convert(*this, arg);
}
////////////////////////////////
template <typename CharT>
template <typename CharTB>
inline chset<CharT>&
chset<CharT>::operator=(chset<CharTB> const& rhs)
{
rep::detach_clear(ptr);
impl::chset_converter::convert(*this, rhs);
return *this;
}
///////////////////////////////////////////////////////////////////////////////
//
// 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)
{
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->clear(*iter);
return a_;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>
operator~(chset<CharT> const& a)
{
return chset<CharT>(anychar) - a;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>
operator&(chset<CharT> const& a, chset<CharT> const& b)
{
return a - ~b;
}
//////////////////////////////////
template <typename CharT>
inline chset<CharT>
operator^(chset<CharT> const& a, chset<CharT> const& b)
{
return a - b | b - a;
}
///////////////////////////////////////////////////////////////////////////////
//
// range <--> chset free operators implementation
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {
//////////////////////////////////
template <typename CharT>
inline CharT
decr(CharT v)
{
return v == std::numeric_limits<CharT>::min() ? v : v-1;
}
//////////////////////////////////
template <typename CharT>
inline CharT
incr(CharT v)
{
return v == std::numeric_limits<CharT>::max() ? v : v+1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -