summaryrefslogtreecommitdiff
path: root/boost/detail
diff options
context:
space:
mode:
authorAnas Nashif <anas.nashif@intel.com>2013-08-26 08:15:55 -0400
committerAnas Nashif <anas.nashif@intel.com>2013-08-26 08:15:55 -0400
commitbb4dd8289b351fae6b55e303f189127a394a1edd (patch)
tree77c9c35a31b1459dd7988c2448e797d142530c41 /boost/detail
parent1a78a62555be32868418fe52f8e330c9d0f95d5a (diff)
downloadboost-bb4dd8289b351fae6b55e303f189127a394a1edd.tar.gz
boost-bb4dd8289b351fae6b55e303f189127a394a1edd.tar.bz2
boost-bb4dd8289b351fae6b55e303f189127a394a1edd.zip
Imported Upstream version 1.51.0upstream/1.51.0
Diffstat (limited to 'boost/detail')
-rw-r--r--boost/detail/container_fwd.hpp15
-rw-r--r--boost/detail/scoped_enum_emulation.hpp349
-rw-r--r--boost/detail/utf8_codecvt_facet.ipp285
3 files changed, 609 insertions, 40 deletions
diff --git a/boost/detail/container_fwd.hpp b/boost/detail/container_fwd.hpp
index 7df7ccbb94..ef17498063 100644
--- a/boost/detail/container_fwd.hpp
+++ b/boost/detail/container_fwd.hpp
@@ -21,10 +21,17 @@
// Define BOOST_DETAIL_NO_CONTAINER_FWD if you don't want this header to //
// forward declare standard containers. //
// //
+// BOOST_DETAIL_CONTAINER_FWD to make it foward declare containers even if it //
+// normally doesn't. //
+// //
+// BOOST_DETAIL_NO_CONTAINER_FWD overrides BOOST_DETAIL_CONTAINER_FWD. //
+// //
////////////////////////////////////////////////////////////////////////////////
#if !defined(BOOST_DETAIL_NO_CONTAINER_FWD)
-# if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
+# if defined(BOOST_DETAIL_CONTAINER_FWD)
+ // Force forward declarations.
+# elif defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
// STLport
# define BOOST_DETAIL_NO_CONTAINER_FWD
# elif defined(__LIBCOMO__)
@@ -76,11 +83,6 @@
# endif
#endif
-// BOOST_DETAIL_TEST_* macros are for testing only
-// and shouldn't be relied upon. But you can use
-// BOOST_DETAIL_NO_CONTAINER_FWD to prevent forward
-// declaration of containers.
-
#if !defined(BOOST_DETAIL_TEST_CONFIG_ONLY)
#if defined(BOOST_DETAIL_NO_CONTAINER_FWD) && \
@@ -118,6 +120,7 @@ namespace std
template <class charT, class traits, class Allocator> class basic_string;
#if BOOST_WORKAROUND(__GNUC__, < 3) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
+
template <class charT> struct string_char_traits;
#else
template <class charT> struct char_traits;
diff --git a/boost/detail/scoped_enum_emulation.hpp b/boost/detail/scoped_enum_emulation.hpp
index e695a20857..80394cf433 100644
--- a/boost/detail/scoped_enum_emulation.hpp
+++ b/boost/detail/scoped_enum_emulation.hpp
@@ -1,56 +1,337 @@
// scoped_enum_emulation.hpp ---------------------------------------------------------//
// Copyright Beman Dawes, 2009
+// Copyright (C) 2011-2012 Vicente J. Botet Escriba
+// Copyright (C) 2012 Anthony Williams
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
-// Generates C++0x scoped enums if the feature is present, otherwise emulates C++0x
-// scoped enums with C++03 namespaces and enums. The Boost.Config BOOST_NO_SCOPED_ENUMS
-// macro is used to detect feature support.
-//
-// See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf for a
-// description of the scoped enum feature. Note that the committee changed the name
-// from strongly typed enum to scoped enum.
-//
-// Caution: only the syntax is emulated; the semantics are not emulated and
-// the syntax emulation doesn't include being able to specify the underlying
-// representation type.
-//
-// The emulation is via struct rather than namespace to allow use within classes.
-// Thanks to Andrey Semashev for pointing that out.
-//
-// Helpful comments and suggestions were also made by Kjell Elster, Phil Endecott,
-// Joel Falcou, Mathias Gaunard, Felipe Magno de Almeida, Matt Calabrese, Vincente
-// Botet, and Daniel James.
-//
-// Sample usage:
-//
-// BOOST_SCOPED_ENUM_START(algae) { green, red, cyan }; BOOST_SCOPED_ENUM_END
-// ...
-// BOOST_SCOPED_ENUM(algae) sample( algae::red );
-// void foo( BOOST_SCOPED_ENUM(algae) color );
-// ...
-// sample = algae::green;
-// foo( algae::cyan );
+/*
+[section:scoped_enums Scoped Enums]
+
+Generates C++0x scoped enums if the feature is present, otherwise emulates C++0x
+scoped enums with C++03 namespaces and enums. The Boost.Config BOOST_NO_SCOPED_ENUMS
+macro is used to detect feature support.
+
+See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf for a
+description of the scoped enum feature. Note that the committee changed the name
+from strongly typed enum to scoped enum.
+
+Some of the enumerations defined in the standard library are scoped enums.
+
+ enum class future_errc
+ {
+ broken_promise,
+ future_already_retrieved,
+ promise_already_satisfied,
+ no_state
+ };
+
+On compilers that don't support them, the library provides two emulations:
+
+[heading Strict]
+
+* Able to specify the underlying type.
+* explicit conversion to/from underlying type.
+* The wrapper is not a C++03 enum type.
+
+The user can declare declare these types as
+
+ BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc)
+ {
+ broken_promise,
+ future_already_retrieved,
+ promise_already_satisfied,
+ no_state
+ }
+ BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
+
+These macros allows to use 'future_errc' in almost all the cases as an scoped enum.
+
+ future_errc err = future_errc::no_state;
+
+There are however some limitations:
+
+* The type is not a C++ enum, so 'is_enum<future_errc>' will be false_type.
+* The emulated scoped enum can not be used in switch nor in template arguments. For these cases the user needs to use some macros.
+
+Instead of
+
+ switch (ev)
+ {
+ case future_errc::broken_promise:
+ // ...
+
+use
+
+ switch (boost::native_value(ev))
+ {
+ case future_errc::broken_promise:
+
+And instead of
+
+ #ifdef BOOST_NO_SCOPED_ENUMS
+ template <>
+ struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc> : public true_type { };
+ #endif
+
+use
+
+ #ifdef BOOST_NO_SCOPED_ENUMS
+ template <>
+ struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc::enum_type > : public true_type { };
+ #endif
+
+
+Sample usage:
+
+ BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(algae, char) { green, red, cyan }; BOOST_SCOPED_ENUM_DECLARE_END(algae)
+ ...
+ algae sample( algae::red );
+ void foo( algae color );
+ ...
+ sample = algae::green;
+ foo( algae::cyan );
+
+ Light
+ Caution: only the syntax is emulated; the semantics are not emulated and
+ the syntax emulation doesn't include being able to specify the underlying
+ representation type.
+
+ The literal scoped emulation is via struct rather than namespace to allow use within classes.
+ Thanks to Andrey Semashev for pointing that out.
+ However the type is an real C++03 enum and so convertible implicitly to an int.
+
+ Sample usage:
+
+ BOOST_SCOPED_ENUM_START(algae) { green, red, cyan }; BOOST_SCOPED_ENUM_END
+ ...
+ BOOST_SCOPED_ENUM(algae) sample( algae::red );
+ void foo( BOOST_SCOPED_ENUM(algae) color );
+ ...
+ sample = algae::green;
+ foo( algae::cyan );
+
+ Helpful comments and suggestions were also made by Kjell Elster, Phil Endecott,
+ Joel Falcou, Mathias Gaunard, Felipe Magno de Almeida, Matt Calabrese, Vicente
+ Botet, and Daniel James.
+
+[endsect]
+*/
+
#ifndef BOOST_SCOPED_ENUM_EMULATION_HPP
#define BOOST_SCOPED_ENUM_EMULATION_HPP
#include <boost/config.hpp>
+#include <boost/detail/workaround.hpp>
+
+namespace boost
+{
#ifdef BOOST_NO_SCOPED_ENUMS
+ /**
+ * Meta-function to get the underlying type of a scoped enum.
+ *
+ * Requires EnumType must be an enum type or the emulation of a scoped enum
+ */
+ template <typename EnumType>
+ struct underlying_type
+ {
+ /**
+ * The member typedef type names the underlying type of EnumType. It is EnumType::underlying_type when the EnumType is an emulated scoped enum,
+ * std::underlying_type<EnumType>::type when the standard library std::underlying_type is provided.
+ *
+ * The user will need to specialize it when the compiler supports scoped enums but don't provides std::underlying_type.
+ */
+ typedef typename EnumType::underlying_type type;
+ };
+
+ /**
+ * Meta-function to get the native enum type associated to an enum class or its emulation.
+ */
+ template <typename EnumType>
+ struct native_type
+ {
+ /**
+ * The member typedef type names the native enum type associated to the scoped enum,
+ * which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum.
+ */
+ typedef typename EnumType::enum_type type;
+ };
+
+ /**
+ * Casts a scoped enum to its underlying type.
+ *
+ * This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type.
+ * @param v A scoped enum.
+ * @returns The underlying type.
+ * @throws No-throws.
+ */
+ template <typename UnderlyingType, typename EnumType>
+ UnderlyingType underlying_cast(EnumType v)
+ {
+ return v.get_underlying_value_();
+ }
+
+ /**
+ * Casts a scoped enum to its native enum type.
+ *
+ * This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can.
+ *
+ * EnumType the scoped enum type
+ *
+ * @param v A scoped enum.
+ * @returns The native enum value.
+ * @throws No-throws.
+ */
+ template <typename EnumType>
+ inline
+ typename EnumType::enum_type native_value(EnumType e)
+ {
+ return e.native_value_();
+ }
+
+#else // BOOST_NO_SCOPED_ENUMS
-# define BOOST_SCOPED_ENUM_START(name) struct name { enum enum_type
-# define BOOST_SCOPED_ENUM_END };
-# define BOOST_SCOPED_ENUM(name) name::enum_type
+ template <typename EnumType>
+ struct underlying_type
+ {
+ //typedef typename std::underlying_type<EnumType>::type type;
+ };
+
+ template <typename EnumType>
+ struct native_type
+ {
+ typedef EnumType type;
+ };
+
+ template <typename UnderlyingType, typename EnumType>
+ UnderlyingType underlying_cast(EnumType v)
+ {
+ return static_cast<UnderlyingType>(v);
+ }
+
+ template <typename EnumType>
+ inline
+ EnumType native_value(EnumType e)
+ {
+ return e;
+ }
+
+#endif
+}
+
+
+#ifdef BOOST_NO_SCOPED_ENUMS
+
+#ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS
+
+#define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
+ explicit operator underlying_type() const { return get_underlying_value_(); }
#else
-# define BOOST_SCOPED_ENUM_START(name) enum class name
-# define BOOST_SCOPED_ENUM_END
-# define BOOST_SCOPED_ENUM(name) name
+#define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR
#endif
+/**
+ * Start a declaration of a scoped enum.
+ *
+ * @param EnumType The new scoped enum.
+ * @param UnderlyingType The underlying type.
+ */
+#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \
+ struct EnumType { \
+ typedef UnderlyingType underlying_type; \
+ EnumType() BOOST_NOEXCEPT {} \
+ explicit EnumType(underlying_type v) : v_(v) {} \
+ underlying_type get_underlying_value_() const { return v_; } \
+ BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
+ private: \
+ underlying_type v_; \
+ typedef EnumType self_type; \
+ public: \
+ enum enum_type
+
+#define BOOST_SCOPED_ENUM_DECLARE_END2() \
+ enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \
+ operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); } \
+ friend bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
+ friend bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
+ friend bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
+ friend bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
+ friend bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
+ friend bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
+ friend bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
+ friend bool operator <(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
+ friend bool operator <(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
+ friend bool operator <=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
+ friend bool operator <=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
+ friend bool operator <=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
+ friend bool operator >(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
+ friend bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
+ friend bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
+ friend bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
+ friend bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
+ friend bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
+ };
+
+#define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \
+ ; \
+ EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {} \
+ BOOST_SCOPED_ENUM_DECLARE_END2()
+
+/**
+ * Starts a declaration of a scoped enum with the default int underlying type.
+ *
+ * @param EnumType The new scoped enum.
+ */
+#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
+ BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int)
+
+/**
+ * Name of the native enum type.
+ *
+ * @param NT The new scoped enum.
+ */
+#define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
+/**
+ * Forward declares an scoped enum.
+ *
+ * @param NT The scoped enum.
+ */
+#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
+
+#else // BOOST_NO_SCOPED_ENUMS
+
+#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType:UnderlyingType
+#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
+#define BOOST_SCOPED_ENUM_DECLARE_END2()
+#define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ;
+
+#define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType
+#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
+
+#endif // BOOST_NO_SCOPED_ENUMS
+
+#define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name)
+#define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2()
+#define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name)
+
+//#ifdef BOOST_NO_SCOPED_ENUMS
+//
+//# define BOOST_SCOPED_ENUM_START(name) struct name { enum enum_type
+//# define BOOST_SCOPED_ENUM_END };
+//# define BOOST_SCOPED_ENUM(name) name::enum_type
+//
+//#else
+//
+//# define BOOST_SCOPED_ENUM_START(name) enum class name
+//# define BOOST_SCOPED_ENUM_END
+//# define BOOST_SCOPED_ENUM(name) name
+//
+//#endif
#endif // BOOST_SCOPED_ENUM_EMULATION_HPP
diff --git a/boost/detail/utf8_codecvt_facet.ipp b/boost/detail/utf8_codecvt_facet.ipp
new file mode 100644
index 0000000000..064fdaf58e
--- /dev/null
+++ b/boost/detail/utf8_codecvt_facet.ipp
@@ -0,0 +1,285 @@
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// utf8_codecvt_facet.ipp
+
+// Copyright (c) 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
+// Andrew Lumsdaine, Indiana University (lums@osl.iu.edu).
+// Use, modification and distribution is 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)
+
+// Please see the comments in <boost/detail/utf8_codecvt_facet.hpp> to
+// learn how this file should be used.
+
+#include <boost/detail/utf8_codecvt_facet.hpp>
+
+#include <cstdlib> // for multi-byte converson routines
+#include <cassert>
+
+#include <boost/limits.hpp>
+#include <boost/config.hpp>
+
+// If we don't have wstring, then Unicode support
+// is not available anyway, so we don't need to even
+// compiler this file. This also fixes the problem
+// with mingw, which can compile this file, but will
+// generate link error when building DLL.
+#ifndef BOOST_NO_STD_WSTRING
+
+BOOST_UTF8_BEGIN_NAMESPACE
+
+/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
+// implementation for wchar_t
+
+// Translate incoming UTF-8 into UCS-4
+std::codecvt_base::result utf8_codecvt_facet::do_in(
+ std::mbstate_t& /*state*/,
+ const char * from,
+ const char * from_end,
+ const char * & from_next,
+ wchar_t * to,
+ wchar_t * to_end,
+ wchar_t * & to_next
+) const {
+ // Basic algorithm: The first octet determines how many
+ // octets total make up the UCS-4 character. The remaining
+ // "continuing octets" all begin with "10". To convert, subtract
+ // the amount that specifies the number of octets from the first
+ // octet. Subtract 0x80 (1000 0000) from each continuing octet,
+ // then mash the whole lot together. Note that each continuing
+ // octet only uses 6 bits as unique values, so only shift by
+ // multiples of 6 to combine.
+ while (from != from_end && to != to_end) {
+
+ // Error checking on the first octet
+ if (invalid_leading_octet(*from)){
+ from_next = from;
+ to_next = to;
+ return std::codecvt_base::error;
+ }
+
+ // The first octet is adjusted by a value dependent upon
+ // the number of "continuing octets" encoding the character
+ const int cont_octet_count = get_cont_octet_count(*from);
+ const wchar_t octet1_modifier_table[] = {
+ 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
+ };
+
+ // The unsigned char conversion is necessary in case char is
+ // signed (I learned this the hard way)
+ wchar_t ucs_result =
+ (unsigned char)(*from++) - octet1_modifier_table[cont_octet_count];
+
+ // Invariants :
+ // 1) At the start of the loop, 'i' continuing characters have been
+ // processed
+ // 2) *from points to the next continuing character to be processed.
+ int i = 0;
+ while(i != cont_octet_count && from != from_end) {
+
+ // Error checking on continuing characters
+ if (invalid_continuing_octet(*from)) {
+ from_next = from;
+ to_next = to;
+ return std::codecvt_base::error;
+ }
+
+ ucs_result *= (1 << 6);
+
+ // each continuing character has an extra (10xxxxxx)b attached to
+ // it that must be removed.
+ ucs_result += (unsigned char)(*from++) - 0x80;
+ ++i;
+ }
+
+ // If the buffer ends with an incomplete unicode character...
+ if (from == from_end && i != cont_octet_count) {
+ // rewind "from" to before the current character translation
+ from_next = from - (i+1);
+ to_next = to;
+ return std::codecvt_base::partial;
+ }
+ *to++ = ucs_result;
+ }
+ from_next = from;
+ to_next = to;
+
+ // Were we done converting or did we run out of destination space?
+ if(from == from_end) return std::codecvt_base::ok;
+ else return std::codecvt_base::partial;
+}
+
+std::codecvt_base::result utf8_codecvt_facet::do_out(
+ std::mbstate_t& /*state*/,
+ const wchar_t * from,
+ const wchar_t * from_end,
+ const wchar_t * & from_next,
+ char * to,
+ char * to_end,
+ char * & to_next
+) const
+{
+ // RG - consider merging this table with the other one
+ const wchar_t octet1_modifier_table[] = {
+ 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
+ };
+
+ wchar_t max_wchar = (std::numeric_limits<wchar_t>::max)();
+ while (from != from_end && to != to_end) {
+
+ // Check for invalid UCS-4 character
+ if (*from > max_wchar) {
+ from_next = from;
+ to_next = to;
+ return std::codecvt_base::error;
+ }
+
+ int cont_octet_count = get_cont_octet_out_count(*from);
+
+ // RG - comment this formula better
+ int shift_exponent = (cont_octet_count) * 6;
+
+ // Process the first character
+ *to++ = static_cast<char>(octet1_modifier_table[cont_octet_count] +
+ (unsigned char)(*from / (1 << shift_exponent)));
+
+ // Process the continuation characters
+ // Invariants: At the start of the loop:
+ // 1) 'i' continuing octets have been generated
+ // 2) '*to' points to the next location to place an octet
+ // 3) shift_exponent is 6 more than needed for the next octet
+ int i = 0;
+ while (i != cont_octet_count && to != to_end) {
+ shift_exponent -= 6;
+ *to++ = static_cast<char>(0x80 + ((*from / (1 << shift_exponent)) % (1 << 6)));
+ ++i;
+ }
+ // If we filled up the out buffer before encoding the character
+ if(to == to_end && i != cont_octet_count) {
+ from_next = from;
+ to_next = to - (i+1);
+ return std::codecvt_base::partial;
+ }
+ ++from;
+ }
+ from_next = from;
+ to_next = to;
+ // Were we done or did we run out of destination space
+ if(from == from_end) return std::codecvt_base::ok;
+ else return std::codecvt_base::partial;
+}
+
+// How many char objects can I process to get <= max_limit
+// wchar_t objects?
+int utf8_codecvt_facet::do_length(
+ BOOST_CODECVT_DO_LENGTH_CONST std::mbstate_t &,
+ const char * from,
+ const char * from_end,
+ std::size_t max_limit
+#if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
+) const throw()
+#else
+) const
+#endif
+{
+ // RG - this code is confusing! I need a better way to express it.
+ // and test cases.
+
+ // Invariants:
+ // 1) last_octet_count has the size of the last measured character
+ // 2) char_count holds the number of characters shown to fit
+ // within the bounds so far (no greater than max_limit)
+ // 3) from_next points to the octet 'last_octet_count' before the
+ // last measured character.
+ int last_octet_count=0;
+ std::size_t char_count = 0;
+ const char* from_next = from;
+ // Use "<" because the buffer may represent incomplete characters
+ while (from_next+last_octet_count <= from_end && char_count <= max_limit) {
+ from_next += last_octet_count;
+ last_octet_count = (get_octet_count(*from_next));
+ ++char_count;
+ }
+ return static_cast<int>(from_next-from_end);
+}
+
+unsigned int utf8_codecvt_facet::get_octet_count(
+ unsigned char lead_octet
+){
+ // if the 0-bit (MSB) is 0, then 1 character
+ if (lead_octet <= 0x7f) return 1;
+
+ // Otherwise the count number of consecutive 1 bits starting at MSB
+// assert(0xc0 <= lead_octet && lead_octet <= 0xfd);
+
+ if (0xc0 <= lead_octet && lead_octet <= 0xdf) return 2;
+ else if (0xe0 <= lead_octet && lead_octet <= 0xef) return 3;
+ else if (0xf0 <= lead_octet && lead_octet <= 0xf7) return 4;
+ else if (0xf8 <= lead_octet && lead_octet <= 0xfb) return 5;
+ else return 6;
+}
+BOOST_UTF8_END_NAMESPACE
+
+namespace {
+template<std::size_t s>
+int get_cont_octet_out_count_impl(wchar_t word){
+ if (word < 0x80) {
+ return 0;
+ }
+ if (word < 0x800) {
+ return 1;
+ }
+ return 2;
+}
+
+template<>
+int get_cont_octet_out_count_impl<4>(wchar_t word){
+ if (word < 0x80) {
+ return 0;
+ }
+ if (word < 0x800) {
+ return 1;
+ }
+
+ // Note that the following code will generate warnings on some platforms
+ // where wchar_t is defined as UCS2. The warnings are superfluous as the
+ // specialization is never instantitiated with such compilers, but this
+ // can cause problems if warnings are being treated as errors, so we guard
+ // against that. Including <boost/detail/utf8_codecvt_facet.hpp> as we do
+ // should be enough to get WCHAR_MAX defined.
+#if !defined(WCHAR_MAX)
+# error WCHAR_MAX not defined!
+#endif
+ // cope with VC++ 7.1 or earlier having invalid WCHAR_MAX
+#if defined(_MSC_VER) && _MSC_VER <= 1310 // 7.1 or earlier
+ return 2;
+#elif WCHAR_MAX > 0x10000
+
+ if (word < 0x10000) {
+ return 2;
+ }
+ if (word < 0x200000) {
+ return 3;
+ }
+ if (word < 0x4000000) {
+ return 4;
+ }
+ return 5;
+
+#else
+ return 2;
+#endif
+}
+
+} // namespace anonymous
+
+BOOST_UTF8_BEGIN_NAMESPACE
+// How many "continuing octets" will be needed for this word
+// == total octets - 1.
+int utf8_codecvt_facet::get_cont_octet_out_count(
+ wchar_t word
+) const {
+ return get_cont_octet_out_count_impl<sizeof(wchar_t)>(word);
+}
+BOOST_UTF8_END_NAMESPACE
+
+#endif