• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #856

28 Dec 2022 02:00AM UTC coverage: 86.602% (+0.05%) from 86.55%
#856

push

StellarBot
Merge #6119

6119: Update CMakeLists.txt r=hkaiser a=khuck

updating the default APEX version


Co-authored-by: Kevin Huck <khuck@cs.uoregon.edu>

174566 of 201573 relevant lines covered (86.6%)

1876093.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/libs/core/program_options/src/convert.cpp
1
// Copyright Vladimir Prus 2004.
2
//  SPDX-License-Identifier: BSL-1.0
3
// Distributed under the Boost Software License, Version 1.0.
4
// (See accompanying file LICENSE_1_0.txt
5
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/program_options/config.hpp>
8
#include <hpx/functional/bind_front.hpp>
9
#include <hpx/program_options/detail/convert.hpp>
10
#include <hpx/program_options/detail/utf8_codecvt_facet.hpp>
11

12
#include <clocale>
13
#include <fstream>
14
#include <locale>
15
#include <stdexcept>
16
#include <string>
17

18
namespace hpx::program_options::detail {
19

20
    /* Internal function to actually perform conversion.
21
       The logic in from_8_bit and to_8_bit function is exactly
22
       the same, except that one calls 'in' method of codecvt and another
23
       calls the 'out' method, and that syntax difference makes straightforward
24
       template implementation impossible.
25

26
       This functions takes a 'fun' argument, which should have the same
27
       parameters and return type and the in/out methods. The actual converting
28
       function will pass functional objects created with std::bind.
29
       Experiments show that the performance loss is less than 10%.
30
    */
31
    template <typename ToChar, typename FromChar, typename Fun>
32
    std::basic_string<ToChar> convert(
31✔
33
        std::basic_string<FromChar> const& s, Fun fun)
34

35
    {
36
        std::basic_string<ToChar> result;
31✔
37

38
        std::mbstate_t state = std::mbstate_t();
31✔
39

40
        FromChar const* from = s.data();
31✔
41
        FromChar const* from_end = s.data() + s.size();
31✔
42
        // The interface of cvt is not really iterator-like, and it's
43
        // not possible the tell the required output size without the conversion.
44
        // All we can is convert data by pieces.
45
        while (from != from_end)
62✔
46
        {
47
            // std::basic_string does not provide non-const pointers to the data,
48
            // so converting directly into string is not possible.
49
            ToChar buffer[32];
50

51
            ToChar* to_next = buffer;
31✔
52
            // Need variable because std::bind doesn't work with rvalues.
53
            ToChar* to_end = buffer + 32;
31✔
54
            std::codecvt_base::result r =
31✔
55
                fun(state, from, from_end, from, buffer, to_end, to_next);
31✔
56

57
            if (r == std::codecvt_base::error)
31✔
58
                throw std::logic_error("character conversion failed");
×
59
            // 'partial' is not an error, it just means not all source
60
            // characters were converted. However, we need to check that at
61
            // least one new target character was produced. If not, it means
62
            // the source data is incomplete, and since we don't have extra
63
            // data to add to source, it's error.
64
            if (to_next == buffer)
31✔
65
                throw std::logic_error("character conversion failed");
×
66

67
            // Add converted characters
68
            result.append(buffer, to_next);
31✔
69
        }
70

71
        return result;
31✔
72
    }
31✔
73
}    // namespace hpx::program_options::detail
74

75
namespace hpx::program_options {
76

77
    std::wstring from_8_bit(std::string const& s,
21✔
78
        std::codecvt<wchar_t, char, std::mbstate_t> const& cvt)
79
    {
80
        return detail::convert<wchar_t>(s,
42✔
81
            hpx::bind_front(
21✔
82
                &std::codecvt<wchar_t, char, std::mbstate_t>::in, &cvt));
21✔
83
    }
84

85
    std::string to_8_bit(std::wstring const& s,
10✔
86
        std::codecvt<wchar_t, char, std::mbstate_t> const& cvt)
87
    {
88
        return detail::convert<char>(s,
20✔
89
            hpx::bind_front(
10✔
90
                &std::codecvt<wchar_t, char, std::mbstate_t>::out, &cvt));
10✔
91
    }
92

93
    namespace {
94

95
        hpx::program_options::detail::utf8_codecvt_facet utf8_facet;
1,251✔
96
    }
97

98
    std::wstring from_utf8(std::string const& s)
20✔
99
    {
100
        return from_8_bit(s, utf8_facet);
20✔
101
    }
102

103
    std::string to_utf8(std::wstring const& s)
8✔
104
    {
105
        return to_8_bit(s, utf8_facet);
8✔
106
    }
107

108
    std::wstring from_local_8_bit(std::string const& s)
1✔
109
    {
110
        using facet_type = std::codecvt<wchar_t, char, std::mbstate_t>;
111
        return from_8_bit(s, std::use_facet<facet_type>(std::locale()));
1✔
112
    }
×
113

114
    std::string to_local_8_bit(std::wstring const& s)
2✔
115
    {
116
        using facet_type = std::codecvt<wchar_t, char, std::mbstate_t>;
117
        return to_8_bit(s, std::use_facet<facet_type>(std::locale()));
2✔
118
    }
×
119

120
    std::string to_internal(std::string const& s)
15,182✔
121
    {
122
        return s;
15,182✔
123
    }
124

125
    std::string to_internal(std::wstring const& s)
8✔
126
    {
127
        return to_utf8(s);
8✔
128
    }
129
}    // namespace hpx::program_options
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc