HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gstring.hpp
1// Copyright Take Vos 2019-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "grapheme.hpp"
8#include "../strings.hpp"
9#include <vector>
10
11namespace tt {
12
13struct gstring {
14 std::vector<grapheme> graphemes;
15
16 using const_iterator = std::vector<grapheme>::const_iterator;
17 using value_type = grapheme;
18
19 ssize_t size() const noexcept {
20 return std::ssize(graphemes);
21 }
22
23 grapheme const &at(ssize_t i) const {
24 return graphemes.at(i);
25 }
26
27 grapheme &at(ssize_t i) {
28 return graphemes.at(i);
29 }
30
31 decltype(auto) begin() noexcept { return graphemes.begin(); }
32 decltype(auto) begin() const noexcept { return graphemes.begin(); }
33 decltype(auto) cbegin() const noexcept { return graphemes.cbegin(); }
34 decltype(auto) end() noexcept { return graphemes.end(); }
35 decltype(auto) end() const noexcept { return graphemes.end(); }
36 decltype(auto) cend() const noexcept { return graphemes.cend(); }
37
38 decltype(auto) front() noexcept { return graphemes.front(); }
39 decltype(auto) front() const noexcept { return graphemes.front(); }
40 decltype(auto) back() noexcept { return graphemes.back(); }
41 decltype(auto) back() const noexcept { return graphemes.back(); }
42
43 gstring &operator+=(gstring const &rhs) noexcept {
44 for (ttlet &rhs_grapheme: rhs.graphemes) {
45 graphemes.push_back(rhs_grapheme);
46 }
47 return *this;
48 }
49
50 gstring &operator+=(grapheme const &grapheme) noexcept {
51 graphemes.push_back(grapheme);
52 return *this;
53 }
54
55 [[nodiscard]] friend std::u32string to_u32string(gstring const &rhs) noexcept {
57 r.reserve(std::ssize(rhs));
58 for (ttlet &c : rhs) {
59 r += c.NFC();
60 }
61 return r;
62 }
63
64 [[nodiscard]] friend std::string to_string(gstring const &rhs) noexcept {
65 return tt::to_string(to_u32string(rhs));
66 }
67
68
69 friend std::ostream &operator<<(std::ostream &lhs, gstring const &rhs) {
70 return lhs << to_string(rhs);
71 }
72};
73
74[[nodiscard]] gstring to_gstring(std::u32string_view rhs) noexcept;
75
76[[nodiscard]] inline gstring to_gstring(std::u8string_view rhs) noexcept {
77 return to_gstring(tt::to_u32string(rhs));
78}
79
80[[nodiscard]] inline gstring to_gstring(std::string_view rhs) noexcept
81{
82 return to_gstring(tt::to_u32string(rhs));
83}
84
85
86}
Definition grapheme.hpp:21
Definition gstring.hpp:13
T at(T... args)
T back(T... args)
T begin(T... args)
T end(T... args)
T front(T... args)
T push_back(T... args)
T reserve(T... args)