]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/enriched_string.h
C++ modernize: Pragma once (#6264)
[dragonfireclient.git] / src / util / enriched_string.h
1 /*
2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, NathanaĆ«l Courant <nore@mesecons.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <string>
23 #include <vector>
24 #include <SColor.h>
25
26 class EnrichedString {
27 public:
28         EnrichedString();
29         EnrichedString(const std::wstring &s,
30                 const irr::video::SColor &color = irr::video::SColor(255, 255, 255, 255));
31         EnrichedString(const wchar_t *str,
32                 const irr::video::SColor &color = irr::video::SColor(255, 255, 255, 255));
33         EnrichedString(const std::wstring &string,
34                 const std::vector<irr::video::SColor> &colors);
35         void operator=(const wchar_t *str);
36         void addAtEnd(const std::wstring &s, const irr::video::SColor &color);
37
38         // Adds the character source[i] at the end.
39         // An EnrichedString should always be able to be copied
40         // to the end of an existing EnrichedString that way.
41         void addChar(const EnrichedString &source, size_t i);
42
43         // Adds a single character at the end, without specifying its
44         // color. The color used will be the one from the last character.
45         void addCharNoColor(wchar_t c);
46
47         EnrichedString substr(size_t pos = 0, size_t len = std::string::npos) const;
48         EnrichedString operator+(const EnrichedString &other) const;
49         void operator+=(const EnrichedString &other);
50         const wchar_t *c_str() const;
51         const std::vector<irr::video::SColor> &getColors() const;
52         const std::wstring &getString() const;
53         inline bool operator==(const EnrichedString &other) const
54         {
55                 return (m_string == other.m_string && m_colors == other.m_colors);
56         }
57         inline bool operator!=(const EnrichedString &other) const
58         {
59                 return !(*this == other);
60         }
61         inline void clear()
62         {
63                 m_string.clear();
64                 m_colors.clear();
65                 m_has_background = false;
66         }
67         inline bool empty() const
68         {
69                 return m_string.empty();
70         }
71         inline size_t size() const
72         {
73                 return m_string.size();
74         }
75         inline bool hasBackground() const
76         {
77                 return m_has_background;
78         }
79         inline irr::video::SColor getBackground() const
80         {
81                 return m_background;
82         }
83 private:
84         std::wstring m_string;
85         std::vector<irr::video::SColor> m_colors;
86         bool m_has_background = false;
87         irr::video::SColor m_background;
88 };