2 * Copyright Andrey Semashev 2007 - 2014.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file string_literal.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of a constant string literal wrapper.
15 #ifndef BOOST_LOG_UTILITY_STRING_LITERAL_HPP_INCLUDED_
16 #define BOOST_LOG_UTILITY_STRING_LITERAL_HPP_INCLUDED_
21 #include <ios> // std::streamsize
24 #include <boost/operators.hpp>
25 #include <boost/throw_exception.hpp>
26 #include <boost/type_traits/is_same.hpp>
27 #include <boost/utility/enable_if.hpp>
28 #include <boost/log/detail/config.hpp>
29 #include <boost/log/utility/string_literal_fwd.hpp>
30 #include <boost/log/detail/header.hpp>
32 #ifdef BOOST_HAS_PRAGMA_ONCE
38 BOOST_LOG_OPEN_NAMESPACE
41 * \brief String literal wrapper
43 * The \c basic_string_literal is a thin wrapper around a constant string literal.
44 * It provides interface similar to STL strings, but because of read-only nature
45 * of string literals, lacks ability to modify string contents. However,
46 * \c basic_string_literal objects can be assigned to and cleared.
48 * The main advantage of this class comparing to other string classes is that
49 * it doesn't dynamically allocate memory and therefore is fast, thin and exception safe.
51 template< typename CharT, typename TraitsT >
52 class basic_string_literal
54 : public totally_ordered1< basic_string_literal< CharT, TraitsT >,
55 totally_ordered2< basic_string_literal< CharT, TraitsT >, const CharT*,
57 basic_string_literal< CharT, TraitsT >,
58 std::basic_string< CharT, TraitsT >
65 typedef basic_string_literal< CharT, TraitsT > this_type;
68 typedef CharT value_type;
69 typedef TraitsT traits_type;
71 typedef std::size_t size_type;
72 typedef std::ptrdiff_t difference_type;
73 typedef const value_type* const_pointer;
74 typedef value_type const& const_reference;
75 typedef const value_type* const_iterator;
76 typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
78 //! Corresponding STL string type
79 typedef std::basic_string< value_type, traits_type > string_type;
82 //! Pointer to the beginning of the literal
83 const_pointer m_pStart;
87 //! Empty string literal to support clear
88 static const value_type g_EmptyString[1];
94 * \post <tt>empty() == true</tt>
96 basic_string_literal() BOOST_NOEXCEPT { clear(); }
99 * Constructor from a string literal
101 * \post <tt>*this == p</tt>
102 * \param p A zero-terminated constant sequence of characters
104 template< typename T, size_type LenV >
105 basic_string_literal(T(&p)[LenV]
107 , typename enable_if< is_same< T, const value_type >, int >::type = 0
110 : m_pStart(p), m_Len(LenV - 1)
117 * \post <tt>*this == that</tt>
118 * \param that Source literal to copy string from
120 basic_string_literal(basic_string_literal const& that) BOOST_NOEXCEPT : m_pStart(that.m_pStart), m_Len(that.m_Len) {}
123 * Assignment operator
125 * \post <tt>*this == that</tt>
126 * \param that Source literal to copy string from
128 this_type& operator= (this_type const& that) BOOST_NOEXCEPT
133 * Assignment from a string literal
135 * \post <tt>*this == p</tt>
136 * \param p A zero-terminated constant sequence of characters
138 template< typename T, size_type LenV >
139 #ifndef BOOST_LOG_DOXYGEN_PASS
141 is_same< T, const value_type >,
146 #endif // BOOST_LOG_DOXYGEN_PASS
147 operator= (T(&p)[LenV]) BOOST_NOEXCEPT
153 * Lexicographical comparison (equality)
155 * \param that Comparand
156 * \return \c true if the comparand string equals to this string, \c false otherwise
158 bool operator== (this_type const& that) const BOOST_NOEXCEPT
160 return (compare_internal(m_pStart, m_Len, that.m_pStart, that.m_Len) == 0);
163 * Lexicographical comparison (equality)
165 * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
166 * \return \c true if the comparand string equals to this string, \c false otherwise
168 bool operator== (const_pointer str) const BOOST_NOEXCEPT
170 return (compare_internal(m_pStart, m_Len, str, traits_type::length(str)) == 0);
173 * Lexicographical comparison (equality)
175 * \param that Comparand
176 * \return \c true if the comparand string equals to this string, \c false otherwise
178 bool operator== (string_type const& that) const
180 return (compare_internal(m_pStart, m_Len, that.c_str(), that.size()) == 0);
184 * Lexicographical comparison (less ordering)
186 * \param that Comparand
187 * \return \c true if this string is less than the comparand, \c false otherwise
189 bool operator< (this_type const& that) const BOOST_NOEXCEPT
191 return (compare_internal(m_pStart, m_Len, that.m_pStart, that.m_Len) < 0);
194 * Lexicographical comparison (less ordering)
196 * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
197 * \return \c true if this string is less than the comparand, \c false otherwise
199 bool operator< (const_pointer str) const BOOST_NOEXCEPT
201 return (compare_internal(m_pStart, m_Len, str, traits_type::length(str)) < 0);
204 * Lexicographical comparison (less ordering)
206 * \param that Comparand
207 * \return \c true if this string is less than the comparand, \c false otherwise
209 bool operator< (string_type const& that) const
211 return (compare_internal(m_pStart, m_Len, that.c_str(), that.size()) < 0);
215 * Lexicographical comparison (greater ordering)
217 * \param that Comparand
218 * \return \c true if this string is greater than the comparand, \c false otherwise
220 bool operator> (this_type const& that) const BOOST_NOEXCEPT
222 return (compare_internal(m_pStart, m_Len, that.m_pStart, that.m_Len) > 0);
225 * Lexicographical comparison (greater ordering)
227 * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
228 * \return \c true if this string is greater than the comparand, \c false otherwise
230 bool operator> (const_pointer str) const BOOST_NOEXCEPT
232 return (compare_internal(m_pStart, m_Len, str, traits_type::length(str)) > 0);
235 * Lexicographical comparison (greater ordering)
237 * \param that Comparand
238 * \return \c true if this string is greater than the comparand, \c false otherwise
240 bool operator> (string_type const& that) const
242 return (compare_internal(m_pStart, m_Len, that.c_str(), that.size()) > 0);
248 * \pre <tt>i < size()</tt>
249 * \param i Requested character index
250 * \return Constant reference to the requested character
252 const_reference operator[] (size_type i) const BOOST_NOEXCEPT
259 * \param i Requested character index
260 * \return Constant reference to the requested character
262 * \b Throws: An <tt>std::exception</tt>-based exception if index \a i is out of string boundaries
264 const_reference at(size_type i) const
267 BOOST_THROW_EXCEPTION(std::out_of_range("basic_string_literal::at: the index value is out of range"));
272 * \return Pointer to the beginning of the literal
274 const_pointer c_str() const BOOST_NOEXCEPT { return m_pStart; }
276 * \return Pointer to the beginning of the literal
278 const_pointer data() const BOOST_NOEXCEPT { return m_pStart; }
280 * \return Length of the literal
282 size_type size() const BOOST_NOEXCEPT { return m_Len; }
284 * \return Length of the literal
286 size_type length() const BOOST_NOEXCEPT { return m_Len; }
289 * \return \c true if the literal is an empty string, \c false otherwise
291 bool empty() const BOOST_NOEXCEPT
297 * \return Iterator that points to the first character of the literal
299 const_iterator begin() const BOOST_NOEXCEPT { return m_pStart; }
301 * \return Iterator that points after the last character of the literal
303 const_iterator end() const BOOST_NOEXCEPT { return m_pStart + m_Len; }
305 * \return Reverse iterator that points to the last character of the literal
307 const_reverse_iterator rbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
309 * \return Reverse iterator that points before the first character of the literal
311 const_reverse_iterator rend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
314 * \return STL string constructed from the literal
316 string_type str() const
318 return string_type(m_pStart, m_Len);
322 * The method clears the literal
324 * \post <tt>empty() == true</tt>
326 void clear() BOOST_NOEXCEPT
328 m_pStart = g_EmptyString;
332 * The method swaps two literals
334 void swap(this_type& that) BOOST_NOEXCEPT
336 const_pointer p = m_pStart;
337 m_pStart = that.m_pStart;
346 * Assignment from another literal
348 * \post <tt>*this == that</tt>
349 * \param that Source literal to copy string from
351 this_type& assign(this_type const& that) BOOST_NOEXCEPT
353 m_pStart = that.m_pStart;
358 * Assignment from another literal
360 * \post <tt>*this == p</tt>
361 * \param p A zero-terminated constant sequence of characters
363 template< typename T, size_type LenV >
364 #ifndef BOOST_LOG_DOXYGEN_PASS
366 is_same< T, const value_type >,
371 #endif // BOOST_LOG_DOXYGEN_PASS
372 assign(T(&p)[LenV]) BOOST_NOEXCEPT
380 * The method copies the literal or its portion to an external buffer
382 * \pre <tt>pos <= size()</tt>
383 * \param str Pointer to the external buffer beginning. Must not be NULL.
384 * The buffer must have enough capacity to accommodate the requested number of characters.
385 * \param n Maximum number of characters to copy
386 * \param pos Starting position to start copying from
387 * \return Number of characters copied
389 * \b Throws: An <tt>std::exception</tt>-based exception if \a pos is out of range.
391 size_type copy(value_type* str, size_type n, size_type pos = 0) const
394 BOOST_THROW_EXCEPTION(std::out_of_range("basic_string_literal::copy: the position is out of range"));
396 size_type len = m_Len - pos;
399 traits_type::copy(str, m_pStart + pos, len);
404 * Lexicographically compares the argument string to a part of this string
406 * \pre <tt>pos <= size()</tt>
407 * \param pos Starting position within this string to perform comparison to
408 * \param n Length of the substring of this string to perform comparison to
409 * \param str Comparand. Must point to a sequence of characters, must not be NULL.
410 * \param len Number of characters in the sequence \a str.
411 * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
412 * a positive value if this string is greater than the comparand.
414 * \b Throws: An <tt>std::exception</tt>-based exception if \a pos is out of range.
416 int compare(size_type pos, size_type n, const_pointer str, size_type len) const
419 BOOST_THROW_EXCEPTION(std::out_of_range("basic_string_literal::compare: the position is out of range"));
421 size_type compare_size = m_Len - pos;
422 if (compare_size > len)
424 if (compare_size > n)
426 return compare_internal(m_pStart + pos, compare_size, str, compare_size);
429 * Lexicographically compares the argument string to a part of this string
431 * \pre <tt>pos <= size()</tt>
432 * \param pos Starting position within this string to perform comparison to
433 * \param n Length of the substring of this string to perform comparison to
434 * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
435 * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
436 * a positive value if this string is greater than the comparand.
438 * \b Throws: An <tt>std::exception</tt>-based exception if \a pos is out of range.
440 int compare(size_type pos, size_type n, const_pointer str) const BOOST_NOEXCEPT
442 return compare(pos, n, str, traits_type::length(str));
445 * Lexicographically compares the argument string literal to a part of this string
447 * \pre <tt>pos <= size()</tt>
448 * \param pos Starting position within this string to perform comparison to
449 * \param n Length of the substring of this string to perform comparison to
450 * \param that Comparand
451 * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
452 * a positive value if this string is greater than the comparand.
454 * \b Throws: An <tt>std::exception</tt>-based exception if \a pos is out of range.
456 int compare(size_type pos, size_type n, this_type const& that) const BOOST_NOEXCEPT
458 return compare(pos, n, that.c_str(), that.size());
461 * Lexicographically compares the argument string to this string
463 * \param str Comparand. Must point to a sequence of characters, must not be NULL.
464 * \param len Number of characters in the sequence \a str.
465 * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
466 * a positive value if this string is greater than the comparand.
468 int compare(const_pointer str, size_type len) const BOOST_NOEXCEPT
470 return compare(0, m_Len, str, len);
473 * Lexicographically compares the argument string to this string
475 * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
476 * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
477 * a positive value if this string is greater than the comparand.
479 int compare(const_pointer str) const BOOST_NOEXCEPT
481 return compare(0, m_Len, str, traits_type::length(str));
484 * Lexicographically compares the argument string to this string
486 * \param that Comparand
487 * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
488 * a positive value if this string is greater than the comparand.
490 int compare(this_type const& that) const BOOST_NOEXCEPT
492 return compare(0, m_Len, that.c_str(), that.size());
496 #ifndef BOOST_LOG_DOXYGEN_PASS
497 //! Internal comparison implementation
498 static int compare_internal(const_pointer pLeft, size_type LeftLen, const_pointer pRight, size_type RightLen) BOOST_NOEXCEPT
502 const int result = traits_type::compare(pLeft, pRight, (LeftLen < RightLen ? LeftLen : RightLen));
506 return LeftLen < RightLen ? -1 : (LeftLen > RightLen ? 1 : 0);
508 #endif // BOOST_LOG_DOXYGEN_PASS
511 template< typename CharT, typename TraitsT >
512 typename basic_string_literal< CharT, TraitsT >::value_type const
513 basic_string_literal< CharT, TraitsT >::g_EmptyString[1] = { 0 };
517 template< typename CharT, typename TraitsT >
518 inline void insert_fill_chars(std::basic_ostream< CharT, TraitsT >& strm, std::size_t n)
520 enum { chunk_size = 8 };
521 CharT fill_chars[chunk_size];
522 const CharT filler = strm.fill();
523 for (unsigned int i = 0; i < chunk_size; ++i)
524 fill_chars[i] = filler;
525 for (; n >= chunk_size && strm.good(); n -= chunk_size)
526 strm.write(fill_chars, static_cast< std::size_t >(chunk_size));
527 if (n > 0 && strm.good())
528 strm.write(fill_chars, n);
531 template< typename CharT, typename TraitsT >
532 void insert_aligned(std::basic_ostream< CharT, TraitsT >& strm, const CharT* p, std::size_t size)
534 const std::size_t alignment_size = static_cast< std::size_t >(strm.width()) - size;
535 const bool align_left = (strm.flags() & std::basic_ostream< CharT, TraitsT >::adjustfield) == std::basic_ostream< CharT, TraitsT >::left;
540 aux::insert_fill_chars(strm, alignment_size);
544 aux::insert_fill_chars(strm, alignment_size);
553 template< typename CharT, typename StrmTraitsT, typename LitTraitsT >
554 inline std::basic_ostream< CharT, StrmTraitsT >& operator<< (
555 std::basic_ostream< CharT, StrmTraitsT >& strm, basic_string_literal< CharT, LitTraitsT > const& lit)
559 const std::size_t size = lit.size();
560 const std::size_t w = static_cast< std::size_t >(strm.width());
562 strm.write(lit.c_str(), static_cast< std::streamsize >(size));
564 aux::insert_aligned(strm, lit.c_str(), lit.size());
571 template< typename CharT, typename TraitsT >
572 inline void swap(basic_string_literal< CharT, TraitsT >& left, basic_string_literal< CharT, TraitsT >& right) BOOST_NOEXCEPT
577 //! Creates a string literal wrapper from a constant string literal
578 #ifdef BOOST_LOG_USE_CHAR
579 template< typename T, std::size_t LenV >
581 #ifndef BOOST_LOG_DOXYGEN_PASS
583 is_same< T, const char >,
587 basic_string_literal< T >
588 #endif // BOOST_LOG_DOXYGEN_PASS
589 str_literal(T(&p)[LenV])
591 return string_literal(p);
595 #ifndef BOOST_LOG_DOXYGEN_PASS
597 #ifdef BOOST_LOG_USE_WCHAR_T
598 template< typename T, std::size_t LenV >
599 inline typename enable_if<
600 is_same< T, const wchar_t >,
603 str_literal(T(&p)[LenV])
605 return wstring_literal(p);
609 #endif // BOOST_LOG_DOXYGEN_PASS
611 BOOST_LOG_CLOSE_NAMESPACE // namespace log
615 #include <boost/log/detail/footer.hpp>
617 #endif // BOOST_LOG_UTILITY_STRING_LITERAL_HPP_INCLUDED_