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

Nic30 / hdlConvertor / #201

10 Jun 2025 02:39PM UTC coverage: 59.995% (-2.5%) from 62.506%
#201

push

travis-ci

Nic30
Merge remote-tracking branch 'origin/mesonbuild'

109 of 127 new or added lines in 14 files covered. (85.83%)

11 existing lines in 6 files now uncovered.

41572 of 69293 relevant lines covered (59.99%)

1047819.11 hits per line

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

85.0
/src/encodingConversions.cpp
1
#include <hdlConvertor/encodingConversions.h>
2
#include <locale>
3
#include <codecvt>
4

5
namespace hdlConvertor {
6

7
std::string iso_8859_1_to_utf8(const std::string &str) {
4,568✔
8
        std::string strOut;
4,568✔
9
        strOut.reserve(str.size());
4,568✔
10
        for (auto it = str.begin(); it != str.end(); ++it) {
14,483,542✔
11
                uint8_t ch = *it;
14,478,974✔
12
                if (ch < 0x80) {
14,478,974✔
13
                        strOut.push_back(ch);
14,478,878✔
14
                } else {
15
                        strOut.push_back(0xc0 | ch >> 6);
96✔
16
                        strOut.push_back(0x80 | (ch & 0x3f));
96✔
17
                }
18
        }
19
        return strOut;
4,568✔
20
}
×
21

22
std::string _to_utf8(const std::string &str, const std::string &encoding) {
13,832✔
23
        if (encoding == "utf-8") {
13,832✔
24
                return str;
9,262✔
25
        } else if (encoding == "utf-16") {
4,570✔
26
                auto & _str = reinterpret_cast<const std::u16string&>(str);
2✔
27
                // because of bug in MSVC https://stackoverflow.com/questions/32055357/visual-studio-c-2015-stdcodecvt-with-char16-t-or-char32-t
28
                #if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
29
                        std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> convert;
30
                        auto p = reinterpret_cast<const int16_t *>(_str.data());
31
                        return convert.to_bytes(p, p + _str.size());
32
            #else
33
                        std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
2✔
34
                        return convert.to_bytes(_str);
2✔
35
                #endif
36
        } else if (encoding == "iso-8859-1") {
4,570✔
37
                return iso_8859_1_to_utf8(str);
4,568✔
38
        } else {
39
                throw std::runtime_error(
40
                                "iParserContainer implemented only for utf-8, utf-16 and iso-8859-1, recieved:"
41
                                                + encoding);
×
42
        }
43
}
44

45
antlr4::ANTLRInputStream ANTLRFileStream_with_encoding(
9,721✔
46
                const std::filesystem::path &file_name, const std::string &encoding) {
47
        std::ifstream ifs;
9,721✔
48
        std::string str;
9,721✔
49
        ifs.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
9,721✔
50
        ifs.open(file_name);
9,721✔
51

52
        // Sets position to the end of the file.
53
        ifs.seekg(0, std::ios::end);
9,721✔
54

55
        // Reserves memory for the file.
56
        str.reserve(ifs.tellg());
9,721✔
57

58
        // Sets position to the start of the file.
59
        ifs.seekg(0, std::ios::beg);
9,721✔
60

61
        // Sets contents of 'str' to all characters in the file.
62
        str.assign(std::istreambuf_iterator<char>(ifs),
9,721✔
63
          std::istreambuf_iterator<char>());
9,721✔
64

65
        str = _to_utf8(str, encoding);
9,721✔
66

67
        try {
68
                antlr4::ANTLRInputStream input_stream(str);
9,721✔
69
                input_stream.name = file_name.u8string();
9,721✔
70
                return input_stream;
19,442✔
UNCOV
71
        } catch (const std::range_error &e) {
×
72
                throw std::range_error(
73
                                std::string("Invalid character/encoding in ")
×
74
                                                + file_name.u8string() + " file");
×
75
        }
×
76
}
9,721✔
77

78
}
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

© 2026 Coveralls, Inc