我在库 API 中定义了一个实用程序结构,它有四个字段,都是数字计数器。
typedef struct {
size_t bytes;
int codepoints;
int graphemes;
int columns;
} TickitStringPos;
我想提供一些实用程序来轻松处理这些结构。我可以将这些实现为(静态内联)函数,例如
static inline void tickit_stringpos_zero(TickitStringPos *pos) {
pos->bytes = pos->codepoints = pos->graphemes = pos->columns = 0;
}
static inline void tickit_stringpos_limit_columns(TickitStringPos *pos, int columns) {
pos->bytes = pos->codepoints = pos->graphemes = -1;
pos->columns = columns;
}
TickitStringPos here, limit;
tickit_stringpos_zero(&here);
tickit_stringpos_limit_columns(&limit, 20);
或者我可以将这些实现为宏,例如
#define TICKIT_STRINGPOS_ZERO(pos) do { \
(pos).bytes = (pos).codepoints = (pos).graphemes = (pos).columns = 0; \
} while(0);
#define TICKIT_STRINGPOS_LIMIT_COLUMNS(pos,_columns) do { \
(pos).bytes = (pos).codepoints = (pos).graphemes = -1; \
(pos).columns = _columns; \
} while(0);
TickitStringPos here, limit;
TICKIT_STRINGPOS_ZERO(here);
TICKIT_STRINGPOS_LIMIT_COLUMNS(limit, 20);
我应该考虑如何权衡这两种方法?每种方法都可能同样强大和灵活,但出于某种原因,一种方法是否特别可取?