/usr/include/loki/flex/smallstringopt.h is in libloki-dev 0.1.7-3.
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | ////////////////////////////////////////////////////////////////////////////////
// flex_string
// Copyright (c) 2001 by Andrei Alexandrescu
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
// The author makes no representations about the
// suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
#ifndef SMALL_STRING_OPT_INC_
#define SMALL_STRING_OPT_INC_
// $Id: smallstringopt.h 836 2007-09-20 15:51:37Z aandrei $
////////////////////////////////////////////////////////////////////////////////
// class template SmallStringOpt
// Builds the small string optimization over any other storage
////////////////////////////////////////////////////////////////////////////////
/* This is the template for a storage policy
////////////////////////////////////////////////////////////////////////////////
template <typename E, class A = @>
class StoragePolicy
{
typedef E value_type;
typedef @ iterator;
typedef @ const_iterator;
typedef A allocator_type;
typedef @ size_type;
StoragePolicy(const StoragePolicy& s);
StoragePolicy(const A&);
StoragePolicy(const E* s, size_type len, const A&);
StoragePolicy(size_type len, E c, const A&);
~StoragePolicy();
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
size_type size() const;
size_type max_size() const;
size_type capacity() const;
void reserve(size_type res_arg);
void append(const E* s, size_type sz);
template <class InputIterator>
void append(InputIterator b, InputIterator e);
void resize(size_type newSize, E fill);
void swap(StoragePolicy& rhs);
const E* c_str() const;
const E* data() const;
A get_allocator() const;
};
////////////////////////////////////////////////////////////////////////////////
*/
#include <memory>
#include <algorithm>
#include <functional>
#include <cassert>
#include <limits>
#include <stdexcept>
#include "flex_string_details.h"
////////////////////////////////////////////////////////////////////////////////
// class template SmallStringOpt
// Builds the small string optimization over any other storage
////////////////////////////////////////////////////////////////////////////////
template <class Storage, unsigned int threshold,
typename Align = typename Storage::value_type*>
class SmallStringOpt
{
public:
typedef typename Storage::value_type value_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef typename Storage::allocator_type allocator_type;
typedef typename allocator_type::size_type size_type;
typedef typename Storage::reference reference;
private:
enum { temp1 = threshold * sizeof(value_type) > sizeof(Storage)
? threshold * sizeof(value_type)
: sizeof(Storage) };
enum { temp2 = temp1 > sizeof(Align) ? temp1 : sizeof(Align) };
public:
enum { maxSmallString =
(temp2 + sizeof(value_type) - 1) / sizeof(value_type) };
private:
enum { magic = maxSmallString + 1 };
union
{
mutable value_type buf_[maxSmallString + 1];
Align align_;
};
Storage& GetStorage()
{
assert(buf_[maxSmallString] == magic);
Storage* p = reinterpret_cast<Storage*>(&buf_[0]);
return *p;
}
const Storage& GetStorage() const
{
assert(buf_[maxSmallString] == magic);
const Storage *p = reinterpret_cast<const Storage*>(&buf_[0]);
return *p;
}
bool Small() const
{
return buf_[maxSmallString] != magic;
}
public:
SmallStringOpt(const SmallStringOpt& s)
{
if (s.Small())
{
flex_string_details::pod_copy(
s.buf_,
s.buf_ + s.size(),
buf_);
}
else
{
new(buf_) Storage(s.GetStorage());
}
buf_[maxSmallString] = s.buf_[maxSmallString];
}
SmallStringOpt(const allocator_type&)
{
buf_[maxSmallString] = maxSmallString;
}
SmallStringOpt(const value_type* s, size_type len, const allocator_type& a)
{
if (len <= maxSmallString)
{
flex_string_details::pod_copy(s, s + len, buf_);
buf_[maxSmallString] = value_type(maxSmallString - len);
}
else
{
new(buf_) Storage(s, len, a);
buf_[maxSmallString] = magic;
}
}
SmallStringOpt(size_type len, value_type c, const allocator_type& a)
{
if (len <= maxSmallString)
{
flex_string_details::pod_fill(buf_, buf_ + len, c);
buf_[maxSmallString] = value_type(maxSmallString - len);
}
else
{
new(buf_) Storage(len, c, a);
buf_[maxSmallString] = magic;
}
}
// Fix suggested by Andrew Barnert on 07/03/2007
SmallStringOpt& operator=(const SmallStringOpt& rhs)
{
if (&rhs == this) return *this;
const size_t rhss = rhs.size();
// Will we use this' allocated buffer?
if (rhss > maxSmallString && capacity() > rhss) {
const size_t s = size();
if (s >= rhss) {
// shrink
resize(rhss, 0);
std::copy(rhs.begin(), rhs.end(), begin());
} else {
// grow
std::copy(rhs.begin(), rhs.begin() + s, begin());
append(rhs.begin() + s, rhs.end());
}
} else {
// this' buffer is useless
if (rhs.Small()) {
// Just destroy and copy over (ugly but efficient)
// Works because construction of a small string can't fail
if (!Small()) this->~SmallStringOpt();
new(this) SmallStringOpt(rhs);
} else {
SmallStringOpt copy(rhs);
if (Small()) {
// no need to swap, just destructively read copy into this
// ugly but efficient again
memcpy(this, ©, sizeof(*this));
copy.buf_[maxSmallString] = maxSmallString; // clear the copy
} else {
// Use the swap trick
copy.swap(*this);
}
}
}
return *this;
}
~SmallStringOpt()
{
if (!Small()) GetStorage().~Storage();
}
iterator begin()
{
if (Small()) return buf_;
return &*GetStorage().begin();
}
const_iterator begin() const
{
if (Small()) return buf_;
return &*GetStorage().begin();
}
iterator end()
{
if (Small()) return buf_ + maxSmallString - buf_[maxSmallString];
return &*GetStorage().end();
}
const_iterator end() const
{
if (Small()) return buf_ + maxSmallString - buf_[maxSmallString];
return &*GetStorage().end();
}
size_type size() const
{
assert(!Small() || maxSmallString >= buf_[maxSmallString]);
return Small()
? maxSmallString - buf_[maxSmallString]
: GetStorage().size();
}
size_type max_size() const
{ return get_allocator().max_size(); }
size_type capacity() const
{ return Small() ? maxSmallString : GetStorage().capacity(); }
void reserve(size_type res_arg)
{
if (Small())
{
if (res_arg <= maxSmallString) return;
SmallStringOpt temp(*this);
this->~SmallStringOpt();
new(buf_) Storage(temp.data(), temp.size(),
temp.get_allocator());
buf_[maxSmallString] = magic;
GetStorage().reserve(res_arg);
}
else
{
GetStorage().reserve(res_arg);
}
assert(capacity() >= res_arg);
}
template <class FwdIterator>
void append(FwdIterator b, FwdIterator e)
{
if (!Small())
{
GetStorage().append(b, e);
}
else
{
// append to a small string
const size_type
sz = std::distance(b, e),
neededCapacity = maxSmallString - buf_[maxSmallString] + sz;
if (maxSmallString < neededCapacity)
{
// need to change storage strategy
allocator_type alloc;
Storage temp(alloc);
temp.reserve(neededCapacity);
temp.append(buf_, buf_ + maxSmallString - buf_[maxSmallString]);
temp.append(b, e);
buf_[maxSmallString] = magic;
new(buf_) Storage(temp.get_allocator());
GetStorage().swap(temp);
}
else
{
std::copy(b, e, buf_ + maxSmallString - buf_[maxSmallString]);
buf_[maxSmallString] -= value_type(sz);
}
}
}
void resize(size_type n, value_type c)
{
if (Small())
{
if (n > maxSmallString)
{
// Small string resized to big string
SmallStringOpt temp(*this); // can't throw
// 11-17-2001: correct exception safety bug
Storage newString(temp.data(), temp.size(),
temp.get_allocator());
newString.resize(n, c);
// We make the reasonable assumption that an empty Storage
// constructor won't throw
this->~SmallStringOpt();
new(&buf_[0]) Storage(temp.get_allocator());
buf_[maxSmallString] = value_type(magic);
GetStorage().swap(newString);
}
else
{
// Small string resized to small string
// 11-17-2001: bug fix: terminating zero not copied
size_type toFill = n > size() ? n - size() : 0;
flex_string_details::pod_fill(end(), end() + toFill, c);
buf_[maxSmallString] = value_type(maxSmallString - n);
}
}
else
{
if (n > maxSmallString)
{
// Big string resized to big string
GetStorage().resize(n, c);
}
else
{
// Big string resized to small string
// 11-17=2001: bug fix in the assertion below
assert(capacity() > n);
// The following two commented-out lines were fixed by
// Jean-Francois Bastien, 07/26/2007
//SmallStringOpt newObj(data(), n, get_allocator());
// newObj.swap(*this);
if (n <= size()) {
SmallStringOpt newObj(data(), n, get_allocator());
newObj.swap(*this);
} else {
SmallStringOpt newObj(data(), size(), get_allocator());
newObj.resize(n, c); // invoke this function recursively
newObj.swap(*this);
}
}
}
}
void swap(SmallStringOpt& rhs)
{
if (Small())
{
if (rhs.Small())
{
// Small swapped with small
std::swap_ranges(buf_, buf_ + maxSmallString + 1,
rhs.buf_);
}
else
{
// Small swapped with big
// Make a copy of myself - can't throw
SmallStringOpt temp(*this);
// Nuke myself
this->~SmallStringOpt();
// Make an empty storage for myself (likely won't throw)
new(buf_) Storage(0, value_type(), rhs.get_allocator());
buf_[maxSmallString] = magic;
// Recurse to this same function
swap(rhs);
// Nuke rhs
rhs.~SmallStringOpt();
// Build the new small string into rhs
new(&rhs) SmallStringOpt(temp);
}
}
else
{
if (rhs.Small())
{
// Big swapped with small
// Already implemented, recurse with reversed args
rhs.swap(*this);
}
else
{
// Big swapped with big
GetStorage().swap(rhs.GetStorage());
}
}
}
const value_type* c_str() const
{
if (!Small()) return GetStorage().c_str();
buf_[maxSmallString - buf_[maxSmallString]] = value_type();
return buf_;
}
const value_type* data() const
{ return Small() ? buf_ : GetStorage().data(); }
allocator_type get_allocator() const
{ return allocator_type(); }
};
#endif // SMALL_STRING_OPT_INC_
|