summary refs log tree commit diff
path: root/format.c
blob: 02406e382de913d314bb9fe8d5fbba606796406e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
2021-12-11Remove donate linkJune McEnroe
2021-12-11Remove Liberapay buttonsJune McEnroe
2021-12-07Update git author nameJune McEnroe
2021-12-03Show weeks plus daysJune McEnroe
2021-12-02Add Six Months, Three Days, Five OthersJune McEnroe
2021-11-28Use noinput, nounput options in lex filesJune McEnroe
2021-11-22Add SorrowlandJune McEnroe
2021-11-13Add My Pal Foot FootJune McEnroe
2021-11-03Update my little description of myselfJune McEnroe
2021-11-03Add A Psalm for the Wild-BuiltJune McEnroe
2021-10-30Publish "Names"June McEnroe
2021-10-29Add PiranesiJune McEnroe
2021-10-25Bounds check color indexesJune McEnroe
2021-10-24Only match first Nm for feedJune McEnroe
2021-10-22Add BerserkJune McEnroe
2021-10-20Only output style attribute if necessaryJune McEnroe
2021-10-20Run shotty after ptee in up -tJune McEnroe
2021-10-20Fix showing cursor with default colorsJune McEnroe
2021-10-20Add entire 256-color palette to shotty -iJune McEnroe
2021-10-20Ignore XTWINOPS in shottyJune McEnroe
2021-10-20Add A Wizard of EarthseaJune McEnroe
2021-10-18Revert "Add shotty -t to animate (crudely) between snapshots"June McEnroe
2021-10-18Add shotty -t to animate (crudely) between snapshotsJune McEnroe
2021-10-18Add ptee -t to output media copy at an intervalJune McEnroe
2021-10-18Swap functions of ^Q and ^S in pteeJune McEnroe
2021-10-18Add shotty -i to output inline colorsJune McEnroe
2021-10-18Remove leftover scheme.h targetJune McEnroe
2021-10-18Cite ctlseqs.ms and UTF-8June McEnroe
2021-10-18Skip DECKP* in lexerJune McEnroe
2021-10-18Add meta charset to up -t outputJune McEnroe
2021-10-18Rewrite shotty with lexJune McEnroe
2021-10-17Remove capsicum support from upJune McEnroe
2021-10-15Fix nested Bl-compact CSS rulesJune McEnroe
2021-10-14Add meta viewport tags to bin htmlJune McEnroe
2021-10-13Set bin WEBROOT to /var/wwwJune McEnroe
2021-10-11Add Knee Play 1June McEnroe
2021-10-04Fix lobsters/tildenews loginJune McEnroe
2021-10-03Limit Atom feed to 20 most recent entriesJune McEnroe
2021-10-03Add EarthlingsJune McEnroe
2021-10-02Just zero mailsize on changemailJune McEnroe
2021-10-02Fix chkmail loop break conditionJune McEnroe
2021-10-02Check sizes of mailboxes, not timesJune McEnroe
2021-10-02Replace filter shell scripts with C programJune McEnroe
2021-10-01Add htagml -m to rename main tagJune McEnroe
2021-10-01Remove igpJune McEnroe
ss="Id">len]; len = 0; if (format->str[0] == L',') { len = MIN(wcsspn(&format->str[1], L"0123456789"), 2); } if (!len) return; format->bg = 0; for (size_t i = 0; i < len; ++i) { format->bg *= 10; format->bg += format->str[1 + i] - L'0'; } if (format->bg > IRCLightGray) format->bg = IRCDefault; format->str = &format->str[1 + len]; } static const wchar_t Codes[] = { IRCBold, IRCColor, IRCReverse, IRCReset, IRCItalic, IRCUnderline, L'\0', }; bool formatParse(struct Format *format, const wchar_t *split) { format->str += format->len; if (!format->str[0]) { if (split == format->str && !format->split) { format->len = 0; format->split = true; return true; } return false; } const wchar_t *init = format->str; for (bool done = false; !done;) { switch (format->str[0]) { break; case IRCBold: format->str++; format->bold ^= true; break; case IRCItalic: format->str++; format->italic ^= true; break; case IRCUnderline: format->str++; format->underline ^= true; break; case IRCReverse: format->str++; format->reverse ^= true; break; case IRCColor: format->str++; parseColor(format); break; case IRCReset: format->str++; formatReset(format); break; default: done = true; } } format->split = (split >= init && split <= format->str); format->len = wcscspn(format->str, Codes); if (split > format->str && split < &format->str[format->len]) { format->len = split - format->str; } return true; } #ifdef TEST #include <assert.h> static bool testColor( const wchar_t *str, enum IRCColor fg, enum IRCColor bg, size_t index ) { struct Format format = { .str = str }; formatReset(&format); if (!formatParse(&format, NULL)) return false; if (format.fg != fg) return false; if (format.bg != bg) return false; return (format.str == &str[index]); } static bool testSplit(const wchar_t *str, size_t index) { struct Format format = { .str = str }; formatReset(&format); bool split = false; while (formatParse(&format, &str[index])) { if (format.split && split) return false; if (format.split) split = true; } return split; } static bool testSplits(const wchar_t *str) { for (size_t i = 0; i <= wcslen(str); ++i) { if (!testSplit(str, i)) return false; } return true; } int main() { assert(testColor(L"\003a", IRCDefault, IRCDefault, 1)); assert(testColor(L"\003,a", IRCDefault, IRCDefault, 1)); assert(testColor(L"\003,1", IRCDefault, IRCDefault, 1)); assert(testColor(L"\0031a", IRCBlack, IRCDefault, 2)); assert(testColor(L"\0031,a", IRCBlack, IRCDefault, 2)); assert(testColor(L"\00312a", IRCLightBlue, IRCDefault, 3)); assert(testColor(L"\00312,a", IRCLightBlue, IRCDefault, 3)); assert(testColor(L"\003123", IRCLightBlue, IRCDefault, 3)); assert(testColor(L"\0031,1a", IRCBlack, IRCBlack, 4)); assert(testColor(L"\0031,12a", IRCBlack, IRCLightBlue, 5)); assert(testColor(L"\0031,123", IRCBlack, IRCLightBlue, 5)); assert(testColor(L"\00312,1a", IRCLightBlue, IRCBlack, 5)); assert(testColor(L"\00312,12a", IRCLightBlue, IRCLightBlue, 6)); assert(testColor(L"\00312,123", IRCLightBlue, IRCLightBlue, 6)); assert(testColor(L"\00316,16a", IRCDefault, IRCDefault, 6)); assert(testColor(L"\00399,99a", IRCDefault, IRCDefault, 6)); assert(testSplits(L"")); assert(testSplits(L"ab")); assert(testSplits(L"\002")); assert(testSplits(L"\002ab")); assert(testSplits(L"a\002b")); assert(testSplits(L"\002\003")); assert(testSplits(L"a\002\003b")); assert(testSplits(L"a\0031b")); assert(testSplits(L"a\00312b")); assert(testSplits(L"a\00312,1b")); assert(testSplits(L"a\00312,12b")); } #endif