/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running cc -Wall -s -O3 -march=native -mtune=native -flto -o ./squeeze ./squeeze.c */ #include #include #include #include #include #ifdef _WIN32 #include #include #endif #ifdef RED_ERRORS #define ERROR_STYLE "\x1b[38;2;204;0;0m" #ifdef __APPLE__ #define ERROR_STYLE "\x1b[31m" #endif #define RESET_STYLE "\x1b[0m" #else #define ERROR_STYLE #define RESET_STYLE #endif #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n") const char* info = "" "squeeze [filenames...]\n" "\n" "Ignore leading/trailing spaces (and carriage-returns) on lines, also turning\n" "all runs of multiple consecutive spaces into single spaces. Spaces around\n" "tabs are ignored as well.\n" ""; // bufwriter is, as the name implies, a buffered-writer: when it's aimed at // stdout, it considerably speeds up this app, as intended typedef struct bufwriter { // buf is the buffer proper unsigned char* buf; // len is how many bytes of the buffer are currently being used size_t len; // cap is the capacity of the buffer, or the most bytes it can hold size_t cap; // out is the destination of all that's written into the buffer FILE* out; } bufwriter; void init_bufwriter(bufwriter* w, FILE* out, unsigned char* b, size_t cap) { w->buf = b; w->len = 0; w->cap = cap; w->out = out; } void write_byte(bufwriter* w, unsigned char b) { if (w->len < w->cap) { w->buf[w->len++] = b; return; } fwrite(w->buf, w->cap, 1, w->out); w->buf[0] = b; w->len = 1; } void flush(bufwriter* w) { if (w->len > 0) { fwrite(w->buf, w->len, 1, w->out); } w->len = 0; fflush(w->out); } // slice is a growable region of bytes in memory typedef struct slice { // ptr is the starting place of the region unsigned char* ptr; // len is how many bytes are currently being used size_t len; // cap is how many bytes the memory region has available size_t cap; } slice; bool starts_with_bom(const unsigned char* b, const size_t n) { return (n >= 3 && b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf); } void write_squeezed_line(bufwriter* w, const unsigned char* p, size_t len) { // ignore trailing CRLF byte-pairs, or trailing line-feed bytes if (len >= 2 && p[len - 2] == '\r' && p[len - 1] == '\n') { len -= 2; } else if (len >= 1 && p[len - 1] == '\n') { len--; } // ignore leading spaces while (len > 0 && p[0] == ' ') { p++; len--; } // ignore trailing spaces // while (len > 0 && p[len - 1] == ' ') { // len--; // } // trailing spaces won't be emitted, since there's nothing to follow // them, which in turn prevents their normal single-space replacement // from being emitted bool space = false; for (size_t i = 0; i < len; i++) { unsigned char b = p[i]; if (b == ' ') { space = true; continue; } if (b == '\t') { space = false; write_byte(w, '\t'); // ignore spaces right after tabs while (i + 1 < len && p[i + 1] == ' ') { i++; } continue; } if (space) { write_byte(w, ' '); space = false; } write_byte(w, b); } write_byte(w, '\n'); } // handle_reader skips leading UTF-8 BOMs (byte-order marks), and turns all // CR-LF pairs into single LF bytes bool handle_reader(bufwriter* w, FILE* r, slice* line, bool live_lines) { for (size_t i = 0; !feof(w->out); i++) { ssize_t len = getline((char**)&line->ptr, &line->cap, r); if (len < 0) { break; } if (line->ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return false; } // get rid of leading UTF-8 BOM (byte-order mark) if 1st line has it if (i == 0 && starts_with_bom(line->ptr, len)) { write_squeezed_line(w, line->ptr + 3, len - 3); continue; } write_squeezed_line(w, line->ptr, len); if (live_lines) { flush(w); } } if (!live_lines) { flush(w); } return true; } // handle_file handles data from the filename given; returns false only when // the file can't be opened bool handle_file(bufwriter* w, const char* path, slice* line, bool live_lines) { FILE* f = fopen(path, "rb"); if (f == NULL) { fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path); return false; } const bool ok = handle_reader(w, f, line, live_lines); fclose(f); return ok; } // run returns the number of errors int run(int argc, char** argv, FILE* w, bool live_lines) { unsigned char outbuf[8 * 1024]; bufwriter bw; size_t dashes = 0; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { dashes++; } } if (dashes > 1) { const char* m = "can't use the standard input (dash) more than once"; fprintf(stderr, ERROR_LINE("%s"), m); return 1; } slice line; line.len = 0; line.cap = 32 * 1024; line.ptr = malloc(line.cap); if (line.ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return 1; } init_bufwriter(&bw, w, outbuf, sizeof(outbuf)); size_t errors = 0; for (int i = 1; i < argc && !feof(w) && line.ptr != NULL; i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { if (!handle_reader(&bw, stdin, &line, live_lines)) { errors++; } continue; } if (!handle_file(&bw, argv[i], &line, live_lines)) { errors++; } } // use stdin when not given any filepaths if (argc <= 1) { if (!handle_reader(&bw, stdin, &line, live_lines)) { errors++; } } free(line.ptr); flush(&bw); return errors; } int main(int argc, char** argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); // ensure output lines end in LF instead of CRLF on windows setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #endif if (argc > 1) { if ( strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--h") == 0 || strcmp(argv[1], "--help") == 0 ) { fprintf(stdout, "%s", info); return 0; } } const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0; if (!live_lines) { setvbuf(stdout, NULL, _IOFBF, 0); } return run(argc, argv, stdout, live_lines) == 0 ? 0 : 1; }