HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
language_tag.hpp
1// Copyright Take Vos 2020.
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 <string>
8#include <ostream>
9#include "../strings.hpp"
10
11namespace tt {
12
16public:
17 language_tag(language_tag const &other) noexcept : tag(other.tag) {}
18
19 language_tag(language_tag &&other) noexcept : tag(std::move(other.tag)) {}
20
21 language_tag &operator=(language_tag const &other) noexcept
22 {
23 tt_axiom(this != &other);
24 tag = other.tag;
25 return *this;
26 }
27
28 language_tag &operator=(language_tag &&other) noexcept
29 {
30 tt_axiom(this != &other);
31 tag = std::move(other.tag);
32 return *this;
33 }
34
35 language_tag() noexcept : tag() {}
36
37 explicit language_tag(std::string tag) noexcept : tag(std::move(tag)) {}
38 explicit language_tag(std::string_view tag) noexcept : tag(tag) {}
39 explicit language_tag(char const *tag) noexcept : tag(tag) {}
40
41 [[nodiscard]] size_t hash() const noexcept
42 {
43 return std::hash<std::string>{}(tag);
44 }
45
46 operator bool() const noexcept
47 {
48 return size(tag) != 0;
49 }
50
51 [[nodiscard]] language_tag short_tag() const noexcept
52 {
53 return language_tag{split(tag, '-').front()};
54 }
55
56 [[nodiscard]] bool operator==(language_tag const &rhs) const noexcept
57 {
58 return tag == rhs.tag;
59 }
60
61 [[nodiscard]] bool operator!=(language_tag const &rhs) const noexcept = default;
62
63 [[nodiscard]] friend std::string to_string(language_tag const &url) noexcept
64 {
65 return url.tag;
66 }
67
68 friend std::ostream &operator<<(std::ostream &lhs, const language_tag &rhs)
69 {
70 return lhs << to_string(rhs);
71 }
72
73private:
74 std::string tag;
75};
76
77} // namespace tt
78
79namespace std {
80
81template<>
82class hash<tt::language_tag> {
83public:
84 [[nodiscard]] size_t operator()(tt::language_tag const &rhs) const noexcept
85 {
86 return rhs.hash();
87 }
88};
89
90} // namespace std
STL namespace.
An IETF BCP 47 Language tag.
Definition language_tag.hpp:15
T move(T... args)
T operator()(T... args)