Alien-libpanda

 view release on metacpan or  search on metacpan

src/panda/basic_string.h  view on Meta::CPAN

        CharT*  ptr;  // pointer to external data originally passed to string's constructor
    };
}

template <class T>
struct DefaultStaticAllocator {
    typedef T value_type;

    static T* allocate (size_t n) {
        void* mem = malloc(n * sizeof(T));
        if (!mem) throw std::bad_alloc();
        return (T*)mem;
    }

    static void deallocate (T* mem, size_t) {
        free(mem);
    }

    static T* reallocate (T* mem, size_t need, size_t /*old*/) {
        void* new_mem = realloc(mem, need * sizeof(T));
        //if (new_mem != mem) { call move constructors if applicable }

src/panda/basic_string.h  view on Meta::CPAN

    constexpr const_reverse_iterator crend   () const { return const_reverse_iterator(cbegin()); }
    constexpr const_reverse_iterator rend    () const { return crend(); }

    explicit
    constexpr operator bool () const { return _length; }

    operator std::basic_string<CharT,Traits> () const { return std::basic_string<CharT,Traits>(_str, _length); }
    operator basic_string_view<CharT,Traits> () const { return basic_string_view<CharT,Traits>(_str, _length); }

    const CharT& at (size_type pos) const {
        if (pos >= _length) throw std::out_of_range("basic_string::at");
        return _str[pos];
    }

    mutable_charref at (size_type pos) {
        if (pos >= _length) throw std::out_of_range("basic_string::at");
        return mutable_charref{ *this, pos };
    }

    constexpr const CharT& operator[] (size_type pos) const { return _str[pos]; }
    mutable_charref        operator[] (size_type pos)       { return mutable_charref{ *this, pos }; }

    constexpr const CharT& front () const { return _str[0]; }
    constexpr const CharT& back  () const { return _str[_length-1]; }
    mutable_charref        front ()       { return mutable_charref{ *this, 0 }; }
    mutable_charref        back  ()       { return mutable_charref{ *this, _length-1 }; }

src/panda/basic_string.h  view on Meta::CPAN

            case State::INTERNAL:
            case State::EXTERNAL:
                return _storage.any->refcnt;
            default: return 1;
        }
    }

    void length (size_type newlen) { _length = newlen; }

    void offset (size_type offset, size_type length = npos) {
        if (offset > _length) throw std::out_of_range("basic_string::offset");
        if (length > _length - offset) _length = _length - offset;
        else _length = length;
        _str += offset;
    }

    basic_string substr (size_type offset = 0, size_type length = npos) const {
        return basic_string(*this, offset, length);
    }

    void resize (size_type count) { resize(count, CharT()); }

src/panda/basic_string.h  view on Meta::CPAN

        std::swap(_length, oth._length);
        if (_state == State::SSO) oth._str = oth._sso + (oth._str - _sso);
        if (oth._state == State::SSO) _str = _sso + (_str - oth._sso);
        // swap union & state after it
        std::swap(((void**)__fill)[0], ((void**)oth.__fill)[0]);
        std::swap(((void**)__fill)[1], ((void**)oth.__fill)[1]);
        std::swap(((void**)__fill)[2], ((void**)oth.__fill)[2]);
    }

    size_type copy (CharT* dest, size_type count, size_type pos = 0) const {
        if (pos > _length) throw std::out_of_range("basic_string::copy");
        if (count > _length - pos) count = _length - pos;
        traits_type::copy(dest, _str + pos, count);
        return count;
    }

    basic_string& erase (size_type pos = 0, size_type count = npos) {
        if (pos > _length) throw std::out_of_range("basic_string::erase");

        if (count > _length - pos) { // remove trail
            _length = pos;
            return *this;
        }

        _length -= count;

        if (pos == 0) { // remove head
            _str += count;

src/panda/basic_string.h  view on Meta::CPAN

        return cbegin() + pos;
    }

    template <class Alloc2>
    int compare (const basic_string<CharT, Traits, Alloc2>& str) const {
        return _compare(_str, _length, str._str, str._length);
    }

    template <class Alloc2>
    int compare (size_type pos1, size_type count1, const basic_string<CharT, Traits, Alloc2>& str) const {
        if (pos1 > _length) throw std::out_of_range("basic_string::compare");
        if (count1 > _length - pos1) count1 = _length - pos1;
        return _compare(_str + pos1, count1, str._str, str._length);
    }

    template <class Alloc2>
    int compare (size_type pos1, size_type count1, const basic_string<CharT, Traits, Alloc2>& str, size_type pos2, size_type count2 = npos) const {
        if (pos1 > _length || pos2 > str._length) throw std::out_of_range("basic_string::compare");
        if (count1 > _length - pos1) count1 = _length - pos1;
        if (count2 > str._length - pos2) count2 = str._length - pos2;
        return _compare(_str + pos1, count1, str._str + pos2, count2);
    }

    template<class _CharT, typename = typename std::enable_if<std::is_same<_CharT, CharT>::value>::type>
    int compare (const _CharT* const& s) const {
        return _compare(_str, _length, s, traits_type::length(s));
    }

src/panda/basic_string.h  view on Meta::CPAN

    int compare (size_type pos1, size_type count1, const _CharT* const& s) const {
        return compare(pos1, count1, s, traits_type::length(s));
    }

    template <size_type SIZE>
    int compare (size_type pos1, size_type count1, const CharT (&s)[SIZE]) const {
        return compare(pos1, count1, s, SIZE-1);
    }

    int compare (size_type pos1, size_type count1, const CharT* ptr, size_type count2) const {
        if (pos1 > _length) throw std::out_of_range("basic_string::compare");
        if (count1 > _length - pos1) count1 = _length - pos1;
        return _compare(_str + pos1, count1, ptr, count2);
    }

    int compare (basic_string_view<CharT, Traits> sv) const {
        return _compare(_str, _length, sv.data(), sv.length());
    }

    int compare (size_type pos1, size_type count1, basic_string_view<CharT, Traits> sv) const {
        return compare(pos1, count1, sv.data(), sv.length());

src/panda/basic_string.h  view on Meta::CPAN

        if (str._length) { // can't call append(const CharT*, size_type) because otherwise if &str == this a fuckup would occur
            _reserve_save(_length + str._length);
            traits_type::copy(_str + _length, str._str, str._length);
            _length += str._length;
        }
        return *this;
    }

    template <class Alloc2>
    basic_string& append (const basic_string<CharT, Traits, Alloc2>& str, size_type pos, size_type count = npos) {
        if (pos > str._length) throw std::out_of_range("basic_string::append");
        if (count > str._length - pos) count = str._length - pos;
        if (count) { // can't call append(const CharT*, size_type) because otherwise if &str == this a fuckup would occur
            _reserve_save(_length + count);
            traits_type::copy(_str + _length, str._str + pos, count);
            _length += count;
        }
        return *this;
    }

    basic_string& append (const CharT* s, size_type count) { // 's' MUST NOT BE any part of this->data()

src/panda/basic_string.h  view on Meta::CPAN

        }
        else return insert(pos, str._str, str._length);
    }

    template <class Alloc2>
    basic_string& insert (size_type pos, const basic_string<CharT, Traits, Alloc2>& str) {
        return insert(pos, str._str, str._length);
    }

    basic_string& insert (size_type pos, const basic_string& str, size_type subpos, size_type sublen = npos) {
        if (subpos > str._length) throw std::out_of_range("basic_string::insert");
        if (sublen > str._length - subpos) sublen = str._length - subpos;
        if (this == &str) {
            const basic_string tmp(str);
            return insert(pos, tmp._str + subpos, sublen);
        }
        else return insert(pos, str._str + subpos, sublen);
    }

    template <class Alloc2>
    basic_string& insert (size_type pos, const basic_string<CharT, Traits, Alloc2>& str, size_type subpos, size_type sublen = npos) {
        if (subpos > str._length) throw std::out_of_range("basic_string::insert");
        if (sublen > str._length - subpos) sublen = str._length - subpos;
        return insert(pos, str._str + subpos, sublen);
    }

    template<class _CharT, typename = typename std::enable_if<std::is_same<_CharT, CharT>::value>::type>
    basic_string& insert (size_type pos, const _CharT* const& s) {
        return insert(pos, s, traits_type::length(s));
    }

    template <size_type SIZE>
    basic_string& insert (size_type pos, const CharT (&s)[SIZE]) {
        return insert(pos, s, SIZE-1);
    }

    basic_string& insert (size_type pos, const CharT* s, size_type count) {
        if (pos >= _length) {
            if (pos == _length) return append(s, count);
            throw std::out_of_range("basic_string::insert");
        }
        if (count == 0) return *this;
        _reserve_middle(pos, 0, count);
        traits_type::copy(_str + pos, s, count);
        return *this;
    }

    basic_string& insert (size_type pos, size_type count, CharT ch) {
        if (pos >= _length) {
            if (pos == _length) return append(count, ch);
            throw std::out_of_range("basic_string::insert");
        }
        if (count == 0) return *this;
        _reserve_middle(pos, 0, count);
        traits_type::assign(_str + pos, count, ch);
        return *this;
    }

    iterator insert (const_iterator it, size_type count, CharT ch) {
        size_type pos = it - cbegin();
        insert(pos, count, ch);

src/panda/basic_string.h  view on Meta::CPAN

    basic_string& replace (size_type pos, size_type remove_count, const basic_string<CharT, Traits, Alloc2>& str) {
        return replace(pos, remove_count, str._str, str._length);
    }

    template <class Alloc2>
    basic_string& replace (const_iterator first, const_iterator last, const basic_string<CharT, Traits, Alloc2>& str) {
        return replace(first - cbegin(), last - first, str);
    }

    basic_string& replace (size_type pos, size_type remove_count, const basic_string& str, size_type pos2, size_type insert_count = npos) {
        if (pos2 > str._length) throw std::out_of_range("basic_string::replace");
        if (insert_count > str._length - pos2) insert_count = str._length - pos2;
        if (this == &str) {
            const basic_string tmp(str);
            return replace(pos, remove_count, tmp._str + pos2, insert_count);
        }
        return replace(pos, remove_count, str._str + pos2, insert_count);
    }

    template <class Alloc2>
    basic_string& replace (size_type pos, size_type remove_count, const basic_string<CharT, Traits, Alloc2>& str, size_type pos2, size_type insert_count = npos) {
        if (pos2 > str._length) throw std::out_of_range("basic_string::replace");
        if (insert_count > str._length - pos2) insert_count = str._length - pos2;
        return replace(pos, remove_count, str._str + pos2, insert_count);
    }

    basic_string& replace (size_type pos, size_type remove_count, const CharT* s, size_type insert_count) {
        if (pos >= _length) {
            if (pos == _length) return append(s, insert_count);
            throw std::out_of_range("basic_string::replace");
        }
        if (remove_count >= _length - pos) {
            _length = pos;
            return append(s, insert_count);
        }
        if (insert_count == 0) {
            if (remove_count == 0) return *this;
            return erase(pos, remove_count);
        }
        _reserve_middle(pos, remove_count, insert_count);

src/panda/basic_string.h  view on Meta::CPAN

    }

    template <size_type SIZE>
    basic_string& replace (const_iterator first, const_iterator last, const CharT (&s)[SIZE]) {
        return replace(first, last, s, SIZE-1);
    }

    basic_string& replace (size_type pos, size_type remove_count, size_type insert_count, CharT ch) {
        if (pos >= _length) {
            if (pos == _length) return append(insert_count, ch);
            throw std::out_of_range("basic_string::replace");
        }
        if (remove_count >= _length - pos) {
            _length = pos;
            return append(insert_count, ch);
        }
        if (insert_count == 0) {
            if (remove_count == 0) return *this;
            return erase(pos, remove_count);
        }
        _reserve_middle(pos, remove_count, insert_count);

src/panda/basic_string.h  view on Meta::CPAN


    basic_string& replace (const_iterator first, const_iterator last, basic_string_view<CharT, Traits> sv) {
        return replace(first - cbegin(), last - first, sv);
    }

    template <typename V>
    from_chars_result to_number (V& value, int base = 10) const { return from_chars(_str, _str + _length, value, base); }

    template <typename V>
    from_chars_result to_number (V& value, size_type pos, size_type count = npos, int base = 10) const {
        if (pos > _length) throw std::out_of_range("basic_string::to_number");
        if (count > _length - pos) count = _length - pos;
        return from_chars(_str + pos, _str + pos + count, value, base);
    }

    template <typename V>
    static basic_string from_number (V value, int base = 10) {
        auto maxsz = to_chars_maxsize<V>(base);
        basic_string ret(maxsz);
        auto res = to_chars(ret._str, ret._str + maxsz, value, base);
        assert(!res.ec);

src/panda/basic_string.h  view on Meta::CPAN


    constexpr size_type _capacity_internal () const { return _storage.internal->capacity - (_str - _storage.internal->start); }
    constexpr size_type _capacity_external () const { return _storage.external->capacity - (_str - _storage.external->ptr); }
    constexpr size_type _capacity_sso      () const { return MAX_SSO_CHARS - (_str - _sso); }

    void _new_auto (size_type capacity) {
        if (capacity <= MAX_SSO_CHARS) {
            _state = State::SSO;
            _str   = _sso;
        } else {
            if (capacity > MAX_SIZE) throw std::length_error("basic_string::_new_auto");
            _state                      = State::INTERNAL;
            _storage.internal           = (Buffer*)Alloc::allocate(capacity + BUF_CHARS);
            _storage.internal->capacity = capacity;
            _storage.internal->refcnt   = 1;
            _str                        = _storage.internal->start;
            _storage.dtor               = &Alloc::deallocate;
        }
    }

    // becomes INTERNAL for capacity, and copy _str to buffer in the way so that none of internal SSO members are written before copy is made.

src/panda/basic_string.h  view on Meta::CPAN

                break;
            case State::SSO:
                memcpy(__fill, oth.__fill, MAX_SSO_BYTES+1); // also sets _state to SSO
                _str = _sso + (oth._str - oth._sso) + offset;
                break;
        }
    }

    template <class Alloc2>
    void _cow_offset (const basic_string<CharT, Traits, Alloc2>& oth, size_type offset, size_type length) {
        if (offset > oth._length) throw std::out_of_range("basic_string::assign");
        if (length > oth._length - offset) length = oth._length - offset;
        _cow(oth, offset, length);
    }

    template <class Alloc2>
    void _move_from (basic_string<CharT, Traits, Alloc2>&& oth) {
        _length = oth._length;
        memcpy(__fill, oth.__fill, MAX_SSO_BYTES+1); // also sets _state
        //#pragma GCC diagnostic pop
        if (oth._state == State::SSO) _str = _sso + (oth._str - oth._sso);

src/panda/basic_string.h  view on Meta::CPAN

        else if (_capacity_internal() < capacity) { // may not to grow storage if str is moved to the beginning
            traits_type::move(_storage.internal->start, _str, _length);
            _str = _storage.internal->start;
        }
    }

    void _internal_realloc (size_type capacity) {
        // see if we can reallocate. if _str != start we should not reallocate because we would need
        // either allocate more space than needed or move everything to the beginning before reallocation
        if (_storage.dtor == &Alloc::deallocate && _str == _storage.internal->start) {
            if (capacity > MAX_SIZE) throw std::length_error("basic_string::_internal_realloc");
            _storage.internal = (Buffer*)Alloc::reallocate((CharT*)_storage.internal, capacity + BUF_CHARS, _storage.internal->capacity + BUF_CHARS);
            _str = _storage.internal->start;
            _storage.internal->capacity = capacity;
        } else { // need to allocate/deallocate
            auto old_buf  = _storage.internal;
            auto old_str  = _str;
            auto old_dtor = _storage.dtor;
            _new_auto(capacity);
            traits_type::copy(_str, old_str, _length);
            _free_internal(old_buf, old_dtor);

src/panda/basic_string_view.h  view on Meta::CPAN

    constexpr const_iterator         end     () const { return _str + _length; }
    constexpr const_iterator         cend    () const { return end(); }
    constexpr const_reverse_iterator rbegin  () const { return const_reverse_iterator(end()); }
    constexpr const_reverse_iterator crbegin () const { return rbegin(); }
    constexpr const_reverse_iterator rend    () const { return const_reverse_iterator(begin()); }
    constexpr const_reverse_iterator crend   () const { return rend(); }

    constexpr const_reference operator[] (size_t pos) const { return _str[pos]; }

    const_reference at (size_t pos) const {
        if (pos >= _length) throw std::out_of_range("basic_string_view::at");
        return _str[pos];
    }

    constexpr const_reference front    () const { return *_str; }
    constexpr const_reference back     () const { return _str[_length-1]; }
    constexpr const_pointer   data     () const { return _str; }
    constexpr size_t          size     () const { return _length; }
    constexpr size_t          length   () const { return _length; }
    constexpr size_t          max_size () const { return npos - 1; }
    constexpr bool            empty    () const { return _length == 0; }

src/panda/basic_string_view.h  view on Meta::CPAN

    void remove_suffix (size_t n) {
        _length -= n;
    }

    void swap (basic_string_view& v) {
        std::swap(_str, v._str);
        std::swap(_length, v._length);
    }

    size_t copy (CharT* dest, size_t count, size_t pos = 0) const {
        if (pos > _length) throw std::out_of_range("basic_string_view::copy");
        if (count > _length - pos) count = _length - pos;
        traits_type::copy(dest, _str, count);
        return count;
    }

    basic_string_view substr (size_t pos = 0, size_t count = npos) const {
        if (pos > _length) throw std::out_of_range("basic_string_view::substr");
        if (count > _length - pos) count = _length - pos;
        return basic_string_view(_str + pos, count);
    }


    int compare (basic_string_view v) const {
        return _compare(_str, _length, v._str, v._length);
    }

    int compare (size_t pos1, size_t count1, basic_string_view v) const {
        return _compare(pos1, count1, v._str, v._length);
    }

    int compare (size_t pos1, size_t count1, basic_string_view v, size_t pos2, size_t count2) const {
        if (pos2 > v._length) throw std::out_of_range("basic_string_view::compare");
        if (count2 > v._length - pos2) count2 = v._length - pos2;
        return _compare(pos1, count1, v._str + pos2, count2);
    }

    template<class _CharT, typename = typename std::enable_if<std::is_same<_CharT, CharT>::value>::type>
    int compare (const CharT* const& s) const {
        return _compare(_str, _length, s, traits_type::length(s));
    }

    template <size_t SIZE>

src/panda/basic_string_view.h  view on Meta::CPAN

    int compare (size_t pos1, size_t count1, const CharT* const& s) const {
        return compare(pos1, count1, s, traits_type::length(s));
    }

    template <size_t SIZE>
    int compare (size_t pos1, size_t count1, const CharT (&s)[SIZE]) const {
        return compare(pos1, count1, s, SIZE-1);
    }

    int compare (size_t pos1, size_t count1, const CharT* s, size_t count2) const {
        if (pos1 > _length) throw std::out_of_range("basic_string_view::compare");
        if (count1 > _length - pos1) count1 = _length - pos1;
        return _compare(_str + pos1, count1, s, count2);
    }


    size_t find (basic_string_view v, size_t pos = 0) const {
        return find(v._str, pos, v._length);
    }

    size_t find (CharT ch, size_t pos = 0) const {

src/panda/excepted.h  view on Meta::CPAN

#pragma once
#include "expected.h"

namespace panda {

namespace {
    template <class E>
    inline typename std::enable_if<!std::is_base_of<std::exception, typename std::decay<E>::type>::value, void>::type exthrow (E&& e) {
        throw bad_expected_access<typename std::decay<E>::type>(std::forward<E>(e));
    }

    template <class E>
    inline typename std::enable_if<std::is_base_of<std::exception, typename std::decay<E>::type>::value, void>::type exthrow (E&& e) {
        throw std::forward<E>(e);
    }
}

template <class T, class E>
struct excepted {
    using value_type = T;
    using error_type = E;
    using unexpected_type = unexpected<E>;

    template <typename = typename std::enable_if<std::is_default_constructible<T>::value>::type>

src/panda/excepted.h  view on Meta::CPAN

    excepted (unexpected<E2>&& uex) {
        construct_err(std::move(uex.value()));
    }

    ~excepted () noexcept(false) {
        if      (_has_val) _val.~T();
        else if (_checked) _err.~E();
        else {
            auto tmp = std::move(_err);
            _err.~E();
            exthrow(std::move(tmp));
        }
    }

    excepted& operator= (const excepted& ex) {
        if (ex._has_val) set_val(ex._val);
        else {
            set_err(ex._err);
            ex._checked = true;
        }
        return *this;

src/panda/excepted.h  view on Meta::CPAN

    }

    template <class E2>
    excepted& operator= (unexpected<E2>&& uex) {
        set_err(std::move(uex.value()));
    }

    constexpr bool     has_value     () const noexcept { _checked = true; return _has_val; }
    constexpr explicit operator bool () const noexcept { _checked = true; return _has_val; }

    const T&  value () const &  { if (!has_value()) exthrow(_err); return _val; }
          T&  value ()       &  { if (!has_value()) exthrow(_err); return _val; }
    const T&& value () const && { if (!has_value()) exthrow(_err); return std::move(_val); }
          T&& value ()       && { if (!has_value()) exthrow(_err); return std::move(_val); }

    template <class T2> constexpr T value_or (T2&& v) const & { _checked = true; return bool(*this) ? this->_val : static_cast<T>(std::forward<T2>(v)); }
    template <class T2> constexpr T value_or (T2&& v)      && { _checked = true; return bool(*this) ? std::move(this->_val) : static_cast<T>(std::forward<T2>(v)); }

    const E&  error () const &  { return _err; }
          E&  error ()       &  { return _err; }
    const E&& error () const && { return std::move(_err); }
          E&& error ()       && { return std::move(_err); }

    const T* operator-> () const { return &_val; }

src/panda/excepted.h  view on Meta::CPAN

    void emplace (Args&&... args) {
        if (_has_val) _val = T(std::forward<Args>(args)...);
        else {
            auto tmp = std::move(_err);
            _err.~E();
            try {
                ::new (&_val) T(std::forward<Args>(args)...);
                _has_val = true;
            } catch (...) {
                new (&_err) E(std::move(tmp));
                throw;
            }
        }
    }

    template <class F>
    auto and_then (F&& f) const & -> decltype(f(std::declval<T>())) {
        _checked = true;
        if (!_has_val) return unexpected_type(_err);
        return f(_val);
    }

src/panda/excepted.h  view on Meta::CPAN

    excepted (unexpected<E2>&& uex) {
        construct_err(std::move(uex.value()));
    }

    ~excepted () noexcept(false) {
        if      (_has_val) return;
        else if (_checked) _err.~E();
        else {
            auto tmp = std::move(_err);
            _err.~E();
            exthrow(std::move(tmp));
        }
    }

    excepted& operator= (const excepted& ex) {
        if (ex._has_val) set_val();
        else {
            set_err(ex._err);
            ex._checked = true;
        }
        return *this;

src/panda/expected.h  view on Meta::CPAN

    }

    template <class E2>
    expected& operator= (unexpected<E2>&& uex) {
        set_err(std::move(uex.value()));
    }

    constexpr bool     has_value     () const noexcept { return _has_val; }
    constexpr explicit operator bool () const noexcept { return _has_val; }

    const T&  value () const &  { if (!_has_val) throw bad_expected_access<E>(_err); return _val; }
          T&  value ()       &  { if (!_has_val) throw bad_expected_access<E>(_err); return _val; }
    const T&& value () const && { if (!_has_val) throw bad_expected_access<E>(_err); return std::move(_val); }
          T&& value ()       && { if (!_has_val) throw bad_expected_access<E>(_err); return std::move(_val); }

    template <class T2> constexpr T value_or (T2&& v) const & { return bool(*this) ? this->_val : static_cast<T>(std::forward<T2>(v)); }
    template <class T2> constexpr T value_or (T2&& v)      && { return bool(*this) ? std::move(this->_val) : static_cast<T>(std::forward<T2>(v)); }

    const E&  error () const &  { return _err; }
          E&  error ()       &  { return _err; }
    const E&& error () const && { return std::move(_err); }
          E&& error ()       && { return std::move(_err); }

    const T* operator-> () const { return &_val; }

src/panda/expected.h  view on Meta::CPAN

    void emplace (Args&&... args) {
        if (_has_val) _val = T(std::forward<Args>(args)...);
        else {
            auto tmp = std::move(_err);
            _err.~E();
            try {
                ::new (&_val) T(std::forward<Args>(args)...);
                _has_val = true;
            } catch (...) {
                new (&_err) E(std::move(tmp));
                throw;
            }
        }
    }

    template <class F>
    auto and_then (F&& f) const & -> decltype(f(std::declval<T>())) {
        if (!_has_val) return unexpected_type(_err);
        return f(_val);
    }

src/panda/hash.cc  view on Meta::CPAN

    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

char* crypt_xor (const char* source, size_t slen, const char* key, size_t klen, char* dest) {
    unsigned char* buf;
    if (dest) buf = (unsigned char*) dest;
    else {
        buf = (unsigned char*) malloc(slen+1); // space for '0'
        if (!buf) throw std::bad_alloc();
    }
    for (size_t i = 0; i < slen; ++i) buf[i] = ((unsigned char) source[i]) ^ ((unsigned char) key[i % klen]);
    buf[slen] = 0;
    return (char*) buf;
}

}}

src/panda/log.cc  view on Meta::CPAN

        ilogger->log(level, cp, s);
        return true;
    }
}

void set_level (Level val, const string& module) {
    if (module) {
        auto& modules = ::panda_log_module->children;
        auto iter = modules.find(module);
        if (iter == modules.end()) {
            throw std::invalid_argument("unknown module");
        }
        iter->second->set_level(val);
    } else {
        panda_log_module->set_level(val);
    }

}

void set_logger (ILogger* l) {
    details::ilogger.reset(l);

src/panda/log.cc  view on Meta::CPAN


Module::Module(const string& name, Module* parent, Level level)
    : level(level), name(name)
{
    if (!parent) return;

    this->parent = parent;

    if (parent->children.find(name) != parent->children.end()) {
        string msg = "panda::log::Module " + name + "is already registered";
        throw std::logic_error(msg.c_str());
    }
    parent->children[name] = this;
}

void Module::set_level(Level level) {
    this->level = level;
    for (auto& p : children) {
        p.second->set_level(level);
    }
}

src/panda/memory.h  view on Meta::CPAN

            if (!pool) pool = small_pools[(size-1)>>2] = new MemoryPool((((size-1)>>2) + 1)<<2);
        }
        else if (size <= 16384) {
            pool = medium_pools[(size-1)>>6];
            if (!pool) pool = medium_pools[(size-1)>>6] = new MemoryPool((((size-1)>>6) + 1)<<6);
        }
        else if (size <= 262144) {
            pool = big_pools[(size-1)>>10];
            if (!pool) pool = big_pools[(size-1)>>10] = new MemoryPool((((size-1)>>10) + 1)<<10);
        }
        else throw std::invalid_argument("ObjectAllocator: object size cannot exceed 256k");

        return pool->allocate();
    }

    void deallocate (void* ptr, size_t size) {
        if (ptr == NULL || size == 0) return;
        MemoryPool* pool;
        if      (size <= 1024)   pool = small_pools[(size-1)>>2];
        else if (size <= 16384)  pool = medium_pools[(size-1)>>6];
        else if (size <= 262144) pool = big_pools[(size-1)>>10];
        else throw std::invalid_argument("ObjectAllocator: object size cannot exceed 256k");
        pool->deallocate(ptr);
    }

    ~DynamicMemoryPool ();

private:
    static constexpr const int POOLS_CNT = 256;
    static DynamicMemoryPool* _global_instance;
    MemoryPool* small_pools[POOLS_CNT];
    MemoryPool* medium_pools[POOLS_CNT];

src/panda/string.h  view on Meta::CPAN


    namespace {
        template <typename T>
        inline T _stox (const string& str, std::size_t* pos = 0, int base = 10) {
            T val;
            auto res = from_chars(str.data(), str.data() + str.length(), val, base);
            if (pos) {
                *pos = res.ptr - str.data();
            }
            if (res.ec) {
                if (res.ec == std::errc::invalid_argument) throw std::invalid_argument("stoi");
                else if (res.ec == std::errc::result_out_of_range) throw std::out_of_range("stoi");
            }
            return val;
        }
    }

    inline int                stoi   (const string& str, std::size_t* pos = 0, int base = 10) { return _stox<int>(str, pos, base); }
    inline long               stol   (const string& str, std::size_t* pos = 0, int base = 10) { return _stox<long>(str, pos, base); }
    inline long long          stoll  (const string& str, std::size_t* pos = 0, int base = 10) { return _stox<long long>(str, pos, base); }
    inline unsigned long      stoul  (const string& str, std::size_t* pos = 0, int base = 10) { return _stox<unsigned long>(str, pos, base); }
    inline unsigned long long stoull (const string& str, std::size_t* pos = 0, int base = 10) { return _stox<unsigned long long>(str, pos, base); }

t/exception.cc  view on Meta::CPAN

    }
    return iptr<BacktraceInfo>();
}

// prevent inlining
extern "C" {

int v = 0;

void fnxx() { ++v; }
void fn00() { ++v; fnxx(); throw bt<std::invalid_argument>("Oops!"); }
void fn01() { fn00(); ++v; }
void fn02() { fn01(); ++v; }
void fn03() { fn02(); ++v; }
void fn04() { fn03(); ++v; }
void fn05() { fn04(); ++v; }
void fn06() { fn05(); ++v; }
void fn07() { fn06(); ++v; }
void fn08() { fn07(); ++v; }
void fn09() { fn08(); ++v; }
void fn10() { fn09(); ++v; }

t/exception.cc  view on Meta::CPAN

        CHECK(fn00_frame);
        CHECK(fn46_frame);
        was_catch = true;
    }
    REQUIRE(was_catch);
}

TEST_CASE("panda::exception with string", "[exception]") {
    bool was_catch = false;
    try {
        throw panda::exception("my-description");
    } catch( const exception& e) {
        REQUIRE(e.whats() == "my-description");
        was_catch = true;
    }
    REQUIRE(was_catch);
}

#endif

t/from_chars.cc  view on Meta::CPAN

    static string max  () { return "18446744073709551615"; }
    static string mmin () { return ""; }
    static string mmax () { return "18446744073709551616"; }
};

template<typename Int>
Int fci (string str, unsigned& pos, int base = 10) {
    Int val;
    auto res = panda::from_chars(str.data() + pos, str.data() + str.size() - pos, val, base);
    pos = res.ptr - str.data();
    if (res.ec) throw Exc();
    return val;
}

template <typename Int, bool is_signed = std::numeric_limits<Int>::is_signed>
struct test_sign_dependent;

template <typename Int>
struct test_sign_dependent<Int, true> {
    static void run () {
        unsigned pos = 0;

t/string_test.h  view on Meta::CPAN

            CHECK_ALLOCS();
        };


        SECTION("sso") {
            StdString cur;

            while (cur.size() <= MAX_SSO_CHARS) {
                String s(cur.data(), cur.size());
                REQUIRE_STR(s, cur, MAX_SSO_CHARS);
                if (cur.size() == defexp.size()) throw "should not happen";
                cur += defexp[cur.size()];
            }
            CHECK_ALLOCS();

            auto sz = BUF_CHARS + cur.size();
            {
                String s(cur.data(), cur.size());
                REQUIRE_STR(s, cur);
                CHECK_ALLOCS(1, sz);
            }

t/string_test.h  view on Meta::CPAN

            }
            CHECK_ALLOCS(0,0,1,EBUF_CHARS,0,0,1,exp.size());
        }
        SECTION("out of bounds") {
            auto exp = mstr("hello");
            FString src(exp.c_str());
            SECTION("too big length acts as npos") {
                String s(src, 3, 10);
                REQUIRE_STRM(s, mstr("lo"));
            }
            SECTION("too big offset throws exception") {
                REQUIRE_THROWS(String(src, 6, 10));
            }
        }
    }

    static void test_substr () {
        auto exp = mstr("hello world, hello world!");
        String src(exp.c_str());
        String s = src.substr(0, 5);
        REQUIRE_STR(s, mstr("hello"), 0, exp.size());

t/string_test.h  view on Meta::CPAN

            s = EMPTY;
            CHECK_ALLOCS(1, EBUF_CHARS, 1, EBUF_CHARS, 0, 0, 1, exp.size());
        }
        SECTION("out of bounds") {
            auto exp = mstr("hello");
            String s(exp.c_str());
            SECTION("too big length acts as npos") {
                s.offset(3, 10);
                REQUIRE_STRM(s, mstr("lo"));
            };
            SECTION("too big offset throws exception") {
                REQUIRE_THROWS(s.offset(6,10));
            }
        }
    }

    template <class FString>
    static void test_swap () {
        SECTION("literal<->literal") {
            String s1;
            FString s2(LITERAL);

t/test.h  view on Meta::CPAN

        return ret;
    }

    template <class T = char, int N = 0>
    struct Allocator {
        typedef T value_type;

        static T* allocate (size_t n) {
            //std::cout << "allocate " << n << std::endl;
            void* mem = malloc(n * sizeof(T));
            if (!mem) throw std::bad_alloc();
            allocs.allocated += n;
            allocs.allocated_cnt++;
            return (T*)mem;
        }

        static void deallocate (T* mem, size_t n) {
            //std::cout << "deallocate " << n << std::endl;
            allocs.deallocated += n;
            allocs.deallocated_cnt++;
            free(mem);

t/to_chars.cc  view on Meta::CPAN

    static string max () { return "18446744073709551615"; }
};

template<typename Int>
string tci (Int val, int base = 10, size_t buflen = 100) {
    string s;
    char* buf = s.reserve(buflen);
    char* bufend = buf + buflen;
    auto res = panda::to_chars(buf, bufend, val, base);
    s.length(res.ptr - buf);
    if (res.ec) throw Exc();
    return s;
}

template <typename Int, bool is_signed = std::numeric_limits<Int>::is_signed>
struct test_sign_dependent;

template <typename Int>
struct test_sign_dependent<Int, true> {
    static void run () {
        SECTION("negative number") {



( run in 0.623 second using v1.01-cache-2.11-cpan-496ff517765 )