📄 cpp_regex_traits.hpp
字号:
/*
*
* Copyright (c) 2004
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE cpp_regex_traits.hpp
* VERSION see <boost/version.hpp>
* DESCRIPTION: Declares regular expression traits class cpp_regex_traits.
*/
#ifndef BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
#define BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
#include <boost/config.hpp>
#ifndef BOOST_NO_STD_LOCALE
#ifndef BOOST_RE_PAT_EXCEPT_HPP
#include <boost/regex/pattern_except.hpp>
#endif
#ifndef BOOST_REGEX_TRAITS_DEFAULTS_HPP_INCLUDED
#include <boost/regex/v4/regex_traits_defaults.hpp>
#endif
#ifdef BOOST_HAS_THREADS
#include <boost/regex/pending/static_mutex.hpp>
#endif
#ifndef BOOST_REGEX_PRIMARY_TRANSFORM
#include <boost/regex/v4/primary_transform.hpp>
#endif
#ifndef BOOST_REGEX_OBJECT_CACHE_HPP
#include <boost/regex/pending/object_cache.hpp>
#endif
#include <istream>
#include <ios>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4786)
#endif
namespace boost{
//
// forward declaration is needed by some compilers:
//
template <class charT>
class cpp_regex_traits;
namespace re_detail{
//
// class parser_buf:
// acts as a stream buffer which wraps around a pair of pointers:
//
template <class charT,
class traits = ::std::char_traits<charT> >
class parser_buf : public ::std::basic_streambuf<charT, traits>
{
typedef ::std::basic_streambuf<charT, traits> base_type;
typedef typename base_type::int_type int_type;
typedef typename base_type::char_type char_type;
typedef typename base_type::pos_type pos_type;
typedef ::std::streamsize streamsize;
typedef typename base_type::off_type off_type;
public:
parser_buf() : base_type() { setbuf(0, 0); }
const charT* getnext() { return this->gptr(); }
protected:
std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n);
typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which);
typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which);
private:
parser_buf& operator=(const parser_buf&);
parser_buf(const parser_buf&);
};
template<class charT, class traits>
std::basic_streambuf<charT, traits>*
parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
{
this->setg(s, s, s + n);
return this;
}
template<class charT, class traits>
typename parser_buf<charT, traits>::pos_type
parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
{
if(which & ::std::ios_base::out)
return pos_type(off_type(-1));
std::ptrdiff_t size = this->egptr() - this->eback();
std::ptrdiff_t pos = this->gptr() - this->eback();
charT* g = this->eback();
switch(way)
{
case ::std::ios_base::beg:
if((off < 0) || (off > size))
return pos_type(off_type(-1));
else
this->setg(g, g + off, g + size);
break;
case ::std::ios_base::end:
if((off < 0) || (off > size))
return pos_type(off_type(-1));
else
this->setg(g, g + size - off, g + size);
break;
case ::std::ios_base::cur:
{
std::ptrdiff_t newpos = pos + off;
if((newpos < 0) || (newpos > size))
return pos_type(off_type(-1));
else
this->setg(g, g + newpos, g + size);
break;
}
default: ;
}
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4244)
#endif
return static_cast<pos_type>(this->gptr() - this->eback());
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
}
template<class charT, class traits>
typename parser_buf<charT, traits>::pos_type
parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which)
{
if(which & ::std::ios_base::out)
return pos_type(off_type(-1));
off_type size = static_cast<off_type>(this->egptr() - this->eback());
charT* g = this->eback();
if(off_type(sp) <= size)
{
this->setg(g, g + off_type(sp), g + size);
}
return pos_type(off_type(-1));
}
//
// class cpp_regex_traits_base:
// acts as a container for locale and the facets we are using.
//
template <class charT>
struct cpp_regex_traits_base
{
cpp_regex_traits_base(const std::locale& l)
{ imbue(l); }
std::locale imbue(const std::locale& l);
std::locale m_locale;
std::ctype<charT> const* m_pctype;
#ifndef BOOST_NO_STD_MESSAGES
std::messages<charT> const* m_pmessages;
#endif
std::collate<charT> const* m_pcollate;
bool operator<(const cpp_regex_traits_base& b)const
{
if(m_pctype == b.m_pctype)
{
#ifndef BOOST_NO_STD_MESSAGES
if(m_pmessages == b.m_pmessages)
{
}
return m_pmessages < b.m_pmessages;
#else
return m_pcollate < b.m_pcollate;
#endif
}
return m_pctype < b.m_pctype;
}
bool operator==(const cpp_regex_traits_base& b)const
{
return (m_pctype == b.m_pctype)
#ifndef BOOST_NO_STD_MESSAGES
&& (m_pmessages == b.m_pmessages)
#endif
&& (m_pcollate == b.m_pcollate);
}
};
template <class charT>
std::locale cpp_regex_traits_base<charT>::imbue(const std::locale& l)
{
std::locale result(m_locale);
m_locale = l;
m_pctype = &BOOST_USE_FACET(std::ctype<charT>, l);
#ifndef BOOST_NO_STD_MESSAGES
m_pmessages = &BOOST_USE_FACET(std::messages<charT>, l);
#endif
m_pcollate = &BOOST_USE_FACET(std::collate<charT>, l);
return result;
}
//
// class cpp_regex_traits_char_layer:
// implements methods that require specialisation for narrow characters:
//
template <class charT>
class cpp_regex_traits_char_layer : public cpp_regex_traits_base<charT>
{
typedef std::basic_string<charT> string_type;
typedef std::map<charT, regex_constants::syntax_type> map_type;
typedef typename map_type::const_iterator map_iterator_type;
public:
cpp_regex_traits_char_layer(const std::locale& l)
: cpp_regex_traits_base<charT>(l)
{
init();
}
cpp_regex_traits_char_layer(const cpp_regex_traits_base<charT>& b)
: cpp_regex_traits_base<charT>(b)
{
init();
}
void init();
regex_constants::syntax_type syntax_type(charT c)const
{
map_iterator_type i = m_char_map.find(c);
return ((i == m_char_map.end()) ? 0 : i->second);
}
regex_constants::escape_syntax_type escape_syntax_type(charT c) const
{
map_iterator_type i = m_char_map.find(c);
if(i == m_char_map.end())
{
if(this->m_pctype->is(std::ctype_base::lower, c)) return regex_constants::escape_type_class;
if(this->m_pctype->is(std::ctype_base::upper, c)) return regex_constants::escape_type_not_class;
return 0;
}
return i->second;
}
private:
string_type get_default_message(regex_constants::syntax_type);
// TODO: use a hash table when available!
map_type m_char_map;
};
template <class charT>
void cpp_regex_traits_char_layer<charT>::init()
{
// we need to start by initialising our syntax map so we know which
// character is used for which purpose:
#ifndef BOOST_NO_STD_MESSAGES
#ifndef __IBMCPP__
typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
#else
typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
#endif
std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
if(cat_name.size())
{
cat = this->m_pmessages->open(
cat_name,
this->m_locale);
if((int)cat < 0)
{
std::string m("Unable to open message catalog: ");
std::runtime_error err(m + cat_name);
boost::re_detail::raise_runtime_error(err);
}
}
//
// if we have a valid catalog then load our messages:
//
if((int)cat >= 0)
{
try{
for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
{
string_type mss = this->m_pmessages->get(cat, 0, i, get_default_message(i));
for(typename string_type::size_type j = 0; j < mss.size(); ++j)
{
m_char_map[mss[j]] = i;
}
}
this->m_pmessages->close(cat);
}
catch(...)
{
this->m_pmessages->close(cat);
throw;
}
}
else
{
#endif
for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
{
const char* ptr = get_default_syntax(i);
while(ptr && *ptr)
{
m_char_map[this->m_pctype->widen(*ptr)] = i;
++ptr;
}
}
#ifndef BOOST_NO_STD_MESSAGES
}
#endif
}
template <class charT>
typename cpp_regex_traits_char_layer<charT>::string_type
cpp_regex_traits_char_layer<charT>::get_default_message(regex_constants::syntax_type i)
{
const char* ptr = get_default_syntax(i);
string_type result;
while(ptr && *ptr)
{
result.append(1, this->m_pctype->widen(*ptr));
++ptr;
}
return result;
}
//
// specialised version for narrow characters:
//
template <>
class BOOST_REGEX_DECL cpp_regex_traits_char_layer<char> : public cpp_regex_traits_base<char>
{
typedef std::string string_type;
public:
cpp_regex_traits_char_layer(const std::locale& l)
: cpp_regex_traits_base<char>(l)
{
init();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -