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