/usr/include/OpenImageIO/string_ref.h is in libopenimageio-dev 1.3.12~dfsg0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | /*
Copyright 2014 Larry Gritz and the other authors and contributors.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the software's owners nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(This is the Modified BSD License)
*/
#pragma once
#include <cstddef>
#include <string>
#include <cstring>
#include <stdexcept>
#include <algorithm>
#include "version.h"
#include "export.h"
OIIO_NAMESPACE_ENTER {
/// Provides a string_ref class that is a non-owning reference to a string
/// or substring.
class string_ref {
public:
typedef char charT;
typedef charT value_type;
typedef const charT* pointer;
typedef const charT* const_pointer;
typedef const charT& reference;
typedef const charT& const_reference;
typedef const_pointer const_iterator;
typedef const_iterator iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef const_reverse_iterator reverse_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef std::char_traits<charT> traits;
static const size_type npos = ~size_type(0);
/// Default ctr
string_ref () { init(NULL,0); }
/// Copy ctr
string_ref (const string_ref ©) {
init (copy.m_chars, copy.m_len);
}
/// Construct from char* and length.
string_ref (const charT *chars, size_t len) { init (chars, len); }
/// Construct from char*, use strlen to determine length.
string_ref (const charT *chars) {
init (chars, chars ? strlen(chars) : 0);
}
/// Construct from std::string.
string_ref (const std::string &str) { init (str.data(), str.size()); }
std::string str() const {
return (m_chars ? std::string(m_chars,m_len) : std::string());
}
/// Explicitly request a 0-terminated string. USUALLY, this turns out to
/// be just data(), with no significant added expense. But in the more
/// rare case that the string_ref represetns a non-0-terminated
/// substring, it will force an allocation and copy underneath.
const char * c_str() const;
// assignments
string_ref& operator= (const string_ref ©) {
init (copy.data(), copy.length());
return *this;
}
operator std::string() const { return str(); }
// iterators
const_iterator begin() const { return m_chars; }
const_iterator end() const { return m_chars + m_len; }
const_iterator cbegin() const { return m_chars; }
const_iterator cend() const { return m_chars + m_len; }
const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); }
const_reverse_iterator rend() const { return const_reverse_iterator (begin()); }
const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); }
const_reverse_iterator crend() const { return const_reverse_iterator (begin()); }
// capacity
size_type size() const { return m_len; }
size_type length() const { return m_len; }
size_type max_size() const { return m_len; }
bool empty() const { return m_len == 0; }
// element access
const charT& operator[] (size_type pos) const { return m_chars[pos]; }
const charT& at (size_t pos) const {
if (pos >= m_len)
throw (std::out_of_range ("OpenImageIO::string_ref::at"));
return m_chars[pos];
}
const charT& front() const { return m_chars[0]; }
const charT& back() const { return m_chars[m_len-1]; }
const charT* data() const { return m_chars; }
// modifiers
void clear() { init(NULL,0); }
void remove_prefix(size_type n) {
if (n > m_len)
n = m_len;
m_chars += n;
m_len -= n;
}
void remove_suffix(size_type n) {
if (n > m_len)
n = m_len;
m_len -= n;
}
string_ref substr (size_type pos, size_type n=npos) const {
if (pos > size())
throw (std::out_of_range ("OIIO::string_ref::substr"));
if (n == npos || pos + n > size())
n = size() - pos;
return string_ref (data() + pos, n);
}
int compare (string_ref x) const {
const int cmp = std::char_traits<char>::compare (m_chars, x.m_chars, (std::min)(m_len, x.m_len));
return cmp != 0 ? cmp : int(m_len - x.m_len);
// Equivalent to:
// cmp != 0 ? cmp : (m_len == x.m_len ? 0 : (m_len < x.m_len ? -1 : 1));
}
#if 0
// Do these later if anybody needs them
bool starts_with(string_ref x) const;
bool ends_with(string_ref x) const;
#endif
/// Find the first occurrence of substring s in *this, starting at
/// position pos.
size_type find (string_ref s, size_t pos=0) const {
if (pos > size())
pos = size();
const_iterator i = std::search (this->cbegin()+pos, this->cend(),
s.cbegin(), s.cend(), traits::eq);
return i == this->cend() ? npos : std::distance (this->cbegin(), i);
}
/// Find the first occurrence of character c in *this, starting at
/// position pos.
size_type find (charT c, size_t pos=0) const {
if (pos > size())
pos = size();
const_iterator i = std::find_if (this->cbegin()+pos, this->cend(),
traits_eq(c));
return i == this->cend() ? npos : std::distance (this->cbegin(), i);
}
/// Find the last occurrence of substring s *this, but only those
/// occurrences earlier than position pos.
size_type rfind (string_ref s, size_t pos=npos) const {
if (pos > size())
pos = size();
const_reverse_iterator b = this->crbegin()+(size()-pos);
const_reverse_iterator e = this->crend();
const_reverse_iterator i = std::search (b, e, s.crbegin(), s.crend(), traits::eq);
return i == e ? npos : (reverse_distance(this->crbegin(),i) - s.size() + 1);
}
/// Find the last occurrence of character c in *this, but only those
/// occurrences earlier than position pos.
size_type rfind (charT c, size_t pos=npos) const {
if (pos > size())
pos = size();
const_reverse_iterator b = this->crbegin()+(size()-pos);
const_reverse_iterator e = this->crend();
const_reverse_iterator i = std::find_if (b, e, traits_eq(c));
return i == e ? npos : reverse_distance (this->crbegin(),i);
}
size_type find_first_of (charT c, size_t pos=0) const { return find (c, pos); }
size_type find_last_of (charT c, size_t pos=npos) const { return rfind (c, pos); }
size_type find_first_of (string_ref s) const {
const_iterator i = std::find_first_of (this->cbegin(), this->cend(),
s.cbegin(), s.cend(), traits::eq);
return i == this->cend() ? npos : std::distance (this->cbegin(), i);
}
size_type find_last_of (string_ref s) const {
const_reverse_iterator i = std::find_first_of (this->crbegin(), this->crend(),
s.cbegin(), s.cend(), traits::eq);
return i == this->crend() ? npos : reverse_distance (this->crbegin(), i);
}
size_type find_first_not_of (string_ref s) const {
const_iterator i = find_not_of (this->cbegin(), this->cend(), s);
return i == this->cend() ? npos : std::distance (this->cbegin(), i);
}
size_type find_first_not_of (charT c) const {
for (const_iterator i = this->cbegin(); i != this->cend(); ++i)
if (! traits::eq (c, *i))
return std::distance (this->cbegin(), i);
return npos;
}
size_type find_last_not_of (string_ref s) const {
const_reverse_iterator i = find_not_of (this->crbegin(), this->crend(), s);
return i == this->crend() ? npos : reverse_distance (this->crbegin(), i);
}
size_type find_last_not_of (charT c) const {
for (const_reverse_iterator i = this->crbegin(); i != this->crend(); ++i)
if (! traits::eq (c, *i))
return reverse_distance (this->crbegin(), i);
return npos;
}
private:
const charT * m_chars;
size_t m_len;
void init (const charT *chars, size_t len) {
m_chars = chars;
m_len = len;
}
template <typename r_iter>
size_type reverse_distance (r_iter first, r_iter last) const {
return m_len - 1 - std::distance (first, last);
}
template <typename iter>
iter find_not_of (iter first, iter last, string_ref s) const {
for ( ; first != last ; ++first)
if (! traits::find (s.data(), s.length(), *first))
return first;
return last;
}
class traits_eq {
public:
traits_eq (charT ch) : ch(ch) {}
bool operator () (charT val) const { return traits::eq (ch, val); }
charT ch;
};
};
inline bool operator== (string_ref x, string_ref y) {
return x.size() == y.size() ? (x.compare (y) == 0) : false;
}
inline bool operator!= (string_ref x, string_ref y) {
return x.size() == y.size() ? (x.compare (y) != 0) : true;
}
inline bool operator< (string_ref x, string_ref y) {
return x.compare(y) < 0;
}
inline bool operator> (string_ref x, string_ref y) {
return x.compare(y) > 0;
}
inline bool operator<= (string_ref x, string_ref y) {
return x.compare(y) <= 0;
}
inline bool operator>= (string_ref x, string_ref y) {
return x.compare(y) >= 0;
}
// Inserter
inline std::ostream& operator<< (std::ostream& out, const string_ref& str) {
if (out.good())
out.write (str.data(), str.size());
return out;
}
} OIIO_NAMESPACE_EXIT
|