Skip to content
Myr

column.c

This file

Path

column.c

185 lines · 5.3 KiB · C

Last changed by

column: fix off-by-one when splitting input into columnsDaniel Eklöfbefore adoption9c22de1

History of this file

Take it

1#include "column.h"23#include <assert.h>4#include <limits.h>5#include <stdlib.h>6#include <string.h>7#include <uchar.h>8#include <unistd.h>910#define LOG_MODULE "column"11#define LOG_ENABLE_DBG 112#include "log.h"13#include "debug.h"14#include "xmalloc.h"1516char32_t *17nth_column(const char32_t *string, char32_t delim, const char *nth_format)18{19    const char32_t *string_end = string + c32len(string);20    xassert(*string_end == U'\0');2122    /* Count number of columns */23    size_t col_count = 1;24    for (const char32_t *next = c32chr(string, delim); next != NULL; next = c32chr(next + 1, delim))25        col_count++;2627    /* Find the beginning, and length, of each column */28    const char32_t *col_starts[col_count];29    size_t col_lens[col_count];3031    col_starts[0] = string;32    for (size_t i = 1; i < col_count; i++) {33        const char32_t *next = c32chr(col_starts[i - 1], delim);3435        xassert(next != NULL);36        col_starts[i] = next + 1;37        col_lens[i - 1] = next - col_starts[i - 1];38    }3940    col_lens[col_count - 1] = string_end - col_starts[col_count - 1];4142    for (size_t i = 0; i < col_count; i++) {43        LOG_DBG("col #%zu: \"%.*ls\" (%zu chars)", i,44                (int)col_lens[i], (const wchar_t *)col_starts[i], col_lens[i]);45    }4647    /* TODO: allocate dynamically */48    char32_t *result = NULL;49    size_t res_chars = 0;50    size_t res_idx = 0;5152    /* Expand the 'nth' template, replacing '{1}' with column[0] etc */53    const char *copy_start = nth_format;54    const char *p = nth_format;55    while (true) {56        const char *arg_start = strchr(p, U'{');57        if (arg_start == NULL) {58            const size_t copy_len = mbstoc32(NULL, copy_start, 0);59            if (copy_len == (size_t)-1) {60                result[res_idx] = U'\0';61                break;62            }6364            const size_t req_res_len = res_idx + copy_len;6566            if (req_res_len > res_chars) {67                res_chars = req_res_len + 1;68                result = xrealloc(result, res_chars * sizeof(result[0]));69            }7071            mbstoc32(&result[res_idx], copy_start, res_chars - res_idx);72            res_idx += copy_len;7374            xassert(res_idx <= res_chars);7576            result[res_idx] = U'\0';77            break;78        }7980        const char *arg_end = strchr(arg_start, U'}');81        if (arg_end == NULL) {82            /* Unclosed '{' */83            p++;84            continue;85        }8687        unsigned int idx_start = 0;88        unsigned int idx_end = 0;89        int dots = 0;9091        for (const char *d = arg_start + 1; d != arg_end; d++) {92            if (*d == '.') {93                if (idx_start == 0 || dots >= 2) {94                    idx_start = 0;95                    break;96                }9798                dots++;99                xassert(dots >= 1 && dots <= 3);100            } else if (*d >= '0' && *d <= '9') {101                if (dots == 0) {102                    idx_start *= 10;103                    idx_start += *d - '0';104                } else if (dots == 2) {105                    idx_end *= 10;106                    idx_end += *d - '0';107                } else {108                    idx_start = 0;109                    break;110                }111            } else {112                /* Invalid index */113                idx_start = 0;114                break;115            }116        }117118        if (idx_end == 0) {119            if (dots == 0)120                idx_end = idx_start;121            else if (dots == 2) {122                /* Open ended - append all remaining columns */123                idx_end = col_count;124            } else125                idx_start = 0;126        }127128        if (idx_start == 0 || idx_start > col_count ||129            idx_end > col_count || idx_end < idx_start)130        {131            /* Invalid index */132            p++;133            continue;134        }135136        xassert(idx_end >= idx_start);137138        /* Non-template prefix length */139        const size_t copy_len = arg_start - copy_start;140        const size_t copy_len_wide_chars = mbsntoc32(NULL, copy_start, copy_len, 0);141142        if (copy_len_wide_chars == (size_t)-1) {143            result[res_idx] = U'\0';144            break;145        }146147        /* Column(s) length */148        size_t total_col_len = 0;149        for (unsigned int i = idx_start; i <= idx_end; i++)150            total_col_len += col_lens[i - 1];151152        if (idx_end > idx_start) {153            /* Space-separated columns */154            total_col_len += idx_end - idx_start;155        }156157        const size_t req_res_len =158            res_idx + copy_len_wide_chars + total_col_len + 1;159160        if (req_res_len > res_chars) {161            res_chars = req_res_len * 2;162            result = xrealloc(result, res_chars * sizeof(result[0]));163        }164165        mbsntoc32(&result[res_idx], copy_start, copy_len, res_chars - res_idx);166        res_idx += copy_len_wide_chars;167        xassert(res_idx <= res_chars);168169        for (unsigned int i = idx_start; i <= idx_end; i++) {170            c32memcpy(&result[res_idx], col_starts[i - 1], col_lens[i - 1]);171            res_idx += col_lens[i - 1];172            xassert(res_idx <= res_chars);173174            if (i != idx_end) {175                result[res_idx++] = U' ';176                xassert(res_idx <= res_chars);177            }178        }179180        result[res_idx] = U'\0';181        copy_start = p = arg_end + 1;182    }183184    return result;185}