Merge pull request #169 from x1nixmzeng/master

Adjustments to wildcard engine
This commit is contained in:
Ben Vanik 2015-02-17 14:52:42 -08:00
commit 29bc8d8620
4 changed files with 25 additions and 24 deletions

View File

@ -109,49 +109,49 @@ WildcardFlags WildcardFlags::LAST(false, true);
WildcardFlags::WildcardFlags()
: FromStart(false)
, ToEnd(false)
, WithCase(false)
{ }
WildcardFlags::WildcardFlags(bool start, bool end)
: FromStart(start)
, ToEnd(end)
, WithCase(false)
{ }
WildcardRule::WildcardRule(std::string str_match, const WildcardFlags &flags)
WildcardRule::WildcardRule(const std::string& str_match, const WildcardFlags& flags)
: match(str_match)
, rules(flags)
{ }
bool WildcardRule::Check(std::string& str) const
{
if( match.empty() ) {
std::transform(match.begin(), match.end(), match.begin(), tolower);
}
bool WildcardRule::Check(const std::string& str_lower, std::string::size_type& offset) const
{
if (match.empty()) {
return true;
}
if( str.size() < match.size() ) {
if ((str_lower.size() - offset) < match.size()) {
return false;
}
std::string::size_type result(str.find(match));
std::string::size_type result(str_lower.find(match, offset));
if( result != std::string::npos ) {
if( rules.FromStart && result != 0 ) {
if (result != std::string::npos) {
if (rules.FromStart && result != offset) {
return false;
}
if( rules.ToEnd && result != ( ( str.size() -1 ) - match.size() ) ) {
if (rules.ToEnd && result != (str_lower.size() - match.size())) {
return false;
}
str.erase(0, result + match.size());
offset = (result + match.size());
return true;
}
return false;
}
void WildcardEngine::PreparePattern(const std::string &pattern)
void WildcardEngine::PreparePattern(const std::string& pattern)
{
rules.clear();
@ -172,17 +172,19 @@ void WildcardEngine::PreparePattern(const std::string &pattern)
}
}
void WildcardEngine::SetRule(const std::string &pattern)
void WildcardEngine::SetRule(const std::string& pattern)
{
PreparePattern(pattern);
}
bool WildcardEngine::Match(const std::string &str) const
bool WildcardEngine::Match(const std::string& str) const
{
std::string str_copy(str);
std::string str_lc;
std::transform(str.begin(), str.end(), std::back_inserter(str_lc), tolower);
std::string::size_type offset(0);
for (const auto& rule : rules) {
if (!(rule.Check(str_copy))) {
if (!(rule.Check(str_lc, offset))) {
return false;
}
}

View File

@ -45,8 +45,7 @@ class WildcardFlags
public:
bool
FromStart: 1,
ToEnd : 1,
WithCase : 1; // Unused for now
ToEnd : 1;
WildcardFlags();
WildcardFlags(bool start, bool end);
@ -58,8 +57,8 @@ public:
class WildcardRule
{
public:
WildcardRule(std::string str_match, const WildcardFlags &flags);
bool Check(std::string& str) const;
WildcardRule(const std::string& str_match, const WildcardFlags &flags);
bool Check(const std::string& str_lower, std::string::size_type& offset) const;
private:
std::string match;
@ -70,6 +69,8 @@ class WildcardEngine
{
public:
void SetRule(const std::string &pattern);
// Always ignoring case
bool Match(const std::string &str) const;
private:
std::vector<WildcardRule> rules;

View File

@ -166,7 +166,6 @@ bool GDFX::ReadEntry(ParseState& state, const uint8_t* buffer,
GDFXEntry* entry = new GDFXEntry();
entry->name = std::string(name, name_length);
entry->name.append(1, '\0');
entry->attributes = (X_FILE_ATTRIBUTES)attributes;
// Add to parent.

View File

@ -213,7 +213,6 @@ STFS::Error STFS::ReadAllEntries(const uint8_t* map_ptr) {
auto entry = std::make_unique<STFSEntry>();
entry->name = std::string((char*)filename, filename_length_flags & 0x3F);
entry->name.append(1, '\0');
// bit 0x40 = consecutive blocks (not fragmented?)
if (filename_length_flags & 0x80) {
entry->attributes = X_FILE_ATTRIBUTE_DIRECTORY;