Skip to content

Commit

Permalink
Merge pull request #73 from janstarke/feature/release/2.1.5
Browse files Browse the repository at this point in the history
Feature/release/2.1.5
  • Loading branch information
janstarke authored Dec 2, 2021
2 parents a10d034 + cf79238 commit dbac1c2
Show file tree
Hide file tree
Showing 28 changed files with 189 additions and 74 deletions.
31 changes: 31 additions & 0 deletions src/librexgen/common/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
rexgen - a tool to create words based on regular expressions
Copyright (C) 2012-2017 Jan Starke <jan.starke@outofbed.org>
This program 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 2 of the License, or (at your option)
any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/

#ifndef REXGEN_MEMORY_H
#define REXGEN_MEMORY_H

#include <memory>

template <typename T>
bool is_uninitialized(std::weak_ptr<T> const& weak) {
using wt = std::weak_ptr<T>;
return !weak.owner_before(wt{}) && !wt{}.owner_before(weak);
}

#endif //REXGEN_MEMORY_H
2 changes: 1 addition & 1 deletion src/librexgen/iterator/caseiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <librexgen/genericerror.h>
#include <cassert>
namespace rexgen {
CaseIterator::CaseIterator(std::unique_ptr<Iterator>& __child, int options)
CaseIterator::CaseIterator(std::shared_ptr<Iterator>& __child, int options)
: IteratorContainer(), handle_case(options) {
child = std::move(__child);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librexgen/iterator/caseiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace rexgen {
class CaseIterator : public IteratorContainer {
public:
CaseIterator(std::unique_ptr<Iterator>& _child, int options);
CaseIterator(std::shared_ptr<Iterator>& _child, int options);

bool next();

Expand Down
10 changes: 5 additions & 5 deletions src/librexgen/iterator/compoundregexiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ namespace rexgen {
if (state == resetted) {
state = usable;
bool res = false;
for (std::unique_ptr<Iterator>& i : iterators) {
for (auto& i : iterators) {
res |= i->next();
}
return res;
}
for (std::unique_ptr<Iterator>& i : iterators) {
for (auto& i : iterators) {
if (i->next()) {
return true;
}
Expand All @@ -42,14 +42,14 @@ namespace rexgen {

void CompoundRegexIterator::value(SimpleString *dst) const {
// assert(canUseValue());
for (const std::unique_ptr<Iterator>& i : iterators) {
for (const auto& i : iterators) {
i->value(dst);
}
}

std::shared_ptr<SerializableState> CompoundRegexIterator::getCurrentState() const {
auto s = Iterator::getCurrentState();
for (const std::unique_ptr<Iterator>& i : iterators) {
for (const auto& i : iterators) {
s->addValue(i->getCurrentState());
}
return s;
Expand All @@ -58,7 +58,7 @@ namespace rexgen {
void CompoundRegexIterator::setCurrentState(const std::shared_ptr<SerializableState>& s) {
Iterator::setCurrentState(s);

for (const std::unique_ptr<Iterator>& i : iterators) {
for (const auto& i : iterators) {
i->setCurrentState(s->getChildState(i->getId()));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librexgen/iterator/groupreferenceiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace rexgen {
class GroupReferenceIterator : public IteratorContainer {
public:
GroupReferenceIterator(int group)
: IteratorContainer(), groupId(group), groupRef(std::ref(null_iter)) {
: IteratorContainer(), groupId(group) {
}

inline bool hasNext() const override { return (state == resetted); }
Expand All @@ -42,15 +42,15 @@ namespace rexgen {
}

inline void value(SimpleString *dst) const override {
groupRef.get().value(dst);
groupRef.lock()->value(dst);
}

void updateReferences(IteratorState& iterState) override;

private:
NullIterator null_iter;
int groupId;
std::reference_wrapper<Iterator> groupRef;
std::weak_ptr<Iterator> groupRef;
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/librexgen/iterator/iteratorcontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ namespace rexgen {

class IteratorContainer : public Iterator {
public:
typedef std::vector<std::unique_ptr<Iterator>> children_list_type;
typedef std::vector<std::shared_ptr<Iterator>> children_list_type;

virtual void updateAttributes(IteratorState& iterState) {
for (std::unique_ptr<Iterator>& child : iterators) {
for (auto& child : iterators) {
child->updateAttributes(iterState);
}
}

virtual void updateReferences(IteratorState& iterState) {
for (std::unique_ptr<Iterator>& child : iterators) {
for (auto& child : iterators) {
child->updateReferences(iterState);
}
}

virtual void addChild(std::unique_ptr<Iterator>&& i) {
virtual void addChild(std::shared_ptr<Iterator>&& i) {
iterators.push_back(std::move(i));
}

Expand Down
2 changes: 1 addition & 1 deletion src/librexgen/iterator/iteratorpermuter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace rexgen {
void IteratorPermuter::init() {
ENTER_METHOD;

for (std::unique_ptr<Iterator>& i : iterators) {
for (std::shared_ptr<Iterator>& i : iterators) {
i->next();
}

Expand Down
34 changes: 19 additions & 15 deletions src/librexgen/iterator/iteratorstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,29 @@
#include <librexgen/iterator/iterator.h>
#include <librexgen/iterator/streamregexiterator.h>
#include <librexgen/regex/regex.h>
#include <librexgen/common/memory.h>
#include <map>
#include <algorithm>
#include <functional>
#include <stdexcept>
namespace rexgen {
class IteratorState {
public:
explicit IteratorState() : streamIterator(nullptr) {}
explicit IteratorState() = default;

/* prevent copying of IteratorState */
IteratorState(const IteratorState&) = delete;
IteratorState(IteratorState&&) = delete;
IteratorState& operator=(const IteratorState&) = delete;
IteratorState& operator=(IteratorState&&) = delete;

void registerIterator(int id, std::reference_wrapper<Iterator> iterator) {
void registerIterator(int id, std::weak_ptr<Iterator> iterator) {
groupIterators.insert(std::make_pair(id, iterator));
}

bool hasId(int id) const {
return (groupIterators.find(id) != groupIterators.end());
}

std::reference_wrapper<Iterator> operator[](int id) const {
std::weak_ptr<Iterator> operator[](int id) const {
if (id == -1) {
return std::ref(static_cast<Iterator&>(getStreamIterator().get()));
return getStreamIterator();
} else {
auto iter = groupIterators.find(id);
if (iter != groupIterators.end()) {
Expand All @@ -59,22 +56,29 @@ namespace rexgen {
}
}

void setStreamIterator(std::unique_ptr<StreamRegexIterator>& iter) {
if (streamIterator != nullptr) {
void setStreamIterator(std::weak_ptr<StreamRegexIterator>& iter) {
if (hasStreamIterator()) {
throw std::runtime_error("multiple stream iterator assignment");
}
streamIterator = std::move(iter);
}

bool hasStreamIterator() const { return streamIterator != nullptr; }
bool hasStreamIterator() const {
if (!is_uninitialized(streamIterator)) {
if (!streamIterator.expired()) {
return true;
}
}
return false;
}

std::reference_wrapper<StreamRegexIterator> getStreamIterator() const {
return std::ref(*streamIterator);
std::weak_ptr<StreamRegexIterator> getStreamIterator() const {
return streamIterator;
}

private:
std::map<int, std::reference_wrapper<Iterator> > groupIterators;
mutable std::unique_ptr<StreamRegexIterator> streamIterator;
std::map<int, std::weak_ptr<Iterator> > groupIterators;
mutable std::weak_ptr<StreamRegexIterator> streamIterator;
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/librexgen/iterator/regexalternativesiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace rexgen {
if (state == resetted) {
state = usable;
bool res = false;
for (std::unique_ptr<Iterator>& i : iterators) {
for (auto& i : iterators) {
res |= (i->next());
}
RETURN(res);
Expand All @@ -56,7 +56,7 @@ namespace rexgen {
RETURN(true);
}

void RegexAlternativesIterator::addChild(std::unique_ptr<Iterator>&& i) {
void RegexAlternativesIterator::addChild(std::shared_ptr<Iterator>&& i) {
ENTER_METHOD;
IteratorContainer::addChild(std::move(i));
resetPosition();
Expand All @@ -78,7 +78,7 @@ namespace rexgen {
auto s = Iterator::getCurrentState();
s->addValue(getPosition() - iterators.begin());

for (const std::unique_ptr<Iterator>& i : iterators) {
for (const auto& i : iterators) {
s->addValue(i->getCurrentState());
}
return s;
Expand All @@ -87,7 +87,7 @@ namespace rexgen {
void RegexAlternativesIterator::setCurrentState(const std::shared_ptr<SerializableState>& s) {
Iterator::setCurrentState(s);

for (std::unique_ptr<Iterator>& i : iterators) {
for (auto& i : iterators) {
i->setCurrentState(s->getChildState(i->getId()));
}

Expand Down
2 changes: 1 addition & 1 deletion src/librexgen/iterator/regexalternativesiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace rexgen {
(*getPosition())->value(dst);
}

void addChild(std::unique_ptr<Iterator>&& re);
void addChild(std::shared_ptr<Iterator>&& re);

std::shared_ptr<SerializableState> getCurrentState() const;

Expand Down
5 changes: 3 additions & 2 deletions src/librexgen/iterator/topiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace rexgen {
explicit TopIterator(std::shared_ptr<Regex>& re) : Iterator() {
needWord = false;
child = re->iterator(state);
assert(child != nullptr);

// register regex alternatives
updateReferences();
Expand All @@ -44,7 +45,7 @@ namespace rexgen {

bool next() override {
if (needWord) {
bool res = state.getStreamIterator().get().forceNext();
bool res = state.getStreamIterator().lock()->forceNext();
if (res) {
needWord = false;
}
Expand All @@ -54,7 +55,7 @@ namespace rexgen {
if (res) { return res; }

if (! state.hasStreamIterator()) { return false; }
res = state.getStreamIterator().get().forceNext();
res = state.getStreamIterator().lock()->forceNext();
if (res) { return res; }
needWord = true;
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/librexgen/regex/classregex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace rexgen {
}


std::unique_ptr<Iterator> ClassRegex::iterator(IteratorState& state) const {
std::shared_ptr<Iterator> ClassRegex::iterator(IteratorState& state) const {
if (getMinOccurs() == 1 && getMaxOccurs() == 1) {
return singleIterator(state);
}
Expand All @@ -129,8 +129,8 @@ namespace rexgen {
return std::make_unique<IteratorPermuter>(*this, state, getMinOccurs(), getMaxOccurs() );
}

std::unique_ptr<Iterator> ClassRegex::singleIterator(IteratorState& /* state */) const {
std::unique_ptr<Iterator> single;
std::shared_ptr<Iterator> ClassRegex::singleIterator(IteratorState& /* state */) const {
std::shared_ptr<Iterator> single;

if (ranges.size() == 0) {
return std::make_unique<ClassRegexIterator>(characters.begin(), characters.end());
Expand Down
4 changes: 2 additions & 2 deletions src/librexgen/regex/classregex.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ namespace rexgen {

void merge(const std::shared_ptr<ClassRegex>& other);

std::unique_ptr<Iterator> iterator(IteratorState& /* state*/ ) const override;
std::shared_ptr<Iterator> iterator(IteratorState& /* state*/ ) const override;

std::unique_ptr<Iterator> singleIterator(IteratorState& /* state */) const override ;
std::shared_ptr<Iterator> singleIterator(IteratorState& /* state */) const override ;

private:
void removeCharacterInstances(const wchar_t min, const wchar_t max);
Expand Down
8 changes: 4 additions & 4 deletions src/librexgen/regex/compoundregex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ namespace rexgen {
LEAVE_METHOD;
}

std::unique_ptr<Iterator> CompoundRegex::singleIterator(IteratorState& state) const {
std::shared_ptr<Iterator> CompoundRegex::singleIterator(IteratorState& state) const {
if (regexObjects.size() == 1) {
return regexObjects[0]->iterator(state);
}

auto cri = std::make_unique<CompoundRegexIterator>();
auto cri = std::make_shared<CompoundRegexIterator>();
std::for_each(regexObjects.begin(), regexObjects.end(),
[&cri,&state](const std::shared_ptr<Regex>& r) {cri->addChild(r->iterator(state));});
return cri;
}

std::unique_ptr<Iterator> CompoundRegex::iterator(IteratorState& state) const {
std::shared_ptr<Iterator> CompoundRegex::iterator(IteratorState& state) const {
if (regexObjects.size() == 1) {
const std::shared_ptr<Regex>& re = regexObjects[0];
if (getMinOccurs() == 1 && getMaxOccurs() == 1) {
return re->iterator(state);
}

return std::make_unique<IteratorPermuter>(*re, state, getMinOccurs(), getMaxOccurs());
return std::make_shared<IteratorPermuter>(*re, state, getMinOccurs(), getMaxOccurs());
}

assert(regexObjects.size() > 1);
Expand Down
4 changes: 2 additions & 2 deletions src/librexgen/regex/compoundregex.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ namespace rexgen {

RegexType getRegexType() const { return Compound; }

std::unique_ptr<Iterator> iterator(IteratorState& state) const;
std::shared_ptr<Iterator> iterator(IteratorState& state) const;

std::unique_ptr<Iterator> singleIterator(IteratorState& state) const;
std::shared_ptr<Iterator> singleIterator(IteratorState& state) const;
};
}
#endif // SRC_LIBREXGEN_REGEX_COMPOUNDREGEX_H_
4 changes: 2 additions & 2 deletions src/librexgen/regex/groupreference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include <librexgen/iterator/groupreferenceiterator.h>
#include <librexgen/string/simplestring.h>
namespace rexgen {
std::unique_ptr<Iterator> GroupReference::singleIterator(IteratorState& /* state */) const {
std::shared_ptr<Iterator> GroupReference::singleIterator(IteratorState& /* state */) const {
auto ref = groupRef.lock();
assert(ref != nullptr);
return std::make_unique<GroupReferenceIterator>(ref->getGroupId());
return std::make_shared<GroupReferenceIterator>(ref->getGroupId());
}
}
2 changes: 1 addition & 1 deletion src/librexgen/regex/groupreference.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace rexgen {
public:
explicit GroupReference(int _groupId) : groupId(_groupId){}

std::unique_ptr<Iterator> singleIterator(IteratorState& state) const;
std::shared_ptr<Iterator> singleIterator(IteratorState& state) const;

RegexType getRegexType() const { return Reference; }

Expand Down
Loading

0 comments on commit dbac1c2

Please sign in to comment.