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

MikkelSchubert / adapterremoval / #39

20 Aug 2024 01:31PM UTC coverage: 79.602% (-3.8%) from 83.365%
#39

push

travis-ci

MikkelSchubert
update schema URL to use github pages

2279 of 2863 relevant lines covered (79.6%)

14257.45 hits per line

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

76.32
/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
starts_with(std::string_view str1, std::string_view str2);
82

83
/** Returns true if str1 ends with str2 (case sensitive) */
84
bool
85
ends_with(std::string_view str1, std::string_view str2);
86

87
string_vec
88
split_text(std::string_view text, char separator);
89

90
/** Split text by newlines */
91
inline string_vec
92
split_lines(std::string_view text)
173✔
93
{
94
  return split_text(text, '\n');
173✔
95
}
96

97
/** Split text by newlines and add fixed indentation following newlines. */
98
std::string
99
indent_lines(std::string_view lines, size_t indentation = DEFAULT_INDENTATION);
100

101
/**
102
 * Formats text into fixed-width columns.
103
 *
104
 * @param value Text representing a single paragraph to be formatted.
105
 * @param max_width Maximum width of output lines in characters.
106
 * @param ljust Indent lines after the first line by this amount of characters.
107
 *
108
 * Note that all whitespace in the input string is consumed; output words are
109
 * separated by a single space, and the terminal line does not end with a
110
 * newline.
111
 */
112
string_vec
113
wrap_text(const std::string& value,
114
          size_t max_width = DEFAULT_MAX_COLUMNS,
115
          size_t ljust = 0);
116

117
template<typename T>
118
std::string
119
join_text(const std::vector<T>& values,
113✔
120
          std::string_view sep,
121
          std::string_view final_sep)
122
{
123
  std::ostringstream stream;
113✔
124
  for (auto it = values.begin(); it != values.end(); ++it) {
962✔
125
    if (it != values.begin()) {
495✔
126
      if (it + 1 == values.end()) {
216✔
127
        stream << final_sep;
165✔
128
      } else {
129
        stream << sep;
165✔
130
      }
131
    }
132

133
    stream << *it;
315✔
134
  }
135

136
  return stream.str();
226✔
137
}
113✔
138

7✔
139
template<typename T>
140
std::string
141
join_text(const std::vector<T>& values, std::string_view sep)
101✔
142
{
7✔
143
  return join_text(values, sep, sep);
189✔
144
}
45✔
145

32✔
146
/**
15✔
147
 * Wrapper around 'indent_lines' and 'wrap_text'.
148
 *
15✔
149
 * Defaults to 78 character columns, with 4 character indentation, and no ljust,
150
 * corresponding to function defaults. Unlike simply combining 'indent_lines'
151
 * and 'wrap_text', the 'format' function preserves newlines, allowing
152
 * paragraphs to be printed easily.
15✔
153
 */
154
class cli_formatter
155
{
14✔
156
public:
7✔
157
  /** Creates formatter using default parameters (see above). */
×
158
  cli_formatter();
159

160
  /** Include (or not) indentation on the first line of output. */
161
  cli_formatter& set_indent_first_line(bool value);
×
162
  /** Maximum number of columns in each line in output. */
×
163
  cli_formatter& set_column_width(size_t value);
×
164
  /** Number of spaces to indent line 2+ in each paragraph. */
×
165
  cli_formatter& set_ljust(size_t value);
×
166
  /** Fixed indentation for each line (see also set_indent_first_line). */
167
  cli_formatter& set_indent(size_t value);
×
168

169
  /** Formats string using the current settings. */
170
  std::string format(std::string_view value) const;
171

×
172
private:
173
  //! Specifies whether or not to indent the first line of output.
174
  bool m_indent_first;
×
175
  //! Number of spaces to indent the 2+ lines in each paragraph.
176
  size_t m_ljust;
177
  //! The maximum number of columns (in characters).
178
  size_t m_columns;
179
  //! The number of spaces to indent each line (see also m_indent_first_line)
4✔
180
  size_t m_indentation;
181
};
4✔
182

183
/** Escapes a string to make it safe to use in copy/pasted commands */
184
std::string
185
shell_escape(std::string_view s);
186

187
/**
188
 * Escapes a string for use in logging; this is equivalent to shell_escape
189
 * except that strings are always quoted for readability.
190
 */
191
std::string
192
log_escape(std::string_view s);
193

194
/** Escapes a full shell command using shell_escape */
195
std::string
196
shell_escape_command(const string_vec& v);
197

198
/** Adds thousand separators to a number */
199
std::string
200
format_thousand_sep(size_t count);
201

202
/** Rounds a number using K, M, etc. units. */
203
std::string
204
format_rough_number(size_t value, size_t out_digits = 3);
205

206
/** Formats a fraction, returning "NA" if denominator is 0 */
207
std::string
208
format_fraction(uint64_t num, uint64_t denom, size_t precision = 2);
209

210
/** Formats a percentage, returning "NA" if denominator is 0 */
211
std::string
212
format_percentage(uint64_t num, uint64_t denom, size_t precision = 1);
213

214
} // 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