Tomographer  v1.0a
Tomographer C++ Framework Documentation
conststr.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2015 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #ifndef TOMOGRAPHER_CONSTSTR_H
28 #define TOMOGRAPHER_CONSTSTR_H
29 
30 #include <string>
31 
32 
41 namespace Tomographer {
42 namespace Tools {
43 
44 
45 
54 class conststr
55 {
56  const char* _p;
57  std::size_t _sz;
58 public:
59  template<std::size_t N>
60  constexpr conststr(const char(&a)[N]) : _p(a), _sz(N - 1) {}
61  constexpr conststr(const char *a, std::size_t n) : _p(a), _sz(n) {}
62 
63  inline constexpr char operator[](std::size_t n) const
64  {
65  return n < _sz ? _p[n] : throw std::out_of_range("");
66  }
67  inline constexpr std::size_t size() const { return _sz; }
68 
69  inline constexpr bool is_in_range(std::size_t n) const
70  {
71  return n < _sz ? true : false;
72  }
73  inline constexpr bool check_range(std::size_t n, bool answer = true) const
74  {
75  return is_in_range(n) ? answer : throw std::out_of_range("");
76  }
77 
78  inline constexpr std::size_t clamp_to_range(const std::size_t pos) const
79  {
80  return pos >= _sz ? _sz-1 : pos;
81  }
82 
83  inline constexpr bool startswith(const conststr& s, std::size_t StartOffset = 0, std::size_t S_I = 0) const {
84  return ((S_I >= s.size())
85  ? true
86  : (StartOffset+S_I < size() && s[S_I] == operator[](StartOffset+S_I)
87  ? startswith(s, StartOffset, S_I+1)
88  : false)
89  );
90  }
91 
92  inline constexpr bool operator==(const conststr& other) const {
93  return startswith(other) && other.size() == size();
94  }
95 
96  inline constexpr conststr substr(std::size_t pos, std::size_t count = std::string::npos) const {
97  return conststr(_p+pos, (pos > size() || count > size() || pos+count>size()) ? (size()-pos) : count);
98  }
99  inline constexpr conststr substr_e(std::size_t pos, std::size_t end = std::string::npos) const {
100  return conststr(_p+pos, (end>size()) ? (size()-pos) : end-pos);
101  }
102 
103  inline constexpr std::size_t find(const conststr& s, std::size_t pos = 0,
104  std::size_t not_found = std::string::npos) const
105  {
106  return (!is_in_range(pos)
107  ? ( not_found )
108  : ( startswith(s, pos)
109  ? pos
110  : (pos <= size()-s.size()
111  ? find(s, pos+1, not_found)
112  : not_found) ));
113  }
114 
115  inline constexpr std::size_t rfind(const conststr& s, std::size_t pos = std::string::npos,
116  std::size_t not_found = std::string::npos) const
117  {
118  return ((s.size() > size())
119  ? ( not_found )
120  : ((pos > size()-s.size())
121  ? rfind(s, size()-s.size(), not_found)
122  : ( startswith(s, pos)
123  ? pos
124  : ((pos > s.size())
125  ? rfind(s, pos-1, not_found)
126  : not_found) )));
127  }
128 
129  inline std::string to_string() const { return std::string(_p, _sz); }
130 };
131 
132 
133 
134 } // namespace Tools
135 } // namespace Tomographer
136 
137 
138 
139 
140 
141 
142 
143 #endif
Base namespace for the Tomographer project.
Definition: dmmhrw.h:51
STL class.
A constexpr string type.
Definition: conststr.h:54