mCRL2
Loading...
Searching...
No Matches
text_utility.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
11
12#ifndef MCRL2_UTILITIES_TEXT_UTILITY_H
13#define MCRL2_UTILITIES_TEXT_UTILITY_H
14
15#include <algorithm>
16#include <iostream>
17#include <iterator>
18#include <limits>
19#include <sstream>
20#include <string>
21#include <vector>
22
23namespace mcrl2
24{
25
26namespace utilities
27{
28
33template <class CharContainer>
34inline bool contains_only_ascii_symbols(const CharContainer& input)
35{
36 for(const typename CharContainer::value_type& c: input)
37 {
38 // Allow printable characters and a tab, line feed, and a carriage return
39 if (!(std::isprint(c) || c=='\t' || c=='\n' || c=='\r' ))
40 {
41 return false;
42 }
43 }
44 return true;
45}
46
51template <typename T>
52inline
53std::string to_string(const T& x)
54{
55 std::stringstream ss;
56 ss << x;
57 return ss.str();
58}
59
63std::vector<std::string> split_paragraphs(const std::string& text);
64
69std::vector<std::string> split(const std::string& line, const std::string& separators);
70
75std::string read_text(const std::string& filename, bool warn=false);
76
80inline
81std::string read_text(std::istream& in)
82{
83 in.unsetf(std::ios::skipws); // Turn of white space skipping on the stream
84 std::string s;
85 std::copy(
86 std::istream_iterator<char>(in),
87 std::istream_iterator<char>(),
88 std::back_inserter(s)
89 );
90 return s;
91}
92
96std::string remove_comments(const std::string& text);
97
101std::string remove_whitespace(const std::string& text);
102
108std::string regex_replace(const std::string& src, const std::string& dest, const std::string& text);
109
114std::vector<std::string> regex_split(const std::string& text, const std::string& sep);
115
119std::string trim_copy(const std::string& text);
120
123void trim(std::string& text);
124
128template <typename Container>
129std::string string_join(const Container& c, const std::string& separator)
130{
131 std::ostringstream out;
132 for (typename Container::const_iterator i = c.begin(); i != c.end(); ++i)
133 {
134 if (i != c.begin())
135 {
136 out << separator;
137 }
138 out << *i;
139 }
140 return out.str();
141}
142
147std::string word_wrap_text(const std::string& text, unsigned int max_line_length = 78);
148
152bool is_numeric_string(const std::string& s);
153
158inline void number2string(std::size_t number, std::string& buffer, std::size_t start_position)
159{
160 // First calculate the number of digital digits of number;
161 std::size_t number_of_digits=0;
162 if (number==0)
163 {
164 number_of_digits=1;
165 }
166 else
167 {
168 for(std::size_t copy=number ; copy!=0; ++number_of_digits, copy=copy/10)
169 {}
170 }
171
172 // Put the number in the buffer at the right position.
173 std::size_t position=start_position+number_of_digits;
174 buffer.resize(position);
175
176 while (position>start_position)
177 {
178 --position;
179 buffer[position] = '0' + number % 10;
180 number = number/10;
181 }
182}
183
188inline std::string number2string(std::size_t number)
189{
190 std::string buffer;
191 buffer.reserve(std::numeric_limits<std::size_t>::digits10 + 1);
192 number2string(number, buffer, 0);
193 return std::string(buffer);
194}
195
196} // namespace utilities
197
198} // namespace mcrl2
199
200#endif // MCRL2_UTILITIES_TEXT_UTILITY_H
std::string string_join(const Container &c, const std::string &separator)
Joins a sequence of strings. This is a replacement for boost::algorithm::join, since it gives stack o...
void number2string(std::size_t number, std::string &buffer, std::size_t start_position)
Convert a number to a string in the buffer starting at position start_position.
std::vector< std::string > split_paragraphs(const std::string &text)
Split a string into paragraphs.
void trim(std::string &text)
Remove all trailing and leading spaces from the input.
std::string regex_replace(const std::string &src, const std::string &dest, const std::string &text)
Regular expression replacement in a string.
std::string read_text(const std::string &filename, bool warn=false)
Read text from a file.
std::string word_wrap_text(const std::string &text, unsigned int max_line_length=78)
Apply word wrapping to a text.
bool is_numeric_string(const std::string &s)
Test if a string is a number.
std::string to_string(const T &x)
Transform parameter into string.
std::vector< std::string > regex_split(const std::string &text, const std::string &sep)
Split a string using a regular expression separator.
std::string remove_comments(const std::string &text)
Remove comments from a text (everything from '' until end of line).
std::string trim_copy(const std::string &text)
Remove all trailing and leading spaces from the input.
std::string remove_whitespace(const std::string &text)
Removes whitespace from a string.
bool contains_only_ascii_symbols(const CharContainer &input)
Checks whether the input only contains proper ascii characters, including common control characters.
std::vector< std::string > split(const std::string &line, const std::string &separators)
Split the text.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72