<commit_before>#include "broker.hpp"
#include "message.hpp"
#include <algorithm>


// struct MqttBroker {
// public:

//     void subscribe(MqttSubscriber subscriber, in string[] topics) {
//         subscribe(subscriber, array(map!(a => MqttSubscribe.Topic(a, 0))(topics)));
//     }

//     void subscribe(MqttSubscriber subscriber, in MqttSubscribe.Topic[] topics) {
//         foreach(t; topics) {
//             const parts = array(splitter(t.topic, "/"));
//             _subscriptions.addSubscription(Subscription(subscriber, t, parts), parts);
//         }
//     }

//     void unsubscribe(MqttSubscriber subscriber) {
//         _subscriptions.removeSubscription(subscriber, _subscriptions._nodes);
//     }

//     void unsubscribe(MqttSubscriber subscriber, in string[] topics) {
//         _subscriptions.removeSubscription(subscriber, topics, _subscriptions._nodes);
//     }

//     void publish(in string topic, in string payload) {
//         publish(topic, cast(ubyte[])payload);
//     }

//     void publish(in string topic, in ubyte[] payload) {
//         auto topParts = array(splitter(topic, "/"));
//         publish(topic, topParts, payload);
//     }

//     @property void useCache(bool u) {
//         _subscriptions._useCache = u;
//     }

// private:

//     SubscriptionTree _subscriptions;

//     void publish(in string topic, string[] topParts, in ubyte[] payload) {
//         _subscriptions.publish(topic, topParts, payload);
//     }
// };

// private bool revStrEquals(in string str1, in string str2) pure nothrow { //compare strings in reverse
//     if(str1.length != str2.length) return false;
//     for(auto i = cast(int)str1.length - 1; i >= 0; --i)
//         if(str1[i] != str2[i]) return false;
//     return true;
// }

// private bool equalOrPlus(in string pat, in string top) pure nothrow {
//     return pat == "+" || pat.revStrEquals(top);
// }

namespace {

// private struct SubscriptionTree {
//     private static struct Node {
//         string part;
//         Node* parent;
//         Node*[string] branches;
//         Subscription[] leaves;
//     }

//     void addSubscription(Subscription s, in string[] parts) {
//         assert(parts.length);
//         if(_useCache) _cache = _cache.init; //invalidate cache
//         addSubscriptionImpl(s, parts, null, _nodes);
//     }

//     void addSubscriptionImpl(Subscription s, const(string)[] parts,
//                              Node* parent, ref Node*[string] nodes) {
//         auto part = parts[0];
//         parts = parts[1 .. $];
//         auto node = addOrFindNode(s, part, parent, nodes);
//         if(parts.empty) {
//             node.leaves ~= s;
//         } else {
//             addSubscriptionImpl(s, parts, node, node.branches);
//         }
//     }<commit_msg>Uncommented revStrEquals and equalOrPlus in broker.cpp<commit_after>#include "broker.hpp"
#include "message.hpp"
#include <algorithm>


// struct MqttBroker {
// public:

//     void subscribe(MqttSubscriber subscriber, in string[] topics) {
//         subscribe(subscriber, array(map!(a => MqttSubscribe.Topic(a, 0))(topics)));
//     }

//     void subscribe(MqttSubscriber subscriber, in MqttSubscribe.Topic[] topics) {
//         foreach(t; topics) {
//             const parts = array(splitter(t.topic, "/"));
//             _subscriptions.addSubscription(Subscription(subscriber, t, parts), parts);
//         }
//     }

//     void unsubscribe(MqttSubscriber subscriber) {
//         _subscriptions.removeSubscription(subscriber, _subscriptions._nodes);
//     }

//     void unsubscribe(MqttSubscriber subscriber, in string[] topics) {
//         _subscriptions.removeSubscription(subscriber, topics, _subscriptions._nodes);
//     }

//     void publish(in string topic, in string payload) {
//         publish(topic, cast(ubyte[])payload);
//     }

//     void publish(in string topic, in ubyte[] payload) {
//         auto topParts = array(splitter(topic, "/"));
//         publish(topic, topParts, payload);
//     }

//     @property void useCache(bool u) {
//         _subscriptions._useCache = u;
//     }

// private:

//     SubscriptionTree _subscriptions;

//     void publish(in string topic, string[] topParts, in ubyte[] payload) {
//         _subscriptions.publish(topic, topParts, payload);
//     }
// };


namespace {

bool revStrEquals(const std::string& str1, const std::string& str2) { //compare strings in reverse
    if(str1.length() != str2.length()) return false;
    for(int i = str1.length() - 1; i >= 0; --i)
        if(str1[i] != str2[i]) return false;
    return true;
}

bool equalOrPlus(const std::string& pat, const std::string& top) {
    return pat == "+" || revStrEquals(pat, top);
}


// private struct SubscriptionTree {
//     private static struct Node {
//         string part;
//         Node* parent;
//         Node*[string] branches;
//         Subscription[] leaves;
//     }

//     void addSubscription(Subscription s, in string[] parts) {
//         assert(parts.length);
//         if(_useCache) _cache = _cache.init; //invalidate cache
//         addSubscriptionImpl(s, parts, null, _nodes);
//     }

//     void addSubscriptionImpl(Subscription s, const(string)[] parts,
//                              Node* parent, ref Node*[string] nodes) {
//         auto part = parts[0];
//         parts = parts[1 .. $];
//         auto node = addOrFindNode(s, part, parent, nodes);
//         if(parts.empty) {
//             node.leaves ~= s;
//         } else {
//             addSubscriptionImpl(s, parts, node, node.branches);
//         }
//     }
{'fraction_non_alphanumeric': 0.15396966993755576, 'fraction_numerical': 0.004281891168599465, 'mean_word_length': 2.2517401392111367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5345572', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1695, 'n_words': 392}