]> git.lyx.org Git - lyx.git/blob - src/support/trivstring.h
Account for old versions of Pygments
[lyx.git] / src / support / trivstring.h
1 // -*- C++ -*-
2 /**
3  * \file trivstring.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Georg Baum
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_TRIVSTRING_H
13 #define LYX_TRIVSTRING_H
14
15 #include "support/strfwd.h"
16
17 #ifdef STD_STRING_USES_COW
18 #include <cstdlib>
19
20 namespace lyx {
21
22 /**
23  * Trivial string class with almost no features.
24  * The public interface is a subset of the std::basic_string interface.
25  * The only important feature is that any read-only access does not need
26  * synchronization between multiple threads, i.e. it is thread-safe without
27  * locking.
28  * Therefore you can safely use a const trivial_string object in multiple
29  * threads at the same time. This is not the case for std::basic_string in some
30  * STL implementations (e. g. GNU libcstd++, see bug 9336 and
31  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=21334.
32  * This class should not be used for anything else than providing thread-safety.
33  * It should be removed as soon as LyX requires C++11, and all supported STL
34  * implementations provide a C++11 conformant std::basic_string.
35  *
36  * If you change anything in this class please ensure that the unit test
37  * tests/check_trivstring.cpp still tests 100% of the public interface.
38  */
39 template <typename Char> class trivial_string
40 {
41 public:
42         /// Corresponding std::basic_string
43         typedef std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > _stdstring;
44         /// Construct an empty string
45         trivial_string() : size_(0), data_(0) {}
46         /// Construct a string from a copy of \p that
47         trivial_string(trivial_string const & that);
48         /// Construct a string from a copy of \p that
49         trivial_string(Char const * that, size_t n);
50         /// Construct a string from a copy of \p that
51         trivial_string(_stdstring const & that);
52         ///
53         ~trivial_string() { if (!use_sso()) delete[] data_; }
54         /// Assign a copy of \p that
55         trivial_string & operator=(trivial_string const & that);
56         /// Assign a copy of \p that
57         trivial_string & operator=(_stdstring const & that);
58         /// Exchange contents with contents of \p that
59         void swap(trivial_string & that);
60         /// The length of the string, excluding the final 0 character
61         size_t length() const { return size_; }
62         /// Is this string empty?
63         bool empty() const { return size_ == 0; }
64         /// Is this string ordered before, at the same position or after \p other?
65         int compare(trivial_string const & other) const;
66         /// Return substring of length \p n starting at \p pos
67         trivial_string substr(size_t pos = 0, size_t n = _stdstring::npos) const;
68         /// Create a copy as std::basic_string
69         operator _stdstring() const;
70         /// Return a C-compatible string, terminated by a 0 character.
71         /// This is never a copy and only valid for the life time of the trivial_string instance.
72         Char const * c_str() const;
73         /// Return character at position \p i (validity of \i is not checked)
74         Char operator[](size_t i) const;
75 private:
76         /**
77          * Whether short string optimization is used.
78          * Short string optimization is a technique where no additional memory
79          * needs to be allocated to store the string contents.
80          * Instead, the memory which would be used to store the pointer to the
81          * character buffer is reinterpreted to be a Char * buffer.
82          * On most 64 bit systems and with Char == char this allows to store
83          * strings of up to 7 characters without allocating additional memory.
84          */
85         bool use_sso() const { return (size_ + 1) * sizeof(Char) <= sizeof(Char *); }
86         /// The character storage if sso is used
87         Char       * data_sso()       { return reinterpret_cast<Char *      >(&data_); }
88         /// The character storage if sso is used
89         Char const * data_sso() const { return reinterpret_cast<Char const *>(&data_); }
90         /// The length of the string, excluding the final 0 character
91         size_t size_;
92         /// The character storage
93         Char * data_;
94 };
95
96
97 /// Comparison operator (needed for std::set etc)
98 template <typename Char> bool operator<(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs);
99
100
101 /// Equality operator
102 template <typename Char> bool operator==(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs);
103 template <typename Char> bool operator==(trivial_string<Char> const & lhs, Char const * rhs);
104 template <typename Char> bool operator==(Char const * lhs, trivial_string<Char> const & rhs);
105
106
107 /// Stream output operator
108 template <typename Char>
109 std::basic_ostream<Char, std::char_traits<Char> > &
110 operator<<(std::basic_ostream<Char, std::char_traits<Char> > &, trivial_string<Char> const &);
111
112 } // namespace lyx
113 #else
114 #include <string>
115 #endif
116 #endif