Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
string.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <stdexcept>
27#include <algorithm>
28
29#include <ext/sstream>
30
31#include "vector.hpp"
32
33namespace ext {
34
41class string : public std::string {
42public:
46 using std::string::string; // NOLINT(modernize-use-equals-default)
47
51 using std::string::operator =;
52#ifndef __clang__
53
57 string ( ) = default;
58
62 string ( const string & other ) = default;
63
67 string ( string && other ) = default;
68
72 string & operator = ( string && other ) = default;
73
77 string & operator = ( const string & other ) = default;
78#endif
85 explicit string ( const std::string & other ) noexcept : std::string ( other ) {
86 }
87
94 explicit string ( std::string && other ) noexcept : std::string ( std::move ( other ) ) {
95 }
96
103 string & operator = ( std::string && other ) noexcept {
104 static_cast < std::string & > ( * this ) = std::move ( other );
105 return * this;
106 }
107
114 string & operator = ( const std::string & other ) noexcept {
115 static_cast < std::string & > ( * this ) = other;
116 return * this;
117 }
118};
119
130template < typename T >
131std::string to_string ( const T & value ) {
133 ss << value;
134 return ss.str ( );
135}
136
147template < typename T >
148T from_string ( const std::string & );
149
158template < >
159std::string from_string ( const std::string & value );
160
169template < >
170int from_string ( const std::string & value );
171
180template < >
181bool from_string ( const std::string & value );
182
191template < >
192long from_string ( const std::string & value );
193
202template < >
203long long from_string ( const std::string & value );
204
213template < >
214unsigned from_string ( const std::string & value );
215
224template < >
225unsigned long from_string ( const std::string & value );
226
235template < >
236unsigned long long from_string ( const std::string & value );
237
246template < >
247double from_string ( const std::string & value );
248
258ext::vector < std::string > explode ( const std::string & source, const std::string & delimiter );
259
269std::string implode ( const std::vector < std::string > & source, const std::string & delimiter );
270
279inline bool isspace ( int ch ) {
280 return std::isspace ( ch ) != 0;
281}
282
291inline bool not_isspace ( int ch ) {
292 return std::isspace ( ch ) == 0;
293}
294
303inline std::string_view ltrim ( std::string_view s ) {
304 const auto * endPos = std::find_if ( s.begin ( ), s.end ( ), not_isspace );
305 s.remove_prefix ( std::distance ( s.begin ( ), endPos ) );
306 return s;
307}
308
317inline std::string_view rtrim ( std::string_view s ) {
318 const auto * beginPos = std::find_if ( s.rbegin ( ), s.rend ( ), not_isspace ).base ( );
319 s.remove_suffix ( std::distance ( beginPos, s.end ( ) ) );
320 return s;
321}
322
331inline std::string_view trim ( std::string_view s ) {
332 return rtrim(ltrim(s));
333}
334
335// Creates a string view from a pair of iterators
336// http://stackoverflow.com/q/33750600/882436
337inline std::string_view make_string_view ( std::string::const_iterator begin, std::string::const_iterator end ) {
338 return std::string_view { ( begin != end ) ? & * begin : nullptr, static_cast < std::string_view::size_type > ( std::max ( std::distance ( begin, end ), static_cast < typename std::string_view::difference_type > ( 0 ) ) ) };
339} // make_string_view
340
341} /* namespace ext */
342
Definition: sstream.h:15
std::string str() const &
Definition: sstream.cpp:29
string(const std::string &other) noexcept
Constructor from standard string.
Definition: string.hpp:85
string & operator=(string &&other)=default
string(std::string &&other) noexcept
Constructor from standard string.
Definition: string.hpp:94
string(string &&other)=default
string(const string &other)=default
string()=default
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Definition: sigHandler.cpp:20
std::string from_string(const std::string &value)
Predeclaration of from string method. The from string methods are supposed to convert string represen...
Definition: string.cpp:13
auto end(Container &&cont) -> decltype(std::forward(cont).end())
Definition: iterator.hpp:912
bool not_isspace(int ch)
Inverse of isspace method.
Definition: string.hpp:291
std::string_view make_string_view(std::string::const_iterator begin, std::string::const_iterator end)
Definition: string.hpp:337
std::string_view trim(std::string_view s)
Trims spaces inside the string from both sides.
Definition: string.hpp:331
constexpr const T & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
std::string implode(const std::vector< std::string > &source, const std::string &delimiter)
Merges strings using the delimiter.
Definition: string.cpp:76
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
std::string_view rtrim(std::string_view s)
Trims spaces inside the string from the right.
Definition: string.hpp:317
ext::vector< std::string > explode(const std::string &source, const std::string &delimiter)
Splits the string to substrings based on delimiter.
Definition: string.cpp:61
std::string_view ltrim(std::string_view s)
Trims spaces inside the string from the left.
Definition: string.hpp:303
bool isspace(int ch)
isspace method.
Definition: string.hpp:279
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
Definition: RandomStringFactory.cpp:12