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

MikkelSchubert / adapterremoval / #37

28 Jul 2024 09:48PM UTC coverage: 86.839% (-0.4%) from 87.26%
#37

push

travis-ci

MikkelSchubert
allow bool arguments without a sink variable

2151 of 2477 relevant lines covered (86.84%)

16470.77 hits per line

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

92.68
/src/json.cpp
1
/*************************************************************************\
2
 * AdapterRemoval - cleaning next-generation sequencing reads            *
3
 *                                                                       *
4
 * Copyright (C) 2015 by Mikkel Schubert - mikkelsch@gmail.com           *
5
 *                                                                       *
6
 * This program is free software: you can redistribute it and/or modify  *
7
 * it under the terms of the GNU 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 General Public License for more details.                          *
15
 *                                                                       *
16
 * You should have received a copy of the GNU General Public License     *
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
18
\*************************************************************************/
19
#include "json.hpp"
20
#include "debug.hpp"    // for AR_REQUIRE
21
#include "strutils.hpp" // for join_text
22
#include <algorithm>    // for max, find
23
#include <cmath>        // for isinf, isnan
24
#include <memory>       // for make_shared, __shared_ptr_access, shar...
25
#include <sstream>      // for ostringstream
26
#include <utility>      // for pair
27

28
namespace adapterremoval {
29

30
std::string
31
_escape(const std::string& value)
49✔
32
{
33
  std::ostringstream stream;
49✔
34

35
  stream << '"';
49✔
36

37
  for (char c : value) {
730✔
38
    switch (c) {
178✔
39
      case '"':
2✔
40
        stream << "\\\"";
2✔
41
        break;
42
      case '\\':
2✔
43
        stream << "\\\\";
2✔
44
        break;
45
      case '\b':
×
46
        stream << "\\b";
×
47
        break;
48
      case '\f':
×
49
        stream << "\\f";
×
50
        break;
51
      case '\n':
2✔
52
        stream << "\\n";
2✔
53
        break;
54
      case '\r':
×
55
        stream << "\\r";
×
56
        break;
57
      case '\t':
5✔
58
        stream << "\\t";
5✔
59
        break;
60
      default: {
167✔
61
        if (c <= 0x1F) {
167✔
62
          stream << "\\u" << std::hex << static_cast<int>(c);
×
63
        } else {
64
          stream << c;
178✔
65
        }
66
      }
67
    }
68
  }
69

70
  stream << '"';
49✔
71

72
  return stream.str();
98✔
73
}
49✔
74

75
std::string
76
format_f64(double value)
18✔
77
{
78
  AR_REQUIRE(!std::isinf(value));
18✔
79
  if (std::isnan(value)) {
18✔
80
    return "null";
4✔
81
  }
82

83
  std::ostringstream out;
16✔
84
  out.precision(3);
16✔
85
  out << std::fixed << value;
32✔
86

87
  return out.str();
16✔
88
}
16✔
89

90
////////////////////////////////////////////////////////////////////////////////
91

92
std::string
93
json_value::to_string() const
22✔
94
{
95
  std::ostringstream ss;
22✔
96

97
  write(ss);
22✔
98

99
  return ss.str();
44✔
100
}
22✔
101

102
////////////////////////////////////////////////////////////////////////////////
103

104
json_token::json_token(std::string value)
56✔
105
  : m_value(std::move(value))
112✔
106
{
107
}
4✔
108

109
json_ptr
110
json_token::from_str(const std::string& value)
10✔
111
{
112
  return std::make_shared<json_token>(_escape(value));
40✔
113
}
114

115
json_ptr
116
json_token::from_str_vec(const string_vec& values)
3✔
117
{
118
  string_vec escaped;
3✔
119
  for (const auto& it : values) {
36✔
120
    escaped.push_back(_escape(it));
18✔
121
  }
122

123
  return json_token::from_raw_vec(escaped);
6✔
124
}
3✔
125

126
json_ptr
127
json_token::from_i64(const int64_t value)
13✔
128
{
129
  return std::make_shared<json_token>(std::to_string(value));
52✔
130
}
131

132
json_ptr
133
json_token::from_i64_vec(const counts& values)
3✔
134
{
135
  string_vec strings;
3✔
136
  for (size_t i = 0; i < values.size(); ++i) {
24✔
137
    strings.push_back(std::to_string(values.get(i)));
36✔
138
  }
139

140
  return json_token::from_raw_vec(strings);
6✔
141
}
3✔
142

143
json_ptr
144
json_token::from_u64(const uint64_t value)
4✔
145
{
146
  return std::make_shared<json_token>(std::to_string(value));
16✔
147
}
148

149
json_ptr
150
json_token::from_f64(const double value)
6✔
151
{
152
  return std::make_shared<json_token>(format_f64(value));
24✔
153
}
154

155
json_ptr
156
json_token::from_f64_vec(const rates& values)
4✔
157
{
158
  string_vec strings;
4✔
159
  for (size_t i = 0; i < values.size(); ++i) {
32✔
160
    strings.push_back(format_f64(values.get(i)));
48✔
161
  }
162

163
  return json_token::from_raw_vec(strings);
8✔
164
}
4✔
165

166
json_ptr
167
json_token::from_boolean(const bool value)
5✔
168
{
169
  return std::make_shared<json_token>(value ? "true" : "false");
19✔
170
}
171

172
json_ptr
173
json_token::from_null()
4✔
174
{
175
  return std::make_shared<json_token>("null");
12✔
176
}
177

178
void
179
json_token::write(std::ostream& out, size_t /* indent */) const
32✔
180
{
181
  out << m_value;
32✔
182
}
32✔
183

184
json_ptr
185
json_token::from_raw_vec(const string_vec& values)
10✔
186
{
187
  std::ostringstream ss;
10✔
188
  ss << "[" << join_text(values, ", ") << "]";
50✔
189

190
  return std::make_shared<json_token>(ss.str());
60✔
191
}
10✔
192

193
////////////////////////////////////////////////////////////////////////////////
194

195
void
196
json_list::write(std::ostream& out, size_t indent_) const
5✔
197
{
198
  const auto indent = std::string(indent_, ' ');
10✔
199

200
  if (m_values.empty()) {
10✔
201
    out << "[]";
2✔
202
  } else {
203
    out << "[\n";
3✔
204

205
    for (size_t i = 0; i < m_values.size(); ++i) {
14✔
206
      out << indent << "  ";
8✔
207
      m_values.at(i)->write(out, indent_ + 2);
12✔
208

209
      if (i < m_values.size() - 1) {
8✔
210
        out << ",";
1✔
211
      }
212

213
      out << "\n";
4✔
214
    }
215

216
    out << indent << "]";
6✔
217
  }
218
}
5✔
219

220
json_dict_ptr
221
json_list::dict()
4✔
222
{
223
  auto ptr = std::make_shared<json_dict>();
4✔
224
  m_values.push_back(ptr);
16✔
225

226
  return ptr;
4✔
227
}
×
228

229
////////////////////////////////////////////////////////////////////////////////
230

231
void
232
json_dict::write(std::ostream& out, size_t indent_) const
32✔
233
{
234
  AR_REQUIRE(m_keys.size() == m_values.size());
96✔
235
  const auto indent = std::string(m_multi_line ? indent_ : 0, ' ');
64✔
236
  const char spacer = m_multi_line ? '\n' : ' ';
32✔
237

238
  if (m_keys.empty()) {
64✔
239
    out << "{}";
5✔
240
  } else {
241
    out << "{" << spacer;
27✔
242

243
    for (size_t i = 0; i < m_keys.size(); ++i) {
120✔
244
      const auto it = m_values.find(m_keys.at(i));
66✔
245
      AR_REQUIRE(it != m_values.end());
99✔
246

247
      out << indent << (m_multi_line ? "  " : "") << _escape(it->first) << ": ";
173✔
248
      it->second->write(out, indent_ + 2);
99✔
249
      if (i < m_keys.size() - 1) {
66✔
250
        out << ",";
6✔
251
      }
252

253
      out << spacer;
33✔
254
    }
255

256
    out << indent << "}";
54✔
257
  }
258
}
32✔
259

260
json_dict_ptr
261
json_dict::dict(const std::string& key)
9✔
262
{
263
  auto ptr = std::make_shared<json_dict>();
9✔
264
  ptr->m_multi_line = m_multi_line;
18✔
265
  _set(key, ptr);
27✔
266

267
  return ptr;
9✔
268
}
×
269

270
json_dict_ptr
271
json_dict::inline_dict(const std::string& key)
6✔
272
{
273
  auto ptr = dict(key);
6✔
274
  ptr->m_multi_line = false;
12✔
275

276
  return ptr;
6✔
277
}
278

279
json_list_ptr
280
json_dict::list(const std::string& key)
2✔
281
{
282
  auto ptr = std::make_shared<json_list>();
2✔
283
  _set(key, ptr);
6✔
284

285
  return ptr;
2✔
286
}
×
287

288
void
289
json_dict::str(const std::string& key, const std::string& value)
6✔
290
{
291
  _set(key, json_token::from_str(value));
12✔
292
}
6✔
293

294
void
295
json_dict::str_vec(const std::string& key, const string_vec& values)
1✔
296
{
297
  _set(key, json_token::from_str_vec(values));
2✔
298
}
1✔
299

300
void
301
json_dict::i64(const std::string& key, const int64_t value)
7✔
302
{
303
  _set(key, json_token::from_i64(value));
14✔
304
}
7✔
305

306
void
307
json_dict::i64_vec(const std::string& key, const counts& values)
1✔
308
{
309
  _set(key, json_token::from_i64_vec(values));
2✔
310
}
1✔
311

312
void
313
json_dict::u64(const std::string& key, const uint64_t value)
×
314
{
315
  _set(key, json_token::from_u64(value));
×
316
}
317

318
void
319
json_dict::f64(const std::string& key, const double value)
2✔
320
{
321
  _set(key, json_token::from_f64(value));
4✔
322
}
2✔
323

324
void
325
json_dict::f64_vec(const std::string& key, const rates& values)
1✔
326
{
327
  _set(key, json_token::from_f64_vec(values));
2✔
328
}
1✔
329

330
void
331
json_dict::boolean(const std::string& key, const bool value)
2✔
332
{
333
  _set(key, json_token::from_boolean(value));
4✔
334
}
2✔
335

336
void
337
json_dict::null(const std::string& key)
2✔
338
{
339
  _set(key, json_token::from_null());
6✔
340
}
2✔
341

342
void
343
json_dict::_set(const std::string& key, const json_ptr& ptr)
33✔
344
{
345
  const auto it = std::find(m_keys.begin(), m_keys.end(), key);
99✔
346
  if (it == m_keys.end()) {
99✔
347
    m_keys.push_back(key);
33✔
348
  }
349

350
  m_values[key] = ptr;
66✔
351
}
33✔
352

353
} // namespace adapterremoval
21✔
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