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

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

lines 1–10why, one step back
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"

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

line 11why, one step back
11#define LOG_ENABLE_DBG 1

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 12why, one step back
12#include "log.h"

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

line 13why, one step back
13#include "debug.h"

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

lines 14–16why, one step back
14#include "xmalloc.h"
1516char32_t *

config: --nth-delimiter=CHRDaniel Eklöfbefore adoption2dd56d8

line 17why, one step back
17nth_column(const char32_t *string, char32_t delim, const char *nth_format)

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 18–20why, one step back
18{
19 const char32_t *string_end = string + c32len(string);20 xassert(*string_end == U'\0');

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 21why, one step back
21

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 22–25why, one step back
22 /* 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++;

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 26why, one step back
26

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 27–29why, one step back
27 /* Find the beginning, and length, of each column */
28 const char32_t *col_starts[col_count];29 size_t col_lens[col_count];

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 30why, one step back
30

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 31–32why, one step back
31 col_starts[0] = string;
32 for (size_t i = 1; i < col_count; i++) {

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

line 33why, one step back
33 const char32_t *next = c32chr(col_starts[i - 1], delim);

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 34why, one step back
34

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 35–37why, one step back
35 xassert(next != NULL);
36 col_starts[i] = next + 1;37 col_lens[i - 1] = next - col_starts[i - 1];

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

lines 38–39why, one step back
38 }
39

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 40–42why, one step back
40 col_lens[col_count - 1] = string_end - col_starts[col_count - 1];
4142 for (size_t i = 0; i < col_count; i++) {

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

lines 43–44why, one step back
43 LOG_DBG("col #%zu: \"%.*ls\" (%zu chars)", i,
44 (int)col_lens[i], (const wchar_t *)col_starts[i], col_lens[i]);

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 45–47why, one step back
45 }
4647 /* TODO: allocate dynamically */

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 48why, one step back
48 char32_t *result = NULL;

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 49–50why, one step back
49 size_t res_chars = 0;
50 size_t res_idx = 0;

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

line 51why, one step back
51

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 52–127why, one step back
52 /* 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 }127

column: detect invalid index range where end < startDaniel Eklöfbefore adoption099ea2a

lines 128–130why, one step back
128 if (idx_start == 0 || idx_start > col_count ||
129 idx_end > col_count || idx_end < idx_start)130 {

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 131–134why, one step back
131 /* Invalid index */
132 p++;133 continue;134 }

config: --nth-delimiter=CHRDaniel Eklöfbefore adoption2dd56d8

lines 135–136why, one step back
135
136 xassert(idx_end >= idx_start);

Rework --accept-nth and --with-nth to accept a format stringDaniel Eklöfbefore adoption4929e73

lines 137–181why, one step back
137
138 /* 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;

Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e

lines 182–185why, one step back
182 }
183184 return result;185}