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

MikkelSchubert / adapterremoval / #43

08 Sep 2024 08:47AM UTC coverage: 75.266% (-4.5%) from 79.763%
#43

push

travis-ci

MikkelSchubert
minor improvements to assert signatures and silence lints

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

75 existing lines in 3 files now uncovered.

2404 of 3194 relevant lines covered (75.27%)

12788.78 hits per line

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

91.12
/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(std::string_view value)
51✔
32
{
33
  std::ostringstream stream;
51✔
34

35
  stream << '"';
51✔
36

37
  for (char c : value) {
337✔
38
    switch (c) {
184✔
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: {
173✔
61
        if (c <= 0x1F) {
173✔
62
          stream << "\\u" << std::hex << static_cast<int>(c);
×
63
        } else {
64
          stream << c;
184✔
65
        }
66
      }
67
    }
68
  }
69

70
  stream << '"';
51✔
71

72
  return stream.str();
102✔
73
}
51✔
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
23✔
94
{
95
  std::ostringstream ss;
23✔
96

97
  write(ss);
23✔
98

99
  return ss.str();
46✔
100
}
23✔
101

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

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

109
json_ptr
110
json_token::from_str(std::string_view value)
11✔
111
{
112
  return std::make_shared<json_token>(_escape(value));
44✔
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));
24✔
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
33✔
180
{
181
  out << m_value;
33✔
182
}
33✔
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
6✔
197
{
198
  const auto indent = std::string(indent_, ' ');
12✔
199

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

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

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

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

216
    out << indent << "]";
8✔
217
  }
218
}
6✔
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
json_dict_ptr
230
json_list::inline_dict()
2✔
231
{
232
  auto ptr = std::make_shared<json_dict>();
2✔
233
  ptr->m_multi_line = false;
4✔
234
  m_values.push_back(ptr);
8✔
235

236
  return ptr;
2✔
UNCOV
237
}
×
238

239
////////////////////////////////////////////////////////////////////////////////
240

241
void
242
json_dict::write(std::ostream& out, size_t indent_) const
34✔
243
{
244
  const auto indent = std::string(m_multi_line ? indent_ : 0, ' ');
68✔
245
  const char spacer = m_multi_line ? '\n' : ' ';
34✔
246

247
  if (m_items.empty()) {
68✔
248
    out << "{}";
6✔
249
  } else {
250
    out << "{" << spacer;
28✔
251

252
    for (size_t i = 0; i < m_items.size(); ++i) {
124✔
253
      const auto it = m_items.at(i);
68✔
254

255
      out << indent << (m_multi_line ? "  " : "") << _escape(it.first) << ": ";
179✔
256
      it.second->write(out, indent_ + 2);
68✔
257
      if (i + 1 < m_items.size()) {
68✔
258
        out << ",";
6✔
259
      }
260

261
      out << spacer;
34✔
262
    }
34✔
263

264
    out << indent << "}";
56✔
265
  }
266
}
34✔
267

268
json_dict_ptr
269
json_dict::dict(std::string_view key)
9✔
270
{
271
  auto ptr = std::make_shared<json_dict>();
9✔
272
  ptr->m_multi_line = m_multi_line;
18✔
273
  _set(key, ptr);
27✔
274

275
  return ptr;
9✔
UNCOV
276
}
×
277

278
json_dict_ptr
279
json_dict::inline_dict(std::string_view key)
6✔
280
{
281
  auto ptr = dict(key);
6✔
282
  ptr->m_multi_line = false;
12✔
283

284
  return ptr;
6✔
285
}
286

287
json_list_ptr
288
json_dict::list(std::string_view key)
2✔
289
{
290
  auto ptr = std::make_shared<json_list>();
2✔
291
  _set(key, ptr);
6✔
292

293
  return ptr;
2✔
UNCOV
294
}
×
295

296
void
297
json_dict::str(std::string_view key, std::string_view value)
7✔
298
{
299
  _set(key, json_token::from_str(value));
14✔
300
}
7✔
301

302
void
303
json_dict::str_vec(std::string_view key, const string_vec& values)
1✔
304
{
305
  _set(key, json_token::from_str_vec(values));
2✔
306
}
1✔
307

308
void
309
json_dict::i64(std::string_view key, const int64_t value)
7✔
310
{
311
  _set(key, json_token::from_i64(value));
14✔
312
}
7✔
313

314
void
315
json_dict::i64_vec(std::string_view key, const counts& values)
1✔
316
{
317
  _set(key, json_token::from_i64_vec(values));
2✔
318
}
1✔
319

320
void
UNCOV
321
json_dict::u64(std::string_view key, const uint64_t value)
×
322
{
UNCOV
323
  _set(key, json_token::from_u64(value));
×
324
}
325

326
void
327
json_dict::f64(std::string_view key, const double value)
2✔
328
{
329
  _set(key, json_token::from_f64(value));
4✔
330
}
2✔
331

332
void
333
json_dict::f64_vec(std::string_view key, const rates& values)
1✔
334
{
335
  _set(key, json_token::from_f64_vec(values));
2✔
336
}
1✔
337

338
void
339
json_dict::boolean(std::string_view key, const bool value)
2✔
340
{
341
  _set(key, json_token::from_boolean(value));
4✔
342
}
2✔
343

344
void
345
json_dict::null(std::string_view key)
2✔
346
{
347
  _set(key, json_token::from_null());
6✔
348
}
2✔
349

350
void
351
json_dict::_set(std::string_view key, const json_ptr& ptr)
34✔
352
{
353
  for (auto& it : m_items) {
168✔
354
    if (it.first == key) {
16✔
UNCOV
355
      it.second = ptr;
×
UNCOV
356
      return;
×
357
    }
358
  }
359

360
  m_items.emplace_back(key, ptr);
34✔
361
}
362

363
} // namespace adapterremoval
17✔
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