File: strutils.hpp

package info (click to toggle)
adapterremoval 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,864 kB
  • sloc: cpp: 24,110; python: 427; makefile: 207; sh: 16
file content (118 lines) | stat: -rw-r--r-- 4,794 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*************************************************************************\
 * AdapterRemoval - cleaning next-generation sequencing reads            *
 *                                                                       *
 * Copyright (C) 2015 by Mikkel Schubert - mikkelsch@gmail.com           *
 *                                                                       *
 * If you use the program, please cite the paper:                        *
 * S. Lindgreen (2012): AdapterRemoval: Easy Cleaning of Next Generation *
 * Sequencing Reads, BMC Research Notes, 5:337                           *
 * http://www.biomedcentral.com/1756-0500/5/337/                         *
 *                                                                       *
 * This program is free software: you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation, either version 3 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
\*************************************************************************/
#ifndef STRUTILS_H
#define STRUTILS_H

#include <string>
#include <sstream>
#include <iomanip>

namespace ar
{

const size_t DEFAULT_MAX_COLUMNS = 78;
const size_t DEFAULT_INDENTATION = 4;


/**
 * Convert a string to an unsigned integer.
 *
 * Throws std::invalid_argument if the string does not contain a proper number,
 * if it contains more than just a number, or if the number overflows a
 * unsigned integer.
 */
unsigned str_to_unsigned(const std::string& s);


/** Uppercases letters in the range a-z */
std::string toupper(const std::string& str);


/** Split text by newlines and add fixed indentation following newlines. */
std::string indent_lines(const std::string& lines, size_t identation = DEFAULT_INDENTATION);


/**
 * Formats text into fixed-width columns.
 *
 * @param value Text representing a single paragraph to be formatted.
 * @param max_width Maximum width of output lines in characters.
 * @param ljust Indent lines after the first line by this amount of characters.
 *
 * Note that all whitespace in the input string is consumed; output words are
 * seperated by a single space, and the terminal line does not end with a
 * newline.
 */
std::string columnize_text(const std::string& value, size_t max_width = DEFAULT_MAX_COLUMNS, size_t ljust = 0);


/**
 * Wrapper around 'indent_lines' and 'columnize_text'.
 *
 * Defaults to 78 character columns, with 4 character indentation, and
 * no ljust, corresponding to function defaults. Unlike simply combining
 * 'indent_lines' and 'columnize_text', the 'format' function preserves
 * newlines, allowing paragraphs to be printed easily.
 */
class cli_formatter
{
public:
    /** Creates formatter using default parameters (see above). */
    cli_formatter();

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

    /** Formats string using the current settings. */
    std::string format(const std::string& value) const;

    /** Format string using default parameters. */
    static std::string fmt(const std::string& value);

    /**
     * Format string using default parameters, but include prefix on first line
     * and indent subsequent lines using the width of the prefix.
     */
    static std::string fmt(const std::string& prefix, const std::string& value);

private:
    //! Specifices whether or not to indent the first line of output.
    bool m_indent_first;
    //! Number of spaces to indent the 2+ lines in each paragraph.
    size_t m_ljust;
    //! The maximum number of columns (in characters).
    size_t m_columns;
    //! The number of spaces to indent each line (see also m_indent_first_line)
    size_t m_indentation;
};

} // namespace ar

#endif