accumulate-string

xod/stream/accumulate-string

Creates a string from a stream of characters by appending each new character to the end of the string until the maximum capacity is reached.
accumulate-string
@/accumulate-string
Creates a string from a stream of characters by appending each new character to the end of the string until the maximum capacity is reached.
CAPnumber
The capacity of the string buffer. Defines the maximum length. Must be a constant value. Any changes during program execution will be ignored.
CHARbyte
A new character to be pushed to the end of the string
PUSHpulse
Push new character
RSTpulse
Empty the accumulated string and start over
accumulate-string
CAP
CHAR
PUSH
RST
STR
UPD
FULL
FULLpulse
Pulses when string reached maximum length and pushed character was ignored
UPDpulse
Pulses when resulting string is updated
STRstring
String that is accumulated so far

C++ implementation

node {
    char* buff;
    char* cursor;
    size_t cap;
    CStringView view;

    void evaluate(Context ctx) {
        if (isSettingUp()) {
            // save initial cap to ignore possiple input changes during program execution
            cap = getValue<input_CAP>(ctx);
            buff = new char[cap + 1]; // +1 to make room for terminal '\0'
            view = CStringView(buff);
        }

        if (isSettingUp() || isInputDirty<input_RST>(ctx)) {
            memset(buff, '\0', cap + 1);
            cursor = buff;
        }

        if (isInputDirty<input_PUSH>(ctx)) {
            if (cursor >= &buff[cap]) {
                emitValue<output_FULL>(ctx, 1);
                return;
            }

            *cursor = getValue<input_CHAR>(ctx);
            cursor++;
            emitValue<output_STR>(ctx, XString(&view));
            emitValue<output_UPD>(ctx, 1);
        }
    }
}