Repository: gchq/CyberChef
File: src/core/lib/Stream.mjs (JavaScript)

Code before:
            const skiptable = new Array();
            val.forEach(function(element, index) {
                skiptable[element] = len - index;
            });
            return skiptable;
        }

        const length = val.length;

        const initial = val[length-1];

        this.position = length;

        // Get the skip table.
        const skiptable = preprocess(val, length);
        let found = true;

        while (this.position < this.length) {

            // Until we hit the final element of val in the stream.
            while ((this.position < this.length) && (this.bytes[this.position++] !== initial));

            found = true;

            // Loop through the elements comparing them to val.
            for (let x = length-1; x+1; x--) {
                if (this.bytes[(this.position-length) + x] !== val[x]) {
                    found = false;

                    // If element is not equal to val's element then jump forward by the correct amount.
                    this.position += skiptable[val[x]];
                    break;
                }
            }
            if (found) {
                this.position = (this.position - length);
                break;
            }
        }
    }


    /**
     * Consume bytes if it matches the supplied value.
     *
     * @param val
     */
    consumeWhile(val) {
        while ((this.position < this.length) && (this.bytes[this.position++] === val));
    }


Review comment (suggestion):
Interesting way of writing the condition here. I think to make it clearer, I'd rather we use `x > 0`. The JS interpreter will optimise it anyway, so we should aim to make the code as readable and obvious as possible.

Code after:
        function preprocess(val, len) {
            const skiptable = new Array();
            val.forEach((element, index) => {
                skiptable[element] = len - index;
            });
            return skiptable;
        }

        const length = val.length;

        const initial = val[length-1];

        this.position = length;

        // Get the skip table.
        const skiptable = preprocess(val, length);
        let found = true;

        while (this.position < this.length) {

            // Until we hit the final element of val in the stream.
            while ((this.position < this.length) && (this.bytes[this.position++] !== initial));

            found = true;

            // Loop through the elements comparing them to val.
            for (let x = length-1; x >= 0; x--) {
                if (this.bytes[this.position-length + x] !== val[x]) {
                    found = false;

                    // If element is not equal to val's element then jump forward by the correct amount.
                    this.position += skiptable[val[x]];
                    break;
                }
            }
            if (found) {
                this.position -= length;
                break;
            }
        }
    }


    /**
     * Consume bytes if it matches the supplied value.
     *
     * @param {Number} val
     */
    consumeWhile(val) {
        while (this.position < this.length) {
            if (this.bytes[this.position] !== val) {