git repos / blockattack-game

blame: source/code/Libs/include/utf8/cpp20.h

normal view · raw

dbf0fff9 Poul Sander 2025-09-18 18:25 1
// Copyright 2022 Nemanja Trifunovic
dbf0fff9 Poul Sander 2025-09-18 18:25 2
dbf0fff9 Poul Sander 2025-09-18 18:25 3
/*
dbf0fff9 Poul Sander 2025-09-18 18:25 4
Permission is hereby granted, free of charge, to any person or organization
dbf0fff9 Poul Sander 2025-09-18 18:25 5
obtaining a copy of the software and accompanying documentation covered by
dbf0fff9 Poul Sander 2025-09-18 18:25 6
this license (the "Software") to use, reproduce, display, distribute,
dbf0fff9 Poul Sander 2025-09-18 18:25 7
execute, and transmit the Software, and to prepare derivative works of the
dbf0fff9 Poul Sander 2025-09-18 18:25 8
Software, and to permit third-parties to whom the Software is furnished to
dbf0fff9 Poul Sander 2025-09-18 18:25 9
do so, all subject to the following:
dbf0fff9 Poul Sander 2025-09-18 18:25 10
dbf0fff9 Poul Sander 2025-09-18 18:25 11
The copyright notices in the Software and this entire statement, including
dbf0fff9 Poul Sander 2025-09-18 18:25 12
the above license grant, this restriction and the following disclaimer,
dbf0fff9 Poul Sander 2025-09-18 18:25 13
must be included in all copies of the Software, in whole or in part, and
dbf0fff9 Poul Sander 2025-09-18 18:25 14
all derivative works of the Software, unless such copies or derivative
dbf0fff9 Poul Sander 2025-09-18 18:25 15
works are solely in the form of machine-executable object code generated by
dbf0fff9 Poul Sander 2025-09-18 18:25 16
a source language processor.
dbf0fff9 Poul Sander 2025-09-18 18:25 17
dbf0fff9 Poul Sander 2025-09-18 18:25 18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dbf0fff9 Poul Sander 2025-09-18 18:25 19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dbf0fff9 Poul Sander 2025-09-18 18:25 20
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
dbf0fff9 Poul Sander 2025-09-18 18:25 21
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
dbf0fff9 Poul Sander 2025-09-18 18:25 22
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
dbf0fff9 Poul Sander 2025-09-18 18:25 23
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dbf0fff9 Poul Sander 2025-09-18 18:25 24
DEALINGS IN THE SOFTWARE.
dbf0fff9 Poul Sander 2025-09-18 18:25 25
*/
dbf0fff9 Poul Sander 2025-09-18 18:25 26
dbf0fff9 Poul Sander 2025-09-18 18:25 27
dbf0fff9 Poul Sander 2025-09-18 18:25 28
#ifndef UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9
dbf0fff9 Poul Sander 2025-09-18 18:25 29
#define UTF8_FOR_CPP_207e906c01_03a3_4daf_b420_ea7ea952b3c9
dbf0fff9 Poul Sander 2025-09-18 18:25 30
dbf0fff9 Poul Sander 2025-09-18 18:25 31
#include "cpp17.h"
dbf0fff9 Poul Sander 2025-09-18 18:25 32
dbf0fff9 Poul Sander 2025-09-18 18:25 33
namespace utf8
dbf0fff9 Poul Sander 2025-09-18 18:25 34
{
dbf0fff9 Poul Sander 2025-09-18 18:25 35
    inline std::u8string utf16tou8(const std::u16string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 36
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 37
        std::u8string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 38
        utf16to8(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 39
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 40
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 41
dbf0fff9 Poul Sander 2025-09-18 18:25 42
    inline std::u8string utf16tou8(std::u16string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 43
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 44
        std::u8string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 45
        utf16to8(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 46
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 47
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 48
dbf0fff9 Poul Sander 2025-09-18 18:25 49
    inline std::u16string utf8to16(const std::u8string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 50
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 51
        std::u16string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 52
        utf8to16(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 53
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 54
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 55
dbf0fff9 Poul Sander 2025-09-18 18:25 56
    inline std::u16string utf8to16(const std::u8string_view& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 57
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 58
        std::u16string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 59
        utf8to16(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 60
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 61
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 62
dbf0fff9 Poul Sander 2025-09-18 18:25 63
    inline std::u8string utf32tou8(const std::u32string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 64
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 65
        std::u8string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 66
        utf32to8(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 67
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 68
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 69
dbf0fff9 Poul Sander 2025-09-18 18:25 70
    inline std::u8string utf32tou8(const std::u32string_view& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 71
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 72
        std::u8string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 73
        utf32to8(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 74
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 75
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 76
dbf0fff9 Poul Sander 2025-09-18 18:25 77
    inline std::u32string utf8to32(const std::u8string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 78
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 79
        std::u32string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 80
        utf8to32(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 81
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 82
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 83
dbf0fff9 Poul Sander 2025-09-18 18:25 84
    inline std::u32string utf8to32(const std::u8string_view& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 85
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 86
        std::u32string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 87
        utf8to32(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 88
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 89
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 90
dbf0fff9 Poul Sander 2025-09-18 18:25 91
    inline std::size_t find_invalid(const std::u8string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 92
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 93
        std::u8string::const_iterator invalid = find_invalid(s.begin(), s.end());
dbf0fff9 Poul Sander 2025-09-18 18:25 94
        return (invalid == s.end()) ? std::string_view::npos : static_cast<std::size_t>(invalid - s.begin());
dbf0fff9 Poul Sander 2025-09-18 18:25 95
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 96
dbf0fff9 Poul Sander 2025-09-18 18:25 97
    inline bool is_valid(const std::u8string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 98
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 99
        return is_valid(s.begin(), s.end());
dbf0fff9 Poul Sander 2025-09-18 18:25 100
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 101
dbf0fff9 Poul Sander 2025-09-18 18:25 102
    inline std::u8string replace_invalid(const std::u8string& s, char32_t replacement)
dbf0fff9 Poul Sander 2025-09-18 18:25 103
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 104
        std::u8string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 105
        replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement);
dbf0fff9 Poul Sander 2025-09-18 18:25 106
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 107
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 108
dbf0fff9 Poul Sander 2025-09-18 18:25 109
    inline std::u8string replace_invalid(const std::u8string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 110
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 111
        std::u8string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 112
        replace_invalid(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 113
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 114
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 115
dbf0fff9 Poul Sander 2025-09-18 18:25 116
    inline bool starts_with_bom(const std::u8string& s)
dbf0fff9 Poul Sander 2025-09-18 18:25 117
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 118
        return starts_with_bom(s.begin(), s.end());
dbf0fff9 Poul Sander 2025-09-18 18:25 119
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 120
 
dbf0fff9 Poul Sander 2025-09-18 18:25 121
} // namespace utf8
dbf0fff9 Poul Sander 2025-09-18 18:25 122
dbf0fff9 Poul Sander 2025-09-18 18:25 123
#endif // header guard
dbf0fff9 Poul Sander 2025-09-18 18:25 124
1970-01-01 00:00 125