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

libbitcoin / libbitcoin-system / 13069453084

31 Jan 2025 08:55AM UTC coverage: 82.728% (-0.007%) from 82.735%
13069453084

push

github

web-flow
Merge pull request #1604 from evoskuil/master

Set _CRTDBG_MAP_ALLOC for vc++ debug mode leak tracking.

2 of 2 new or added lines in 1 file covered. (100.0%)

15 existing lines in 11 files now uncovered.

10039 of 12135 relevant lines covered (82.73%)

3871639.49 hits per line

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

98.31
/src/words/languages.cpp
1
/**
2
 * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/system/words/languages.hpp>
20

21
#include <algorithm>
22
#include <string>
23
#include <unordered_map>
24
#include <bitcoin/system/data/data.hpp>
25
#include <bitcoin/system/unicode/unicode.hpp>
26

27
namespace libbitcoin {
28
namespace system {
29
namespace words {
30

31
// local definitions
32
// ----------------------------------------------------------------------------
33

34
typedef std::unordered_map<language, const char*> language_map;
35

36
// All languages, dictionary-independent.
37
// Dictionaries are collections of words in one of these languages.
38
// There can be multiple dictionaries for a given language identifier.
39

40
static const language_map map
41
{
42
    { language::en, "en" },
43
    { language::es, "es" },
44
    { language::it, "it" },
45
    { language::fr, "fr" },
46
    { language::cs, "cs" },
47
    { language::pt, "pt" },
48
    { language::ja, "ja" },
49
    { language::ko, "ko" },
50
    { language::zh_Hans, "zh_Hans" },
51
    { language::zh_Hant, "zh_Hant" }
52
};
53

54
// static methods
55
// ----------------------------------------------------------------------------
56

57
language languages::from_name(const std::string& name) NOEXCEPT
12✔
58
{
59
    const auto it = std::find_if(map.begin(), map.end(),
12✔
60
        [&](const language_map::value_type& pair) NOEXCEPT
75✔
61
        {
62
            return pair.second == name;
75✔
63
        });
64

65
    return it != map.end() ? it->first : language::none;
12✔
66
}
67

68
std::string languages::to_name(language identifier) NOEXCEPT
47✔
69
{
70
    const auto it = std::find_if(map.begin(), map.end(),
47✔
71
        [&](const language_map::value_type& pair) NOEXCEPT
285✔
72
        {
73
            return pair.first == identifier;
285✔
74
        });
75

76
    return it != map.end() ? it->second : "";
47✔
77
}
78

79
std::string languages::to_delimiter(language identifier) NOEXCEPT
220✔
80
{
81
    return identifier == language::ja ? ideographic_space : ascii_space;
380✔
82
}
83

84
std::string languages::join(const string_list& words,
210✔
85
    language identifier) NOEXCEPT
86
{
87
    // Language is specialized for joining in japanese.
88
    return system::join(words, to_delimiter(identifier));
420✔
89
}
90

91
string_list languages::split(const std::string& sentence, language) NOEXCEPT
137✔
92
{
93
    // Language is not currently specialized for splitting.
94
    return system::split(sentence, unicode_separators, unicode_whitespace);
137✔
95
}
96

97
// protected
98
string_list languages::try_normalize(const string_list& words) NOEXCEPT
439✔
99
{
100
    string_list normal(words.size());
439✔
101

102
    // This is only used for dictionary matching.
103
    // All dictionaries are confirmed via test cases to be lower/nfkd.
104
    std::transform(words.begin(), words.end(), normal.begin(),
439✔
105
        [](const std::string& word) NOEXCEPT
5,779✔
106
        {
107
            auto token = ascii_to_lower(trim_copy(word, unicode_whitespace));
5,779✔
108
            to_compatibility_decomposition(token);
5,779✔
109
            to_lower(token);
5,779✔
110
            return token;
5,779✔
111
        });
112

113
    return normal;
439✔
UNCOV
114
}
×
115

116
// constructors
117
// ----------------------------------------------------------------------------
118

119
// protected
120
languages::languages() NOEXCEPT
344✔
121
  : entropy_(), words_(), identifier_(language::none)
344✔
122
{
123
}
344✔
124

125
languages::languages(const languages& other) NOEXCEPT
320✔
126
  : entropy_(other.entropy_), words_(other.words_),
320✔
127
    identifier_(other.identifier_)
320✔
128
{
129
}
320✔
130

131
// protected
132
languages::languages(const data_chunk& entropy, const string_list& words,
308✔
133
    language identifier) NOEXCEPT
308✔
134
  : entropy_(entropy), words_(words), identifier_(identifier)
308✔
135
{
136
}
308✔
137

138
// public methods
139
// ----------------------------------------------------------------------------
140

141
const data_chunk& languages::entropy() const NOEXCEPT
215✔
142
{
143
    return entropy_;
215✔
144
}
145

146
language languages::lingo() const NOEXCEPT
411✔
147
{
148
    return identifier_;
411✔
149
}
150

151
std::string languages::sentence() const NOEXCEPT
208✔
152
{
153
    return join(words(), lingo());
416✔
154
}
155

156
const string_list& languages::words() const NOEXCEPT
486✔
157
{
158
    return words_;
208✔
159
}
160

161
// operators
162
// ----------------------------------------------------------------------------
163

164
languages::operator bool() const NOEXCEPT
878✔
165
{
166
    return !entropy_.empty();
878✔
167
}
168

169
languages& languages::operator=(const languages& other) NOEXCEPT
12✔
170
{
171
    entropy_ = other.entropy_;
12✔
172
    words_ = other.words_;
12✔
173
    identifier_ = other.identifier_;
12✔
174
    return *this;
12✔
175
}
176

177
bool languages::operator<(const languages& other) const NOEXCEPT
12✔
178
{
179
    return sentence() < other.sentence();
12✔
180
}
181

182
bool languages::operator==(const languages& other) const NOEXCEPT
36✔
183
{
184
    // Words and entropy are equivalent except in the case of electrum_v1
185
    // overflows. Comparing here prevents the need for electrum_v1 override.
186
    return entropy_ == other.entropy_ && identifier_ == other.identifier_ &&
36✔
187
        words_ == other.words_;
23✔
188
}
189

190
bool languages::operator!=(const languages& other) const NOEXCEPT
16✔
191
{
192
    return !(*this == other);
16✔
193
}
194

195
} // namespace words
196
} // namespace system
197
} // namespace libbitcoin
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