Browse Source

implemented case insensitive match option and consolidated matcher computation code

master
Pete McNeil 2 years ago
parent
commit
f38dbaf48e
1 changed files with 44 additions and 38 deletions
  1. 44
    38
      roller.hpp

+ 44
- 38
roller.hpp View File

@@ -10,9 +10,13 @@
#pragma once

#include <cstdint>
#include <ctype.h>
#include <string>
#include <vector>

#include <iostream>
#include <bitset>

namespace codedweller {

class Roller32 {
@@ -41,34 +45,50 @@ namespace codedweller {
}
};

namespace roller{
static const unsigned char caseBit = 'A' ^ 'a';
static const unsigned char caseMask = ~caseBit;

unsigned char applyCaseSensitivity(unsigned char byte, bool caseSensitive) {
if (true==caseSensitive) return byte;
if (false==isalpha(byte) && 0xFF != byte) return byte;
unsigned char insensitiveByte = byte & caseMask;
return insensitiveByte;
}

unsigned char appropriateBitMask(unsigned char byte, bool caseSensitive) {
if (true==caseSensitive) return 0xff;
if (false==isalpha(byte)) return 0xff;
return caseMask;
}

template <class D, class T>
void compute(D data, bool caseSensitive, T& matcher, T& masker) {
size_t ingest = std::min(sizeof(matcher.value()), data.size());
for(size_t count = 0; count < ingest; count++) {
unsigned char theByte = data.at(count);
matcher.add(roller::applyCaseSensitivity(theByte, caseSensitive));
masker.add(roller::appropriateBitMask(theByte, caseSensitive));
}
}
}
class RollerMatch32 {
private:
uint_fast32_t match;
uint_fast32_t mask;

public:
RollerMatch32(const std::vector<unsigned char> pattern) {
match = mask = 0;
size_t ingest = std::min(sizeof(uint_fast32_t), pattern.size());
Roller32 matcher;
Roller32 masker;
for(size_t count = 0; count < ingest; count++) {
matcher.add(pattern.at(count));
masker.add(0xFF);
}
RollerMatch32(const std::vector<unsigned char> pattern, bool caseSensitive=true) {
Roller32 matcher, masker;
roller::compute(pattern, caseSensitive, matcher, masker);
match = matcher.value();
mask = masker.value();
}

RollerMatch32(const std::string pattern) {
match = mask = 0;
size_t ingest = std::min(sizeof(uint_fast32_t), pattern.size());
Roller32 matcher;
Roller32 masker;
for(size_t count = 0; count < ingest; count++) {
matcher.add(pattern.at(count));
masker.add(0xFF);
}
RollerMatch32(const std::string pattern, bool caseSensitive=true) {
Roller32 matcher, masker;
roller::compute(pattern, caseSensitive, matcher, masker);
match = matcher.value();
mask = masker.value();
}
@@ -85,28 +105,16 @@ namespace codedweller {
uint_fast64_t mask;

public:
RollerMatch64(const std::vector<unsigned char> pattern) {
match = mask = 0;
size_t ingest = std::min(sizeof(uint_fast64_t), pattern.size());
Roller64 matcher;
Roller64 masker;
for(size_t count = 0; count < ingest; count++) {
matcher.add(pattern.at(count));
masker.add(0xFF);
}
RollerMatch64(const std::vector<unsigned char> pattern, bool caseSensitive=true) {
Roller64 matcher, masker;
roller::compute(pattern, caseSensitive, matcher, masker);
match = matcher.value();
mask = masker.value();
}

RollerMatch64(const std::string pattern) {
match = mask = 0;
size_t ingest = std::min(sizeof(uint_fast64_t), pattern.size());
Roller64 matcher;
Roller64 masker;
for(size_t count = 0; count < ingest; count++) {
matcher.add(pattern.at(count));
masker.add(0xFF);
}
RollerMatch64(const std::string pattern, bool caseSensitive=true) {
Roller64 matcher, masker;
roller::compute(pattern, caseSensitive, matcher, masker);
match = matcher.value();
mask = masker.value();
}
@@ -115,8 +123,6 @@ namespace codedweller {
return (match == (roller.value() & mask));
}
};


}



Loading…
Cancel
Save