git repos / blockattack-game

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

normal view · raw

dbf0fff9 Poul Sander 2025-09-18 18:25 1
// Copyright 2018 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_7e906c01_03a3_4daf_b420_ea7ea952b3c9
dbf0fff9 Poul Sander 2025-09-18 18:25 29
#define UTF8_FOR_CPP_7e906c01_03a3_4daf_b420_ea7ea952b3c9
dbf0fff9 Poul Sander 2025-09-18 18:25 30
dbf0fff9 Poul Sander 2025-09-18 18:25 31
#include "cpp11.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::string utf16to8(std::u16string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 36
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 37
        std::string 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::u16string utf8to16(std::string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 43
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 44
        std::u16string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 45
        utf8to16(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::string utf32to8(std::u32string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 50
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 51
        std::string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 52
        utf32to8(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::u32string utf8to32(std::string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 57
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 58
        std::u32string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 59
        utf8to32(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::size_t find_invalid(std::string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 64
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 65
        std::string_view::const_iterator invalid = find_invalid(s.begin(), s.end());
dbf0fff9 Poul Sander 2025-09-18 18:25 66
        return (invalid == s.end()) ? std::string_view::npos : static_cast<std::size_t>(invalid - s.begin());
dbf0fff9 Poul Sander 2025-09-18 18:25 67
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 68
dbf0fff9 Poul Sander 2025-09-18 18:25 69
    inline bool is_valid(std::string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 70
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 71
        return is_valid(s.begin(), s.end());
dbf0fff9 Poul Sander 2025-09-18 18:25 72
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 73
dbf0fff9 Poul Sander 2025-09-18 18:25 74
    inline std::string replace_invalid(std::string_view s, char32_t replacement)
dbf0fff9 Poul Sander 2025-09-18 18:25 75
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 76
        std::string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 77
        replace_invalid(s.begin(), s.end(), std::back_inserter(result), replacement);
dbf0fff9 Poul Sander 2025-09-18 18:25 78
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 79
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 80
dbf0fff9 Poul Sander 2025-09-18 18:25 81
    inline std::string replace_invalid(std::string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 82
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 83
        std::string result;
dbf0fff9 Poul Sander 2025-09-18 18:25 84
        replace_invalid(s.begin(), s.end(), std::back_inserter(result));
dbf0fff9 Poul Sander 2025-09-18 18:25 85
        return result;
dbf0fff9 Poul Sander 2025-09-18 18:25 86
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 87
dbf0fff9 Poul Sander 2025-09-18 18:25 88
    inline bool starts_with_bom(std::string_view s)
dbf0fff9 Poul Sander 2025-09-18 18:25 89
    {
dbf0fff9 Poul Sander 2025-09-18 18:25 90
        return starts_with_bom(s.begin(), s.end());
dbf0fff9 Poul Sander 2025-09-18 18:25 91
    }
dbf0fff9 Poul Sander 2025-09-18 18:25 92
dbf0fff9 Poul Sander 2025-09-18 18:25 93
} // namespace utf8
dbf0fff9 Poul Sander 2025-09-18 18:25 94
dbf0fff9 Poul Sander 2025-09-18 18:25 95
#endif // header guard
dbf0fff9 Poul Sander 2025-09-18 18:25 96
1970-01-01 00:00 97