pulse-on-sequence

xod/stream/pulse-on-sequence

Pulses each time when a specified sequence of characters is encountered
pulse-on-sequence
@/pulse-on-sequence
Pulses each time when a specified sequence of characters is encountered
IN1byte
The next character in the stream
IN2pulse
Push the next character
SEQstring
The searched sequence
pulse-on-sequence
IN1
IN2
OUT
SEQ
OUTpulse
Pulses each time when sequence is encountered

C++ implementation

node {
    int pos = 0;
    int _length;

    char charAt(XString str, int pos) {
        auto it = str.iterate();
        for (int i = 0; i < pos; i++) {
            ++it;
        }

        return *it;
    }

    void evaluate(Context ctx) {
        auto str = getValue<input_SEQ>(ctx);

        if (isInputDirty<input_SEQ>(ctx)) {
            _length = length(str);
            pos = 0;
        }

        if (!isInputDirty<input_IN2>(ctx))
            return;

        auto c = getValue<input_IN1>(ctx);
        if (c == charAt(str, pos)) {
            pos++;

            // it was the last char in a string
            if (pos == _length) {
                emitValue<output_OUT>(ctx, 1);
                // we will start over on next pulse
                pos = 0;
            }
        } else {
            // we will start over on next pulse
            pos = 0;
        }
    }
}