IRC client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright (C) 2018 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <wchar.h>
  20. #include "chat.h"
  21. void formatReset(struct Format *format) {
  22. format->bold = false;
  23. format->italic = false;
  24. format->underline = false;
  25. format->reverse = false;
  26. format->fg = IRCDefault;
  27. format->bg = IRCDefault;
  28. }
  29. static void parseColor(struct Format *format) {
  30. size_t len = MIN(wcsspn(format->str, L"0123456789"), 2);
  31. if (!len) {
  32. format->fg = IRCDefault;
  33. format->bg = IRCDefault;
  34. return;
  35. }
  36. format->fg = 0;
  37. for (size_t i = 0; i < len; ++i) {
  38. format->fg *= 10;
  39. format->fg += format->str[i] - L'0';
  40. }
  41. if (format->fg > IRCLightGray) format->fg = IRCDefault;
  42. format->str = &format->str[len];
  43. len = 0;
  44. if (format->str[0] == L',') {
  45. len = MIN(wcsspn(&format->str[1], L"0123456789"), 2);
  46. }
  47. if (!len) return;
  48. format->bg = 0;
  49. for (size_t i = 0; i < len; ++i) {
  50. format->bg *= 10;
  51. format->bg += format->str[1 + i] - L'0';
  52. }
  53. if (format->bg > IRCLightGray) format->bg = IRCDefault;
  54. format->str = &format->str[1 + len];
  55. }
  56. static const wchar_t Codes[] = {
  57. IRCBold, IRCColor, IRCReverse, IRCReset, IRCItalic, IRCUnderline, L'\0',
  58. };
  59. bool formatParse(struct Format *format, const wchar_t *split) {
  60. format->str += format->len;
  61. if (!format->str[0]) {
  62. if (split == format->str && !format->split) {
  63. format->len = 0;
  64. format->split = true;
  65. return true;
  66. }
  67. return false;
  68. }
  69. const wchar_t *init = format->str;
  70. for (bool done = false; !done;) {
  71. switch (format->str[0]) {
  72. break; case IRCBold: format->str++; format->bold ^= true;
  73. break; case IRCItalic: format->str++; format->italic ^= true;
  74. break; case IRCUnderline: format->str++; format->underline ^= true;
  75. break; case IRCReverse: format->str++; format->reverse ^= true;
  76. break; case IRCColor: format->str++; parseColor(format);
  77. break; case IRCReset: format->str++; formatReset(format);
  78. break; default: done = true;
  79. }
  80. }
  81. format->split = (split >= init && split <= format->str);
  82. format->len = wcscspn(format->str, Codes);
  83. if (split > format->str && split < &format->str[format->len]) {
  84. format->len = split - format->str;
  85. }
  86. return true;
  87. }
  88. #ifdef TEST
  89. #include <assert.h>
  90. static bool testColor(
  91. const wchar_t *str, enum IRCColor fg, enum IRCColor bg, size_t index
  92. ) {
  93. struct Format format = { .str = str };
  94. formatReset(&format);
  95. if (!formatParse(&format, NULL)) return false;
  96. if (format.fg != fg) return false;
  97. if (format.bg != bg) return false;
  98. return (format.str == &str[index]);
  99. }
  100. static bool testSplit(const wchar_t *str, size_t index) {
  101. struct Format format = { .str = str };
  102. formatReset(&format);
  103. bool split = false;
  104. while (formatParse(&format, &str[index])) {
  105. if (format.split && split) return false;
  106. if (format.split) split = true;
  107. }
  108. return split;
  109. }
  110. static bool testSplits(const wchar_t *str) {
  111. for (size_t i = 0; i <= wcslen(str); ++i) {
  112. if (!testSplit(str, i)) return false;
  113. }
  114. return true;
  115. }
  116. int main() {
  117. assert(testColor(L"\003a", IRCDefault, IRCDefault, 1));
  118. assert(testColor(L"\003,a", IRCDefault, IRCDefault, 1));
  119. assert(testColor(L"\003,1", IRCDefault, IRCDefault, 1));
  120. assert(testColor(L"\0031a", IRCBlack, IRCDefault, 2));
  121. assert(testColor(L"\0031,a", IRCBlack, IRCDefault, 2));
  122. assert(testColor(L"\00312a", IRCLightBlue, IRCDefault, 3));
  123. assert(testColor(L"\00312,a", IRCLightBlue, IRCDefault, 3));
  124. assert(testColor(L"\003123", IRCLightBlue, IRCDefault, 3));
  125. assert(testColor(L"\0031,1a", IRCBlack, IRCBlack, 4));
  126. assert(testColor(L"\0031,12a", IRCBlack, IRCLightBlue, 5));
  127. assert(testColor(L"\0031,123", IRCBlack, IRCLightBlue, 5));
  128. assert(testColor(L"\00312,1a", IRCLightBlue, IRCBlack, 5));
  129. assert(testColor(L"\00312,12a", IRCLightBlue, IRCLightBlue, 6));
  130. assert(testColor(L"\00312,123", IRCLightBlue, IRCLightBlue, 6));
  131. assert(testColor(L"\00316,16a", IRCDefault, IRCDefault, 6));
  132. assert(testColor(L"\00399,99a", IRCDefault, IRCDefault, 6));
  133. assert(testSplits(L""));
  134. assert(testSplits(L"ab"));
  135. assert(testSplits(L"\002"));
  136. assert(testSplits(L"\002ab"));
  137. assert(testSplits(L"a\002b"));
  138. assert(testSplits(L"\002\003"));
  139. assert(testSplits(L"a\002\003b"));
  140. assert(testSplits(L"a\0031b"));
  141. assert(testSplits(L"a\00312b"));
  142. assert(testSplits(L"a\00312,1b"));
  143. assert(testSplits(L"a\00312,12b"));
  144. }
  145. #endif