1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-19 12:04:54 +00:00

Use string_ref instead of string and implement join().

This commit is contained in:
King_DuckZ 2015-11-05 17:22:34 +01:00
parent 31a70945bb
commit 109ce9b82a
4 changed files with 73 additions and 15 deletions

View file

@ -4,8 +4,14 @@ project(dindexer VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11")
find_library (Boost REQUIRED 1.53.0)
add_executable(${PROJECT_NAME}
src/main.cpp
src/filesearcher.cpp
src/pathname.cpp
)
target_include_directories(${PROJECT_NAME} SYSTEM
INTERFACE ${Boost_INCLUDE_DIR}
)

View file

@ -50,5 +50,8 @@ int main (int parArgc, char* parArgv[]) {
std::cout << pn2.path() << '\n';
std::cout << pn3.path() << '\n';
std::cout << pn4.path() << '\n';
pn1.join(pn2);
std::cout << pn1.path() << '\n';
return 0;
}

View file

@ -17,34 +17,45 @@
#include "pathname.hpp"
#include <algorithm>
#include <cstring>
#include <functional>
#include <ciso646>
#include <iostream>
namespace din {
namespace {
void split_path (std::vector<std::string>* parOut, const char* parPath, std::size_t parLen) {
bool ptr_between (const char* parPtr, const char* parBeg, const char* parEnd) {
std::less<const char*> less;
std::less_equal<const char*> lesseq;
return lesseq(parBeg, parPtr) and less(parPtr, parEnd);
}
void split_path (std::vector<boost::string_ref>* parOut, boost::string_ref parPath) {
parOut->clear();
const char* from = parPath;
const char* next;
while (parPath + parLen != (next = std::find(from, parPath + parLen, '/'))) {
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(std::string(from, next));
parOut->push_back(parPath.substr(from - beg, next - from));
from = next;
}
++from;
}
if (next != from) {
parOut->push_back(std::string(from, next));
parOut->push_back(parPath.substr(from - beg, next - from));
}
}
} //unnamed namespace
PathName::PathName (const char* parPath) {
if (not parPath) {
m_absolute = false;
}
else {
PathName::PathName (const char* parPath) :
m_original_path(parPath)
{
if (not m_original_path.empty()) {
m_absolute = ('/' == *parPath);
split_path(&m_atoms, parPath, std::strlen(parPath));
split_path(&m_atoms, m_original_path);
}
}
@ -69,9 +80,38 @@ namespace din {
const char* slash = (m_absolute ? "/" : "");
for (const auto& itm : m_atoms) {
out += slash;
out += itm;
out.insert(out.end(), itm.begin(), itm.end());
slash = "/";
}
return std::move(out);
}
void PathName::join (const PathName& parOther) {
typedef std::pair<std::string, std::size_t> PairType;
using boost::string_ref;
for (const auto& itm : parOther.m_pool) {
m_pool[itm.first] += itm.second;
}
const auto& other_path = parOther.original_path();
const auto it_other_path = m_pool.insert(PairType(other_path, 0)).first;
for (auto str : parOther.m_atoms) {
if (ptr_between(str.data(), other_path.data(), other_path.data() + other_path.size())) {
it_other_path->second++;
auto offset = str.data() - other_path.data();
m_atoms.push_back(string_ref(it_other_path->first).substr(offset, str.size()));
}
else {
}
}
if (not it_other_path->second) {
m_pool.erase(it_other_path);
}
std::cout << " --------------- content -----------------\n";
for (const auto& itm : m_pool) {
std::cout << itm.first << " - " << itm.second << '\n';
}
}
} //namespace din

View file

@ -20,6 +20,8 @@
#include <vector>
#include <string>
#include <boost/utility/string_ref.hpp>
#include <map>
namespace din {
class PathName {
@ -29,9 +31,16 @@ namespace din {
bool is_absolute ( void ) const;
std::string path ( void ) const;
const std::string& original_path ( void ) const { return m_original_path; }
std::size_t atom_count ( void ) const { return m_atoms.size(); }
const boost::string_ref operator[] ( std::size_t parIndex ) const { return m_atoms[parIndex]; }
void join ( const PathName& parOther );
private:
std::vector<std::string> m_atoms;
typedef std::vector<boost::string_ref> AtomList;
std::map<std::string, std::size_t> m_pool;
AtomList m_atoms;
std::string m_original_path;
bool m_absolute;
};
} //namespace din