parse-u32

xod/stream/parse-u32

Read an unsigned integer number from a stream of characters and returns it as 4 bytes. Stop when a non-numeric character was encountered
parse-u32
@/parse-u32
Read an unsigned integer number from a stream of characters and returns it as 4 bytes. Stop when a non-numeric character was encountered
CHARbyte
The next character to be processed
PUSHpulse
Push a new character to process
RSTpulse
Reset the parsed unsigned integer to 0 and start over
parse-u32
B3
B2
B0
B1
END
CHAR
PUSH
RST
ENDpulse
Pulses when the non-numerical character was encountered
B1byte
B0byte
Least significant byte
B2byte
B3byte
Most significant byte

C++ implementation

node {
    uint32_t n = 0;
    bool isDone = false;

    void evaluate(Context ctx) {
        if (isInputDirty<input_RST>(ctx)) {
            isDone = false;
            n = 0;
        }

        if (!isInputDirty<input_PUSH>(ctx) || isDone)
            return;

        auto c = getValue<input_CHAR>(ctx);
        if (c >= '0' && c <= '9') {
            n *= 10;
            n += c - '0';
        } else {
            isDone = true;
            emitValue<output_END>(ctx, 1);
        }

        uint8_t b3 = (n >> 24);
        uint8_t b2 = (n >> 16);
        uint8_t b1 = (n >> 8);
        uint8_t b0 = n;
        emitValue<output_B0>(ctx, b0);
        emitValue<output_B1>(ctx, b1);
        emitValue<output_B2>(ctx, b2);
        emitValue<output_B3>(ctx, b3);
    }
}