Phytolizer

joined 1 year ago
[–] Phytolizer 1 points 1 year ago

Hm, the renderer really messed with this (HTML escaped the less than, removed includes as if they were HTML tags?) :/

[–] Phytolizer 1 points 1 year ago* (last edited 1 year ago) (1 children)

ISO C99:

#include 
#include 
#include 
#include 

static char opener(char ch) {
    switch (ch) {
        case '}': return '{';
        case ']': return '[';
        case ')': return '(';
        default: return 0;
    }
}

enum { MAX_LINE_LENGTH = 2048 };
static char buffer[MAX_LINE_LENGTH];

int main(void) {
    char* line = fgets(buffer, sizeof(buffer), stdin);
    if (line == NULL) {
        fprintf(stderr, "Error: could not read input.\n");
        return EXIT_FAILURE;
    }
    size_t len = strlen(line);
    if (len > 0 and line[len - 1] == '\n') {
        line[--len] = 0;
    }

    char stack[128];
    size_t stack_size = 0;
    for (size_t i = 0; i < len; ++i) {
        char pair = opener(line[i]);
        if (pair and stack_size != 0 and stack[stack_size - 1] == pair) {
            --stack_size;
        } else {
            stack[stack_size++] = line[i];
        }
    }
    stack[stack_size] = 0;
    puts(stack);
}

Mirrored code on bpa.st

Compile: cc -std=c99 -pedantic-errors -O2 bracket.c -o bracket

(Edited to add -O2 because you mentioned it would be timed.)

[–] Phytolizer 2 points 1 year ago

I installed a ton of them and use shifty to randomly pick one whenever I want. Currently it's Gruvbox.