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

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

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

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

7
#include <hpx/program_options/config.hpp>
8
#include <hpx/modules/functional.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(
×
33
        std::basic_string<FromChar> const& s, Fun fun)
34

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

38
        auto state = std::mbstate_t();
×
39

40
        FromChar const* from = s.data();
×
41
        FromChar const* from_end = s.data() + s.size();
×
42

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

52
            ToChar* to_next = buffer;
×
53
            std::codecvt_base::result const r = fun(
54
                state, from, from_end, from, &buffer[0], &buffer[32], to_next);
55

56
            if (r == std::codecvt_base::error)
57
                throw std::logic_error("character conversion failed");
58

×
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)
65
                throw std::logic_error("character conversion failed");
66

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

×
71
        return result;
72
    }
73

×
74
    std::wstring from_8_bit(std::string const& s,
75
        std::codecvt<wchar_t, char, std::mbstate_t> const& cvt)
76
    {
77
        return detail::convert<wchar_t>(s,
78
            hpx::bind_front(
79
                &std::codecvt<wchar_t, char, std::mbstate_t>::in, &cvt));
×
80
    }
81

82
    std::string to_8_bit(std::wstring const& s,
83
        std::codecvt<wchar_t, char, std::mbstate_t> const& cvt)
84
    {
×
85
        return detail::convert<char>(s,
86
            hpx::bind_front(
87
                &std::codecvt<wchar_t, char, std::mbstate_t>::out, &cvt));
×
88
    }
89

90
    namespace {
91

92
        hpx::program_options::detail::utf8_codecvt_facet utf8_facet;
×
93
    }
94

95
    std::wstring from_utf8(std::string const& s)
96
    {
97
        return from_8_bit(s, utf8_facet);
98
    }
99

100
    std::string to_utf8(std::wstring const& s)
×
101
    {
102
        return to_8_bit(s, utf8_facet);
×
103
    }
104

105
    std::wstring from_local_8_bit(std::string const& s)
×
106
    {
107
        using facet_type = std::codecvt<wchar_t, char, std::mbstate_t>;
×
108
        return from_8_bit(s, std::use_facet<facet_type>(std::locale()));
109
    }
110

×
111
    std::string to_local_8_bit(std::wstring const& s)
112
    {
113
        using facet_type = std::codecvt<wchar_t, char, std::mbstate_t>;
×
114
        return to_8_bit(s, std::use_facet<facet_type>(std::locale()));
115
    }
116

×
117
    std::string to_internal(std::string const& s)
118
    {
119
        return s;
×
120
    }
121

122
    std::string to_internal(std::wstring const& s)
504✔
123
    {
124
        return to_utf8(s);
504✔
125
    }
126
}    // namespace hpx::program_options::detail
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