/* * * Copyright (c) 1998-2002 * 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 regex_kmp.hpp * VERSION see * DESCRIPTION: Provides Knuth Morris Pratt search operations. * Note this is an internal header file included * by regex.hpp, do not include on its own. */ #ifndef BOOST_REGEX_KMP_HPP #define BOOST_REGEX_KMP_HPP #ifdef BOOST_REGEX_CONFIG_HPP #include #endif namespace boost{ namespace re_detail{ #ifdef BOOST_HAS_ABI_HEADERS # include BOOST_ABI_PREFIX #endif template struct kmp_info { unsigned int size; unsigned int len; const charT* pstr; int kmp_next[1]; }; template void kmp_free(kmp_info* pinfo, const Allocator& a) { typedef typename boost::detail::rebind_allocator::type atype; atype(a).deallocate(reinterpret_cast(pinfo), pinfo->size); } template kmp_info* kmp_compile(iterator first, iterator last, charT, Trans translate, const Allocator& a) { typedef typename boost::detail::rebind_allocator::type atype; int i, j, m; i = 0; m = static_cast(::boost::re_detail::distance(first, last)); ++m; unsigned int size = sizeof(kmp_info) + sizeof(int)*m + sizeof(charT)*m; --m; // // allocate struct and fill it in: // kmp_info* pinfo = reinterpret_cast*>(atype(a).allocate(size)); BOOST_REGEX_NOEH_ASSERT(pinfo) pinfo->size = size; pinfo->len = m; charT* p = reinterpret_cast(reinterpret_cast(pinfo) + sizeof(kmp_info) + sizeof(int)*(m+1)); pinfo->pstr = p; while(first != last) { *p = translate(*first); ++first; ++p; } *p = 0; // // finally do regular kmp compile: // j = pinfo->kmp_next[0] = -1; while (i < m) { while ((j > -1) && (pinfo->pstr[i] != pinfo->pstr[j])) j = pinfo->kmp_next[j]; ++i; ++j; if (pinfo->pstr[i] == pinfo->pstr[j]) pinfo->kmp_next[i] = pinfo->kmp_next[j]; else pinfo->kmp_next[i] = j; } return pinfo; } #ifdef BOOST_HAS_ABI_HEADERS # include BOOST_ABI_SUFFIX #endif } // namepsace re_detail } // namespace boost #endif // BOOST_REGEX_KMP_HPP