mCRL2
Loading...
Searching...
No Matches
parse_numbers.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_PARSE_NUMBERS_H
13#define MCRL2_UTILITIES_PARSE_NUMBERS_H
14
15#include <cctype>
16#include <cstddef>
17#include <string>
18#include <vector>
19
21
22namespace mcrl2 {
23
24namespace utilities {
25
26namespace detail {
27
28// Reads the next integer from the range [first, last), and the spaces behind it
29// Returns the position in the range after the next integer
30// Precondition: (first != last) && !std::isspace(*first)
31template <typename Iterator>
32Iterator parse_next_natural_number(Iterator first, Iterator last, std::size_t& result)
33{
34 assert((first != last) && !std::isspace(*first));
35
36 Iterator i = first;
37 result = 0;
38
39 for (;;)
40 {
41 if (*i < '0' || *i > '9')
42 {
43 throw mcrl2::runtime_error("could not read an integer from " + std::string(first, last));
44 }
45 result *= 10;
46 result += *i - '0';
47 ++i;
48 if (i == last)
49 {
50 break;
51 }
52 if (std::isspace(*i))
53 {
54 ++i;
55 while (i != last && std::isspace(*i))
56 {
57 ++i;
58 }
59 break;
60 }
61 }
62 return i;
63}
64
65} // namespace detail
66
68inline
69std::size_t parse_natural_number(const std::string& text)
70{
71 auto first = text.begin();
72 auto last = text.end();
73
74 // skip leading spaces
75 while (first != last && std::isspace(*first))
76 {
77 ++first;
78 }
79
80 if (first == last)
81 {
82 throw mcrl2::runtime_error("could not read an integer from " + text);
83 }
84
85 std::size_t value;
86 first = detail::parse_next_natural_number(first, last, value);
87
88 if (first != last)
89 {
90 throw mcrl2::runtime_error("could not read an integer from " + text);
91 }
92
93 return value;
94}
95
97inline
98std::vector<std::size_t> parse_natural_number_sequence(const std::string& text)
99{
100 std::vector<std::size_t> result;
101
102 auto first = text.begin();
103 auto last = text.end();
104
105 // skip leading spaces
106 while (first != last && std::isspace(*first))
107 {
108 ++first;
109 }
110
111 while (first != last)
112 {
113 std::size_t value;
114 first = detail::parse_next_natural_number(first, last, value);
115 result.push_back(value);
116 }
117
118 return result;
119}
120
121} // namespace utilities
122
123} // namespace mcrl2
124
125#endif // MCRL2_UTILITIES_PARSE_NUMBERS_H
const_iterator end() const
const_iterator begin() const
Standard exception class for reporting runtime errors.
Definition exception.h:27
Exception classes for use in libraries and tools.
Iterator parse_next_natural_number(Iterator first, Iterator last, std::size_t &result)
std::size_t parse_natural_number(const std::string &text)
Parses a natural number from a string.
std::vector< std::size_t > parse_natural_number_sequence(const std::string &text)
Parses a sequence of natural numbers (separated by spaces) from a string.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72