blob: d6571f76e7e8024e49a823ed2fbb51091e07c458 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright Gennadiy Rozental 2004-2008.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision: 49312 $
11//
12// Description : generic char traits class; wraps std::char_traits
13// ***************************************************************************
14
15#ifndef BOOST_TEST_BCS_CHAR_TRAITS_HPP_071894GER
16#define BOOST_TEST_BCS_CHAR_TRAITS_HPP_071894GER
17
18// Boost
19#include <ndnboost/config.hpp>
20#include <ndnboost/detail/workaround.hpp>
21#include <ndnboost/test/detail/config.hpp>
22#include <ndnboost/type_traits/add_const.hpp>
23
24// STL
25#include <string> // std::char_traits
26#include <cstddef> // std::size_t
27
28#include <ndnboost/test/detail/suppress_warnings.hpp>
29
30//____________________________________________________________________________//
31
32namespace ndnboost {
33
34namespace unit_test {
35
36namespace ut_detail {
37
38template<typename CharT> struct bcs_base_char { typedef CharT type; };
39
40template<> struct bcs_base_char<char const> { typedef char type; };
41template<> struct bcs_base_char<unsigned char> { typedef char type; };
42#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
43template<> struct bcs_base_char<unsigned char const> { typedef char type; };
44#endif
45
46template<> struct bcs_base_char<wchar_t const> { typedef wchar_t type; };
47
48// ************************************************************************** //
49// ************** bcs_char_traits ************** //
50// ************************************************************************** //
51
52template<typename CharT>
53struct bcs_char_traits_impl
54{
55#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
56 typedef CharT const const_char;
57#else
58 typedef typename ndnboost::add_const<CharT>::type const_char;
59#endif
60 static bool eq( CharT c1, CharT c2 )
61 {
62 return c1 == c2;
63 }
64 static bool lt( CharT c1, CharT c2 )
65 {
66 return c1 < c2;
67 }
68
69 static int compare( const_char* cstr1, const_char* cstr2, std::size_t n )
70 {
71 while( n > 0 ) {
72 if( !eq( *cstr1, *cstr2 ) )
73 return lt( *cstr1, *cstr2 ) ? -1 : 1;
74 ++cstr1;
75 ++cstr2;
76 --n;
77 }
78
79 return 0;
80 }
81
82 static std::size_t length( const_char* cstr )
83 {
84 const_char null_char = CharT();
85
86 const_char* ptr = cstr;
87 while( !eq( *ptr, null_char ) )
88 ++ptr;
89
90 return ptr - cstr;
91 }
92
93 static const_char* find( const_char* s, std::size_t n, CharT c )
94 {
95 while( n > 0 ) {
96 if( eq( *s, c ) )
97 return s;
98
99 ++s;
100 --n;
101 }
102 return 0;
103 }
104};
105
106#ifdef BOOST_CLASSIC_IOSTREAMS
107template<typename CharT>
108struct char_traits_with_find : std::string_char_traits<CharT> {
109 static CharT const* find( CharT const* s, std::size_t n, CharT c )
110 {
111 while( n > 0 ) {
112 if( eq( *s, c ) )
113 return s;
114
115 ++s;
116 --n;
117 }
118 return 0;
119 }
120};
121
122template<> struct bcs_char_traits_impl<char> : char_traits_with_find<char> {};
123template<> struct bcs_char_traits_impl<wchar_t> : char_traits_with_find<wchar_t> {};
124#else
125template<> struct bcs_char_traits_impl<char> : std::char_traits<char> {};
126template<> struct bcs_char_traits_impl<wchar_t> : std::char_traits<wchar_t> {};
127#endif
128
129template<typename CharT>
130class bcs_char_traits : public bcs_char_traits_impl<CharT> {
131 typedef typename ut_detail::bcs_base_char<CharT>::type the_base_char;
132public:
133#ifdef BOOST_CLASSIC_IOSTREAMS
134 typedef std::basic_string<the_base_char, std::string_char_traits<the_base_char> > std_string;
135#else
136 typedef std::basic_string<the_base_char, std::char_traits<the_base_char> > std_string;
137#endif
138};
139
140} // namespace ut_detail
141
142} // namespace unit_test
143
144} // namespace ndnboost
145
146//____________________________________________________________________________//
147
148#include <ndnboost/test/detail/enable_warnings.hpp>
149
150#endif // BOOST_TEST_BCS_CHAR_TRAITS_HPP_071894GER