From 872608e5c4fb814604a359a4b8b2f3bcf317cf0f Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Wed, 12 Sep 2018 20:23:45 -0400 Subject: Factor out IRC formatting parsing --- format.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 format.c (limited to 'format.c') diff --git a/format.c b/format.c new file mode 100644 index 0000000..800388b --- /dev/null +++ b/format.c @@ -0,0 +1,89 @@ +/* Copyright (C) 2018 Curtis McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include "chat.h" + +void formatReset(struct Format *format) { + format->bold = false; + format->italic = false; + format->underline = false; + format->reverse = false; + format->fg = -1; + format->bg = -1; +} + +static void parseColor(struct Format *format) { + size_t len = MIN(wcsspn(format->str, L"0123456789"), 2); + if (!len) { + format->fg = -1; + format->bg = -1; + return; + } + format->fg = 0; + for (size_t i = 0; i < len; ++i) { + format->fg *= 10; + format->fg += format->str[i] - L'0'; + } + if (format->fg > IRCLightGray) format->fg = -1; + format->str = &format->str[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 = -1; + format->str = &format->str[1 + len]; +} + +static const wchar_t Stops[] = { + L' ', + IRCBold, IRCColor, IRCReverse, IRCReset, IRCItalic, IRCUnderline, + L'\0', +}; + +bool formatParse(struct Format *format, const wchar_t *stop) { + format->str += format->len; + if (!format->str[0]) return false; + + 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); + } + + if (format->str[0] == L' ') { + format->len = 1 + wcscspn(&format->str[1], Stops); + } else { + format->len = wcscspn(format->str, Stops); + } + if (stop && stop > format->str && stop < &format->str[format->len]) { + format->len = stop - format->str; + } + return true; +} -- cgit 1.4.1 026518ebfca077abf33e72d&follow=1'>Update to OpenBSD 7.4 2064.25Kusoneko 2023-10-11Update to Linux man-pages 6.05 2064.15Štěpán Němec 2023-10-11Update to FreeBSD 13.2Štěpán Němec 2023-10-11Update to Linux man-pages 6.04, OpenBSD 7.3Štěpán Němec 2023-02-13Update to Linux man-pages 6.03 2063.93Štěpán Němec 2023-02-13Update to Linux man-pages 6.02Štěpán Němec 2022-11-01Update to Linux man-pages 6.01 2063.91Štěpán Němec 2022-10-21Update to OpenBSD 7.2 2063.9June McEnroe 2022-10-16NetBSD: include text.tar.xz man pages 2063.8Štěpán Němec 2022-10-16Update to Linux man-mages 6.00Štěpán Němec 2022-10-16Update to NetBSD 9.3Štěpán Němec 2022-09-18Make copyrights and AUTHORS consistentJune McEnroe 2022-07-27Update to FreeBSD 13.1 2062.83June McEnroe 2022-07-27openbsd: Include man pages from baseXX.tgzKeith Whitney 2022-05-08Update to OpenBSD 7.1 2062.73June McEnroe 2021-10-15Update to OpenBSD 7.0 2062.63June McEnroe 2021-08-29Update to Linux man-pages 5.13 2062.53Štěpán Němec 2021-08-26Update to NetBSD 9.2 2062.52June McEnroe 2021-08-26Support DESTDIR in install/uninstallJune McEnroe 2021-08-26Add version number generatorJune McEnroe 2021-08-22Add ISC license headerJune McEnroe 2021-08-22Update to Linux man-pages 5.12Štěpán Němec 2021-06-21Add manuals for macOS 11.3June McEnroe 2021-05-08Update to OpenBSD 6.9June McEnroe 2021-04-26Update to Linux man-pages 5.11June McEnroe 2021-04-26Update to FreeBSD 13.0June McEnroe 2021-01-27Completely rewrite how manuals are fetched and installedJune McEnroe 2020-12-14Update to man-pages-posix 2017-aJune McEnroe 2020-12-14Update to OpenBSD 6.8June McEnroe 2020-12-14Update to NetBSD 9.1June McEnroe 2020-12-14Update to man-pages 5.09June McEnroe 2020-12-14Update to FreeBSD 12.2June McEnroe 2020-06-08Update to OpenBSD 6.7June McEnroe 2020-05-04Add hack for macOS to search extra man sectionsJune McEnroe 2020-05-04Don't clear MANSECTJune McEnroe