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

MikkelSchubert / adapterremoval / #38

07 Aug 2024 07:47PM UTC coverage: 83.365% (-3.5%) from 86.839%
#38

push

travis-ci

MikkelSchubert
additional tests

2190 of 2627 relevant lines covered (83.37%)

15528.72 hits per line

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

75.0
/src/strutils.hpp
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
#pragma once
20

21
#include <cstddef>     // for size_t
22
#include <cstdint>     // for uint64_t
23
#include <ostream>     // for ostream
24
#include <sstream>     // for ostringstream
25
#include <string>      // for string
26
#include <string_view> // for string_view
27
#include <vector>      // for vector
28

29
namespace adapterremoval {
30

31
using string_vec = std::vector<std::string>;
32

33
const size_t DEFAULT_MAX_COLUMNS = 78;
34
const size_t DEFAULT_INDENTATION = 4;
35

36
/** Computes the Levenshtein distance between two strings. */
37
size_t
38
levenshtein(std::string_view s, std::string_view t);
39

40
/**
41
 * Returns a timestamp in the specified format, see
42
 * https://en.cppreference.com/w/cpp/io/manip/put_time
43
 */
44
std::string
45
timestamp(const char* format, bool milliseconds = false);
46

47
/**
48
 * Convert a string to an unsigned integer.
49
 *
50
 * Throws std::invalid_argument if the string does not contain a proper number,
51
 * if it contains more than just a number, or if the number overflows a
52
 * unsigned integer.
53
 */
54
unsigned
55
str_to_unsigned(const std::string& s);
56

57
/** Lowercases letters in the range a-z */
58
constexpr char
59
to_lower(char c)
48✔
60
{
61
  return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + 32) : c;
48✔
62
}
63

64
/** Lowercases letters in the range a-z */
65
std::string
66
to_lower(std::string str);
67

68
/** Uppercase letters in the range a-z */
69
constexpr char
70
to_upper(char c)
12✔
71
{
72
  return (c >= 'a' && c <= 'z') ? static_cast<char>(c - 32) : c;
12✔
73
}
74

75
/** Uppercase letters in the range a-z */
76
std::string
77
to_upper(std::string str);
78

79
/** Returns true if str1 ends with str2 (case sensitive) */
80
bool
81
ends_with(std::string_view str1, std::string_view str2);
82

83
/** Split text by newlines */
84
string_vec
85
split_lines(std::string_view text);
86

87
/** Split text by newlines and add fixed indentation following newlines. */
88
std::string
89
indent_lines(std::string_view lines, size_t indentation = DEFAULT_INDENTATION);
90

91
/**
92
 * Formats text into fixed-width columns.
93
 *
94
 * @param value Text representing a single paragraph to be formatted.
95
 * @param max_width Maximum width of output lines in characters.
96
 * @param ljust Indent lines after the first line by this amount of characters.
97
 *
98
 * Note that all whitespace in the input string is consumed; output words are
99
 * separated by a single space, and the terminal line does not end with a
100
 * newline.
101
 */
102
string_vec
103
wrap_text(const std::string& value,
104
          size_t max_width = DEFAULT_MAX_COLUMNS,
105
          size_t ljust = 0);
106

107
template<typename T>
108
std::string
109
join_text(const std::vector<T>& values,
107✔
110
          std::string_view sep,
111
          std::string_view final_sep)
112
{
113
  std::ostringstream stream;
107✔
114
  for (auto it = values.begin(); it != values.end(); ++it) {
920✔
115
    if (it != values.begin()) {
477✔
116
      if (it + 1 == values.end()) {
216✔
117
        stream << final_sep;
159✔
118
      } else {
119
        stream << sep;
159✔
120
      }
121
    }
122

123
    stream << *it;
303✔
124
  }
125

126
  return stream.str();
214✔
127
}
107✔
128

7✔
129
template<typename T>
130
std::string
131
join_text(const std::vector<T>& values, std::string_view sep)
95✔
132
{
7✔
133
  return join_text(values, sep, sep);
183✔
134
}
45✔
135

32✔
136
/**
15✔
137
 * Wrapper around 'indent_lines' and 'wrap_text'.
138
 *
15✔
139
 * Defaults to 78 character columns, with 4 character indentation, and no ljust,
140
 * corresponding to function defaults. Unlike simply combining 'indent_lines'
141
 * and 'wrap_text', the 'format' function preserves newlines, allowing
142
 * paragraphs to be printed easily.
15✔
143
 */
144
class cli_formatter
145
{
14✔
146
public:
7✔
147
  /** Creates formatter using default parameters (see above). */
×
148
  cli_formatter();
149

150
  /** Include (or not) indentation on the first line of output. */
151
  cli_formatter& set_indent_first_line(bool value);
×
152
  /** Maximum number of columns in each line in output. */
×
153
  cli_formatter& set_column_width(size_t value);
×
154
  /** Number of spaces to indent line 2+ in each paragraph. */
×
155
  cli_formatter& set_ljust(size_t value);
×
156
  /** Fixed indentation for each line (see also set_indent_first_line). */
157
  cli_formatter& set_indent(size_t value);
×
158

159
  /** Formats string using the current settings. */
160
  std::string format(std::string_view value) const;
161

×
162
private:
163
  //! Specifies whether or not to indent the first line of output.
164
  bool m_indent_first;
×
165
  //! Number of spaces to indent the 2+ lines in each paragraph.
166
  size_t m_ljust;
167
  //! The maximum number of columns (in characters).
168
  size_t m_columns;
169
  //! The number of spaces to indent each line (see also m_indent_first_line)
4✔
170
  size_t m_indentation;
171
};
4✔
172

173
/** Escapes a string to make it safe to use in copy/pasted commands */
174
std::string
175
shell_escape(std::string_view s);
176

177
/**
178
 * Escapes a string for use in logging; this is equivalent to shell_escape
179
 * except that strings are always quoted for readability.
180
 */
181
std::string
182
log_escape(std::string_view s);
183

184
/** Escapes a full shell command using shell_escape */
185
std::string
186
shell_escape_command(const string_vec& v);
187

188
/** Adds thousand separators to a number */
189
std::string
190
format_thousand_sep(size_t count);
191

192
/** Rounds a number using K, M, etc. units. */
193
std::string
194
format_rough_number(size_t value, size_t out_digits = 3);
195

196
/** Formats a fraction, returning "NA" if denominator is 0 */
197
std::string
198
format_fraction(uint64_t num, uint64_t denom, size_t precision = 2);
199

200
/** Formats a percentage, returning "NA" if denominator is 0 */
201
std::string
202
format_percentage(uint64_t num, uint64_t denom, size_t precision = 1);
203

204
} // namespace adapterremoval
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