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 = default;
18 language_tag(language_tag &&other) noexcept = default;
19 language_tag &operator=(language_tag const &other) noexcept = default;
20 language_tag &operator=(language_tag &&other) noexcept = default;
21
22 language_tag() noexcept : tag() {}
23 explicit language_tag(std::string tag) noexcept : tag(std::move(tag)) {}
24 explicit language_tag(std::string_view tag) noexcept : tag(tag) {}
25 explicit language_tag(char const *tag) noexcept : tag(tag) {}
26
27 [[nodiscard]] size_t hash() const noexcept
28 {
29 return std::hash<std::string>{}(tag);
30 }
31
32 explicit operator bool() const noexcept
33 {
34 return size(tag) != 0;
35 }
36
37 [[nodiscard]] language_tag short_tag() const noexcept
38 {
39 return language_tag{split(tag, '-').front()};
40 }
41
42 [[nodiscard]] bool operator==(language_tag const &rhs) const noexcept
43 {
44 return tag == rhs.tag;
45 }
46
47 [[nodiscard]] bool operator!=(language_tag const &rhs) const noexcept = default;
48
49 [[nodiscard]] friend std::string to_string(language_tag const &url) noexcept
50 {
51 return url.tag;
52 }
53
54 friend std::ostream &operator<<(std::ostream &lhs, language_tag const &rhs)
55 {
56 return lhs << to_string(rhs);
57 }
58
59private:
60 std::string tag;
61};
62
63
64} // namespace tt
65
66namespace std {
67
68template<>
69class hash<tt::language_tag> {
70public:
71 [[nodiscard]] size_t operator()(tt::language_tag const &rhs) const noexcept
72 {
73 return rhs.hash();
74 }
75};
76
77} // namespace std
STL namespace.
An IETF BCP 47 Language tag.
Definition language_tag.hpp:15
T move(T... args)
T operator()(T... args)