1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-23 00:33:44 +00:00

Import pathname from dindexer.

This commit is contained in:
King_DuckZ 2017-04-11 18:10:19 +01:00
parent ab9b4ec8c3
commit 6a6d4c85bb
8 changed files with 803 additions and 0 deletions

5
config/tawashi.ini Normal file
View file

@ -0,0 +1,5 @@
[tawashi]
redis_server = 127.0.0.1
redis_port = 6379
redis_mode = inet
base_uri = http://127.0.0.1:8080

View file

@ -0,0 +1,30 @@
#ifndef assert_hh_INCLUDED
#define assert_hh_INCLUDED
namespace Kakoune
{
class StringView;
// return true if user asked to ignore the error
bool notify_fatal_error(StringView message);
void on_assert_failed(const char* message);
}
#define STRINGIFY(X) #X
#define TOSTRING(X) STRINGIFY(X)
#ifdef KAK_DEBUG
#define kak_assert(...) do { \
if (not (__VA_ARGS__)) \
on_assert_failed("assert failed \"" #__VA_ARGS__ \
"\" at " __FILE__ ":" TOSTRING(__LINE__)); \
} while (false)
#else
#define kak_assert(...) do {} while(false)
#endif
#endif // assert_hh_INCLUDED

View file

@ -0,0 +1,133 @@
#ifndef ref_ptr_hh_INCLUDED
#define ref_ptr_hh_INCLUDED
#include <utility>
#if !defined(NDEBUG) && !defined(KAK_DEBUG)
# define KAK_DEBUG
#endif
namespace Kakoune
{
struct WorstMatch { [[gnu::always_inline]] WorstMatch(...) {} };
[[gnu::always_inline]]
inline void ref_ptr_moved(WorstMatch, void*, void*) noexcept {}
template<typename T, typename TForOverload = T>
struct RefPtr
{
RefPtr() = default;
explicit RefPtr(T* ptr) : m_ptr(ptr) { acquire(); }
~RefPtr() { release(); }
RefPtr(const RefPtr& other) : m_ptr(other.m_ptr) { acquire(); }
RefPtr(RefPtr&& other)
noexcept(noexcept(std::declval<RefPtr>().moved(nullptr)))
: m_ptr(other.m_ptr) { other.m_ptr = nullptr; moved(&other); }
RefPtr& operator=(const RefPtr& other)
{
if (other.m_ptr != m_ptr)
{
release();
m_ptr = other.m_ptr;
acquire();
}
return *this;
}
RefPtr& operator=(RefPtr&& other)
{
release();
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
moved(&other);
return *this;
}
RefPtr& operator=(T* ptr)
{
if (ptr != m_ptr)
{
release();
m_ptr = ptr;
acquire();
}
return *this;
}
[[gnu::always_inline]]
T* operator->() const { return m_ptr; }
[[gnu::always_inline]]
T& operator*() const { return *m_ptr; }
[[gnu::always_inline]]
T* get() const { return m_ptr; }
[[gnu::always_inline]]
explicit operator bool() const { return m_ptr; }
void reset(T* ptr = nullptr)
{
if (ptr == m_ptr)
return;
release();
m_ptr = ptr;
acquire();
}
friend bool operator==(const RefPtr& lhs, const RefPtr& rhs) { return lhs.m_ptr == rhs.m_ptr; }
friend bool operator!=(const RefPtr& lhs, const RefPtr& rhs) { return lhs.m_ptr != rhs.m_ptr; }
private:
T* m_ptr = nullptr;
[[gnu::always_inline]]
void acquire()
{
if (m_ptr)
inc_ref_count(static_cast<TForOverload*>(m_ptr), this);
}
[[gnu::always_inline]]
void release()
{
if (m_ptr)
dec_ref_count(static_cast<TForOverload*>(m_ptr), this);
m_ptr = nullptr;
}
[[gnu::always_inline]]
void moved(void* from)
noexcept(noexcept(ref_ptr_moved(static_cast<TForOverload*>(nullptr), nullptr, nullptr)))
{
if (m_ptr)
ref_ptr_moved(static_cast<TForOverload*>(m_ptr), from, this);
}
};
struct RefCountable
{
int refcount = 0;
virtual ~RefCountable() = default;
friend void inc_ref_count(RefCountable* r, void*)
{
++r->refcount;
}
friend void dec_ref_count(RefCountable* r, void*)
{
if (--r->refcount == 0)
delete r;
}
};
}
#if defined(KAK_DEBUG)
# undef KAK_DEBUG
#endif
#endif // ref_ptr_hh_INCLUDED

View file

@ -0,0 +1,106 @@
#ifndef safe_ptr_hh_INCLUDED
#define safe_ptr_hh_INCLUDED
// #define SAFE_PTR_TRACK_CALLSTACKS
#include "ref_ptr.hh"
#include <type_traits>
#include <utility>
#ifdef SAFE_PTR_TRACK_CALLSTACKS
#include "vector.hh"
#include <algorithm>
#endif
#include <cassert>
#define kak_assert assert
#if !defined(NDEBUG) && !defined(KAK_DEBUG)
# define KAK_DEBUG
#endif
namespace Kakoune
{
// *** SafePtr: objects that assert nobody references them when they die ***
class SafeCountable
{
public:
#ifdef KAK_DEBUG
SafeCountable() : m_count(0) {}
~SafeCountable()
{
kak_assert(m_count == 0);
#ifdef SAFE_PTR_TRACK_CALLSTACKS
kak_assert(m_callstacks.empty());
#endif
}
friend void inc_ref_count(const SafeCountable* sc, void* /*ptr*/)
{
++sc->m_count;
#ifdef SAFE_PTR_TRACK_CALLSTACKS
sc->m_callstacks.emplace_back(ptr);
#endif
}
friend void dec_ref_count(const SafeCountable* sc, void* /*ptr*/)
{
--sc->m_count;
kak_assert(sc->m_count >= 0);
#ifdef SAFE_PTR_TRACK_CALLSTACKS
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
[=](const Callstack& cs) { return cs.ptr == ptr; });
kak_assert(it != sc->m_callstacks.end());
sc->m_callstacks.erase(it);
#endif
}
friend void ref_ptr_moved(const SafeCountable* /*sc*/, void* /*from*/, void* /*to*/)
{
#ifdef SAFE_PTR_TRACK_CALLSTACKS
auto it = std::find_if(sc->m_callstacks.begin(), sc->m_callstacks.end(),
[=](const Callstack& cs) { return cs.ptr == from; });
kak_assert(it != sc->m_callstacks.end());
it->ptr = to;
#endif
}
private:
#ifdef SAFE_PTR_TRACK_CALLSTACKS
struct Callstack
{
Callstack(void* p) : ptr(p) {}
void* ptr;
Backtrace bt;
};
mutable Vector<Callstack> m_callstacks;
#endif
mutable int m_count;
#else
[[gnu::always_inline]]
friend void inc_ref_count(const SafeCountable* /*sc*/, void* /*ptr*/) {}
[[gnu::always_inline]]
friend void dec_ref_count(const SafeCountable* /*sc*/, void* /*ptr*/) {}
#endif
};
template<typename T, typename U>
using PropagateConst = typename std::conditional<std::is_const<T>::value, const U, U>::type;
template<typename T>
using SafePtr = RefPtr<T, PropagateConst<T, SafeCountable>>;
}
#if defined(KAK_DEBUG)
# undef KAK_DEBUG
#endif
#undef kak_assert
#endif // safe_ptr_hh_INCLUDED

254
src/pathname/pathname.cpp Normal file
View file

@ -0,0 +1,254 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "pathname.hpp"
#include <algorithm>
#include <functional>
#include <ciso646>
#include <iostream>
#include <cassert>
namespace mchlib {
const std::string PathName::m_empty_str("");
namespace {
std::string get_joint_atoms ( const StringPool<char>& parPool, bool parAbs, std::size_t parSkipRight=0 );
std::size_t calc_join_size ( const StringPool<char>& parPool, bool parAbs, std::size_t parSkipRight=0 );
std::size_t get_adjusted_atom_count ( const StringPool<char>& parPool, std::size_t parSkipRight );
std::size_t get_adjusted_atom_count (const StringPool<char>& parPool, std::size_t parSkipRight) {
const auto orig_atom_count = parPool.size();
const auto atom_count = (parSkipRight >= orig_atom_count ? 0 : orig_atom_count - parSkipRight);
return atom_count;
}
std::size_t calc_join_size (const StringPool<char>& parPool, bool parAbs, std::size_t parSkipRight) {
const auto atom_count = get_adjusted_atom_count(parPool, parSkipRight);
if (not atom_count) {
if (parPool.empty() and parAbs) {
return 1;
}
else {
return 0;
}
}
std::size_t reserve = (parAbs ? 1 : 0);
for (std::size_t z = 0; z < atom_count; ++z) {
reserve += parPool[z].size();
}
reserve += atom_count - 1;
return reserve;
}
std::size_t count_grouped (boost::string_ref parIn, char parDelim) {
std::size_t retval = 0;
char prev = '\0';
for (auto c : parIn) {
retval += (parDelim == c and prev != parDelim ? 1 : 0);
prev = c;
}
return retval;
}
void split_path (std::vector<boost::string_ref>* parOut, boost::string_ref parPath) {
auto from = parPath.begin();
boost::string_ref::const_iterator next;
const auto end = parPath.end();
const auto beg = parPath.begin();
while (end != (next = std::find(from, end, '/'))) {
if (next != from) {
parOut->push_back(parPath.substr(from - beg, next - from));
from = next;
}
++from;
}
if (next != from) {
parOut->push_back(parPath.substr(from - beg, next - from));
}
}
std::string get_joint_atoms (const StringPool<char>& parPool, bool parAbs, std::size_t parSkipRight) {
const auto reserve = calc_join_size(parPool, parAbs, parSkipRight);
switch (reserve) {
case 0:
//reserve 0 means the resulting string is empty
return std::string("");
case 1:
//when reserve is 1 and we're talking about an absolute path,
//the resulting string can only be "/"
if (parAbs) {
return std::string("/");
}
};
std::string out;
out.reserve(reserve);
const char* slash = (parAbs ? "/" : "");
const auto atom_count = get_adjusted_atom_count(parPool, parSkipRight);
for (std::size_t z = 0; z < atom_count; ++z) {
out += slash;
const auto& curr_itm = parPool[z];
out.insert(out.end(), curr_itm.begin(), curr_itm.end());
slash = "/";
}
assert(reserve == out.size());
return out;
}
} //unnamed namespace
PathName::PathName (boost::string_ref parPath) {
if (not parPath.empty()) {
m_absolute = ('/' == parPath.front());
std::string path(parPath.begin(), parPath.end());
const auto count = count_grouped(path, '/');
const std::size_t trailing = (path.back() == '/' ? 1 : 0);
const std::size_t absolute = (m_absolute ? 1 : 0);
const auto res = count + 1 - trailing - absolute;
std::vector<boost::string_ref> atoms;
atoms.reserve(res);
split_path(&atoms, path);
m_pool.insert(atoms, &path);
}
else {
m_original_path = nullptr;
m_absolute = false;
}
}
std::string PathName::path() const {
return get_joint_atoms(m_pool, m_absolute);
}
void PathName::join (const PathName& parOther) {
m_pool.update(parOther.m_pool);
}
const boost::string_ref PathName::operator[] (std::size_t parIndex) const {
return *(m_pool.begin() + parIndex);
}
std::size_t PathName::atom_count ( void ) const {
return m_pool.size();
}
void PathName::join (const char* parOther) {
const std::string src(parOther);
const boost::string_ref ref(src);
m_pool.insert(ref, &src);
}
void PathName::join (boost::string_ref parOther, const std::string* parSource) {
m_pool.insert(parOther, parSource);
}
PathName make_relative_path (const PathName& parBasePath, const PathName& parOtherPath) {
if (not parBasePath.is_absolute() and parOtherPath.is_absolute()) {
return parOtherPath;
}
std::size_t common_atoms = 0;
{
const std::size_t shortest = std::min(parOtherPath.atom_count(), parBasePath.atom_count());
for (std::size_t z = 0; z < shortest; ++z) {
if (parOtherPath[z] == parBasePath[z]) {
++common_atoms;
}
else {
break;
}
}
}
PathName retval("");
const auto ellipses_count = parBasePath.atom_count() - common_atoms;
for (std::size_t z = 0; z < ellipses_count; ++z) {
retval.join("..");
}
const auto remaining_atoms = parOtherPath.atom_count() - common_atoms;
for (std::size_t z = 0; z < remaining_atoms; ++z) {
retval.join(parOtherPath[z + common_atoms], parOtherPath.get_stringref_source(z + common_atoms));
}
return retval;
}
const std::string* PathName::get_stringref_source (std::size_t parIndex) const {
return m_pool.get_stringref_source(parIndex);
}
std::string PathName::dirname() const {
if (this->atom_count() == 0)
return std::string();
return get_joint_atoms(m_pool, m_absolute, 1);
}
std::ostream& operator<< (std::ostream& parStream, const PathName& parPath) {
parStream << parPath.path();
return parStream;
}
const boost::string_ref basename (const PathName& parPath) {
static const char* const empty = "";
const auto sz = parPath.atom_count();
if (not sz) {
return boost::string_ref(empty);
}
assert(sz > 0);
return parPath[sz - 1];
}
PathName& PathName::pop_right() {
m_pool.pop();
return *this;
}
bool PathName::operator!= (const PathName& parOther) const {
const auto count = atom_count();
if (count != parOther.atom_count()) {
return true;
}
for (std::size_t z = 0; z < count; ++z) {
if ((*this)[z] != parOther[z]) {
return true;
}
}
return false;
}
bool PathName::operator== (const PathName& parOther) const {
const auto count = atom_count();
if (count != parOther.atom_count()) {
return false;
}
for (std::size_t z = 0; z < count; ++z) {
if ((*this)[z] != parOther[z]) {
return false;
}
}
return true;
}
std::size_t PathName::str_path_size() const {
return calc_join_size(m_pool, is_absolute());
}
} //namespace mchlib

65
src/pathname/pathname.hpp Normal file
View file

@ -0,0 +1,65 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id279E04E31E2C4D98B8C902781A3CE018
#define id279E04E31E2C4D98B8C902781A3CE018
#include "stringpool.hpp"
#include "kakoune/safe_ptr.hh"
#include <vector>
#include <string>
#include <boost/utility/string_ref.hpp>
#include <map>
#include <iostream>
namespace mchlib {
class PathName : public Kakoune::SafeCountable {
public:
PathName ( PathName&& ) = default;
PathName ( const PathName& ) = default;
explicit PathName ( boost::string_ref parPath );
~PathName ( void ) noexcept = default;
bool is_absolute ( void ) const { return m_absolute; }
std::string path ( void ) const;
std::size_t str_path_size ( void ) const;
const std::string& original_path ( void ) const { return (m_original_path ? *m_original_path : m_empty_str); }
std::size_t atom_count ( void ) const;
const boost::string_ref operator[] ( std::size_t parIndex ) const;
void join ( const PathName& parOther );
void join ( const char* parOther );
void join ( boost::string_ref parOther, const std::string* parSource );
const std::string* get_stringref_source ( std::size_t parIndex ) const;
std::string dirname ( void ) const;
PathName& pop_right ( void );
bool operator!= ( const PathName& parOther ) const;
bool operator== ( const PathName& parOther ) const;
private:
static const std::string m_empty_str;
StringPool<char> m_pool;
const std::string* m_original_path;
bool m_absolute;
};
PathName make_relative_path ( const PathName& parBasePath, const PathName& parOtherPath );
std::ostream& operator<< ( std::ostream& parStream, const PathName& parPath );
const boost::string_ref basename ( const PathName& parPath );
} //namespace mchlib
#endif

View file

@ -0,0 +1,70 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id9CF5E6FA7E334DF09559C2968C494CB9
#define id9CF5E6FA7E334DF09559C2968C494CB9
#include <string>
#include <boost/utility/string_ref.hpp>
#include <vector>
#include <utility>
#include <ciso646>
#include <cstdint>
#include <algorithm>
#include <functional>
#include <boost/iterator/transform_iterator.hpp>
namespace mchlib {
template <typename C, typename Str=std::basic_string<C>, typename StrRef=boost::basic_string_ref<C>>
class StringPool {
typedef std::pair<StrRef, const Str*> StringListPair;
typedef std::vector<std::pair<Str, std::size_t>> PoolType;
typedef std::vector<StringListPair> StringListType;
typedef std::function<StrRef(const StringListPair&)> FuncGetFirst;
public:
typedef C char_type;
typedef Str string_type;
typedef StrRef stringref_type;
typedef boost::transform_iterator<FuncGetFirst, typename StringListType::const_iterator> const_iterator;
StringPool ( void ) = default;
~StringPool ( void ) noexcept = default;
template <typename ItR>
void update ( ItR parDataBeg, ItR parDataEnd );
void update ( const StringPool& parOther );
void insert ( const std::vector<stringref_type>& parStrings, const string_type* parBaseString );
void insert ( stringref_type parString, const string_type* parBaseString );
const string_type* ptr_to_literal ( const char* parLiteral );
std::size_t size ( void ) const { return m_strings.size(); }
bool empty ( void ) const { return m_strings.empty(); }
const_iterator begin ( void ) const;
const_iterator end ( void ) const;
const string_type* get_stringref_source ( std::size_t parIndex ) const;
const stringref_type& operator[] ( std::size_t parIndex ) const;
void pop ( void );
private:
PoolType m_pool;
StringListType m_strings;
};
} //namespace mchlib
#include "stringpool.inl"
#endif

140
src/pathname/stringpool.inl Normal file
View file

@ -0,0 +1,140 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
namespace mchlib {
namespace implem {
template <typename StrRef>
std::pair<StrRef, bool> clone_ifp (const StrRef& parClone, StrRef parSource) {
const auto offset = parSource.find(parClone);
if (parSource.npos != offset) {
return std::make_pair(parSource.substr(offset, parClone.size()), true);
}
else {
return std::make_pair(parClone, false);
}
}
} //namespace implem
template <typename C, typename Str, typename StrRef>
auto StringPool<C, Str, StrRef>::ptr_to_literal (const char* parLiteral) -> const string_type* {
if (not parLiteral)
return nullptr;
for (const auto& p : m_pool) {
if (m_pool.first == parLiteral) {
return &m_pool.first;
}
}
return nullptr;
}
template <typename C, typename Str, typename StrRef>
template <typename ItR>
void StringPool<C, Str, StrRef>::update (ItR parDataBeg, ItR parDataEnd) {
typedef std::pair<string_type, std::size_t> PoolPair;
while (parDataBeg != parDataEnd) {
const auto& remote_str = parDataBeg->first;
const auto* remote_source_str = parDataBeg->second;
bool cloned = false;
for (auto& local_src : m_pool) {
const string_type& local_str = local_src.first;
auto& local_ref_count = local_src.second;
auto cloned_result = implem::clone_ifp<StrRef>(remote_str, local_str);
cloned = cloned_result.second;
const auto& cloned_str = cloned_result.first;
if (cloned) {
++local_ref_count;
m_strings.push_back(StringListPair(cloned_str, &local_str));
break;
}
}
if (not cloned) {
m_pool.push_back(PoolPair(*remote_source_str, static_cast<std::size_t>(1)));
const auto offset = remote_str.data() - remote_source_str->data();
m_strings.push_back(StringListPair(stringref_type(m_pool.back().first).substr(offset, remote_str.size()), &m_pool.back().first));
}
++parDataBeg;
}
}
template <typename C, typename Str, typename StrRef>
void StringPool<C, Str, StrRef>::update (const StringPool& parOther) {
this->update(parOther.m_strings.begin(), parOther.m_strings.end());
}
template <typename C, typename Str, typename StrRef>
auto StringPool<C, Str, StrRef>::begin() const -> const_iterator {
return const_iterator(m_strings.cbegin(), [](const StringListPair& parItm) { return parItm.first; });
}
template <typename C, typename Str, typename StrRef>
auto StringPool<C, Str, StrRef>::end() const -> const_iterator {
return const_iterator(m_strings.cend(), [](const StringListPair& parItm) { return parItm.first; });
}
template <typename C, typename Str, typename StrRef>
void StringPool<C, Str, StrRef>::insert (const std::vector<stringref_type>& parStrings, const string_type* parBaseString) {
StringListType dummy;
dummy.reserve(parStrings.size());
for (const auto& itm : parStrings) {
dummy.push_back(StringListPair(itm, parBaseString));
}
this->update(dummy.begin(), dummy.end());
}
template <typename C, typename Str, typename StrRef>
void StringPool<C, Str, StrRef>::insert (stringref_type parString, const string_type* parBaseString) {
StringListType dummy;
dummy.reserve(1);
dummy.push_back(StringListPair(parString, parBaseString));
this->update(dummy.begin(), dummy.end());
}
template <typename C, typename Str, typename StrRef>
auto StringPool<C, Str, StrRef>::get_stringref_source (std::size_t parIndex) const -> const string_type* {
return m_strings[parIndex].second;
}
template <typename C, typename Str, typename StrRef>
auto StringPool<C, Str, StrRef>::operator[] (std::size_t parIndex) const -> const stringref_type& {
return m_strings[parIndex].first;
}
template <typename C, typename Str, typename StrRef>
void StringPool<C, Str, StrRef>::pop() {
if (m_strings.empty()) {
return;
}
for (auto z = m_pool.size(); z > 0; --z) {
auto& pool_itm = m_pool[z - 1];
if (&pool_itm.first == m_strings.back().second) {
m_strings.resize(m_strings.size() - 1);
--pool_itm.second;
if (0 == pool_itm.second) {
m_pool.erase(m_pool.begin() + (z - 1));
}
break;
}
}
return;
}
} //namespace mchlib