Update baseline symbols for hppa-linux.
[official-gcc.git] / gcc / text-art / styled-string.cc
bloba0cc187c8cb2c6cd251d0f2e46b5061484d0ba67
1 /* Implementation of text_art::styled_string.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #define INCLUDE_VECTOR
24 #include "system.h"
25 #include "coretypes.h"
26 #include "make-unique.h"
27 #include "pretty-print.h"
28 #include "intl.h"
29 #include "diagnostic.h"
30 #include "selftest.h"
31 #include "text-art/selftests.h"
32 #include "text-art/types.h"
33 #include "color-macros.h"
35 using namespace text_art;
37 namespace {
39 /* Support class for parsing text containing escape codes.
40 See e.g. https://en.wikipedia.org/wiki/ANSI_escape_code
41 We only support the codes that pretty-print.cc can generate. */
43 class escape_code_parser
45 public:
46 escape_code_parser (style_manager &sm,
47 std::vector<styled_unichar> &out)
48 : m_sm (sm),
49 m_out (out),
50 m_cur_style_obj (),
51 m_cur_style_id (style::id_plain),
52 m_state (state::START)
56 void on_char (cppchar_t ch)
58 switch (m_state)
60 default:
61 gcc_unreachable ();
62 case state::START:
63 if (ch == '\033')
65 /* The start of an escape sequence. */
66 m_state = state::AFTER_ESC;
67 return;
69 break;
70 case state::AFTER_ESC:
71 if (ch == '[')
73 /* ESC [ is a Control Sequence Introducer. */
74 m_state = state::CS_PARAMETER_BYTES;
75 return;
77 else if (ch == ']')
79 /* ESC ] is an Operating System Command. */
80 m_state = state::WITHIN_OSC;
81 return;
83 break;
84 case state::CS_PARAMETER_BYTES:
85 if (parameter_byte_p (ch))
87 m_parameter_bytes.push_back ((char)ch);
88 return;
90 else if (intermediate_byte_p (ch))
92 m_intermediate_bytes.push_back ((char)ch);
93 m_state = state::CS_INTERMEDIATE_BYTES;
94 return;
96 else if (final_byte_p (ch))
98 on_final_csi_char (ch);
99 return;
101 break;
102 case state::CS_INTERMEDIATE_BYTES:
103 /* Expect zero or more intermediate bytes. */
104 if (intermediate_byte_p (ch))
106 m_intermediate_bytes.push_back ((char)ch);
107 return;
109 else if (final_byte_p (ch))
111 on_final_csi_char (ch);
112 return;
114 break;
115 case state::WITHIN_OSC:
116 /* Accumulate chars into m_osc_string, until we see an ST or a BEL. */
118 /* Check for ESC \, the String Terminator (aka "ST"). */
119 if (ch == '\\'
120 && m_osc_string.size () > 0
121 && m_osc_string.back () == '\033')
123 m_osc_string.pop_back ();
124 on_final_osc_char ();
125 return;
127 else if (ch == '\a')
129 // BEL
130 on_final_osc_char ();
131 return;
133 m_osc_string.push_back (ch);
134 return;
136 break;
139 /* Test of handling U+FE0F VARIATION SELECTOR-16 to select the emoji
140 variation for the previous character. */
141 if (ch == 0xFE0F)
143 if (m_out.size () > 0)
144 m_out.back ().set_emoji_variant ();
145 return;
148 if (cpp_is_combining_char (ch))
150 if (m_out.size () > 0)
152 m_out.back ().add_combining_char (ch);
153 return;
156 /* By default, add the char. */
157 m_out.push_back (styled_unichar (ch, false, m_cur_style_id));
160 private:
161 void on_final_csi_char (cppchar_t ch)
163 switch (ch)
165 default:
166 /* Unrecognized. */
167 break;
168 case 'm':
170 /* SGR control sequence. */
171 if (m_parameter_bytes.empty ())
172 reset_style ();
173 std::vector<int> params (params_from_decimal ());
174 for (auto iter = params.begin (); iter != params.end (); )
176 const int param = *iter;
177 switch (param)
179 default:
180 /* Unrecognized SGR parameter. */
181 break;
182 case 0:
183 reset_style ();
184 break;
185 case 1:
186 set_style_bold ();
187 break;
188 case 4:
189 set_style_underscore ();
190 break;
191 case 5:
192 set_style_blink ();
193 break;
195 /* Named foreground colors. */
196 case 30:
197 set_style_fg_color (style::named_color::BLACK);
198 break;
199 case 31:
200 set_style_fg_color (style::named_color::RED);
201 break;
202 case 32:
203 set_style_fg_color (style::named_color::GREEN);
204 break;
205 case 33:
206 set_style_fg_color (style::named_color::YELLOW);
207 break;
208 case 34:
209 set_style_fg_color (style::named_color::BLUE);
210 break;
211 case 35:
212 set_style_fg_color (style::named_color::MAGENTA);
213 break;
214 case 36:
215 set_style_fg_color (style::named_color::CYAN);
216 break;
217 case 37:
218 set_style_fg_color (style::named_color::WHITE);
219 break;
221 /* 8-bit and 24-bit color */
222 case 38:
223 case 48:
225 const bool fg = (param == 38);
226 iter++;
227 if (iter != params.end ())
228 switch (*(iter++))
230 default:
231 break;
232 case 5:
233 /* 8-bit color. */
234 if (iter != params.end ())
236 const uint8_t col = *(iter++);
237 if (fg)
238 set_style_fg_color (style::color (col));
239 else
240 set_style_bg_color (style::color (col));
242 continue;
243 case 2:
244 /* 24-bit color. */
245 if (iter != params.end ())
247 const uint8_t r = *(iter++);
248 if (iter != params.end ())
250 const uint8_t g = *(iter++);
251 if (iter != params.end ())
253 const uint8_t b = *(iter++);
254 if (fg)
255 set_style_fg_color (style::color (r,
257 b));
258 else
259 set_style_bg_color (style::color (r,
261 b));
265 continue;
267 continue;
269 break;
271 /* Named background colors. */
272 case 40:
273 set_style_bg_color (style::named_color::BLACK);
274 break;
275 case 41:
276 set_style_bg_color (style::named_color::RED);
277 break;
278 case 42:
279 set_style_bg_color (style::named_color::GREEN);
280 break;
281 case 43:
282 set_style_bg_color (style::named_color::YELLOW);
283 break;
284 case 44:
285 set_style_bg_color (style::named_color::BLUE);
286 break;
287 case 45:
288 set_style_bg_color (style::named_color::MAGENTA);
289 break;
290 case 46:
291 set_style_bg_color (style::named_color::CYAN);
292 break;
293 case 47:
294 set_style_bg_color (style::named_color::WHITE);
295 break;
297 /* Named foreground colors, bright. */
298 case 90:
299 set_style_fg_color (style::color (style::named_color::BLACK,
300 true));
301 break;
302 case 91:
303 set_style_fg_color (style::color (style::named_color::RED,
304 true));
305 break;
306 case 92:
307 set_style_fg_color (style::color (style::named_color::GREEN,
308 true));
309 break;
310 case 93:
311 set_style_fg_color (style::color (style::named_color::YELLOW,
312 true));
313 break;
314 case 94:
315 set_style_fg_color (style::color (style::named_color::BLUE,
316 true));
317 break;
318 case 95:
319 set_style_fg_color (style::color (style::named_color::MAGENTA,
320 true));
321 break;
322 case 96:
323 set_style_fg_color (style::color (style::named_color::CYAN,
324 true));
325 break;
326 case 97:
327 set_style_fg_color (style::color (style::named_color::WHITE,
328 true));
329 break;
331 /* Named foreground colors, bright. */
332 case 100:
333 set_style_bg_color (style::color (style::named_color::BLACK,
334 true));
335 break;
336 case 101:
337 set_style_bg_color (style::color (style::named_color::RED,
338 true));
339 break;
340 case 102:
341 set_style_bg_color (style::color (style::named_color::GREEN,
342 true));
343 break;
344 case 103:
345 set_style_bg_color (style::color (style::named_color::YELLOW,
346 true));
347 break;
348 case 104:
349 set_style_bg_color (style::color (style::named_color::BLUE,
350 true));
351 break;
352 case 105:
353 set_style_bg_color (style::color (style::named_color::MAGENTA,
354 true));
355 break;
356 case 106:
357 set_style_bg_color (style::color (style::named_color::CYAN,
358 true));
359 break;
360 case 107:
361 set_style_bg_color (style::color (style::named_color::WHITE,
362 true));
363 break;
365 ++iter;
368 break;
370 m_parameter_bytes.clear ();
371 m_intermediate_bytes.clear ();
372 m_state = state::START;
375 void on_final_osc_char ()
377 if (!m_osc_string.empty ())
379 switch (m_osc_string[0])
381 default:
382 break;
383 case '8':
384 /* Hyperlink support; see:
385 https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
386 We don't support params, so we expect either:
387 (a) "8;;URL" to begin a url (see pp_begin_url), or
388 (b) "8;;" to end a URL (see pp_end_url). */
389 if (m_osc_string.size () >= 3
390 && m_osc_string[1] == ';'
391 && m_osc_string[2] == ';')
393 set_style_url (m_osc_string.begin () + 3,
394 m_osc_string.end ());
396 break;
399 m_osc_string.clear ();
400 m_state = state::START;
403 std::vector<int> params_from_decimal () const
405 std::vector<int> result;
407 int curr_int = -1;
408 for (auto param_ch : m_parameter_bytes)
410 if (param_ch >= '0' && param_ch <= '9')
412 if (curr_int == -1)
413 curr_int = 0;
414 else
415 curr_int *= 10;
416 curr_int += param_ch - '0';
418 else
420 if (curr_int != -1)
422 result.push_back (curr_int);
423 curr_int = -1;
427 if (curr_int != -1)
428 result.push_back (curr_int);
429 return result;
432 void refresh_style_id ()
434 m_cur_style_id = m_sm.get_or_create_id (m_cur_style_obj);
436 void reset_style ()
438 m_cur_style_obj = style ();
439 refresh_style_id ();
441 void set_style_bold ()
443 m_cur_style_obj.m_bold = true;
444 refresh_style_id ();
446 void set_style_underscore ()
448 m_cur_style_obj.m_underscore = true;
449 refresh_style_id ();
451 void set_style_blink ()
453 m_cur_style_obj.m_blink = true;
454 refresh_style_id ();
456 void set_style_fg_color (style::color color)
458 m_cur_style_obj.m_fg_color = color;
459 refresh_style_id ();
461 void set_style_bg_color (style::color color)
463 m_cur_style_obj.m_bg_color = color;
464 refresh_style_id ();
466 void set_style_url (std::vector<cppchar_t>::iterator begin,
467 std::vector<cppchar_t>::iterator end)
469 // The empty string means "no URL"
470 m_cur_style_obj.m_url = std::vector<cppchar_t> (begin, end);
471 refresh_style_id ();
474 static bool parameter_byte_p (cppchar_t ch)
476 return ch >= 0x30 && ch <= 0x3F;
479 static bool intermediate_byte_p (cppchar_t ch)
481 return ch >= 0x20 && ch <= 0x2F;
484 static bool final_byte_p (cppchar_t ch)
486 return ch >= 0x40 && ch <= 0x7E;
489 style_manager &m_sm;
490 std::vector<styled_unichar> &m_out;
492 style m_cur_style_obj;
493 style::id_t m_cur_style_id;
495 /* Handling of control sequences. */
496 enum class state
498 START,
500 /* After ESC, expecting '['. */
501 AFTER_ESC,
503 /* Expecting zero or more parameter bytes, an
504 intermediate byte, or a final byte. */
505 CS_PARAMETER_BYTES,
507 /* Expecting zero or more intermediate bytes, or a final byte. */
508 CS_INTERMEDIATE_BYTES,
510 /* Within OSC. */
511 WITHIN_OSC
513 } m_state;
514 std::vector<char> m_parameter_bytes;
515 std::vector<char> m_intermediate_bytes;
516 std::vector<cppchar_t> m_osc_string;
519 } // anon namespace
521 /* class text_art::styled_string. */
523 /* Construct a styled_string from STR.
524 STR is assumed to be UTF-8 encoded and 0-terminated.
526 Parse SGR formatting chars from being in-band (within in the sequence
527 of chars) to being out-of-band, as style elements.
528 We only support parsing the subset of SGR chars that can be emitted
529 by pretty-print.cc */
531 styled_string::styled_string (style_manager &sm, const char *str)
532 : m_chars ()
534 escape_code_parser parser (sm, m_chars);
536 /* We don't actually want the display widths here, but
537 it's an easy way to decode UTF-8. */
538 cpp_char_column_policy policy (8, cpp_wcwidth);
539 cpp_display_width_computation dw (str, strlen (str), policy);
540 while (!dw.done ())
542 cpp_decoded_char decoded_char;
543 dw.process_next_codepoint (&decoded_char);
545 if (!decoded_char.m_valid_ch)
546 /* Skip bytes that aren't valid UTF-8. */
547 continue;
549 /* Decode SGR formatting. */
550 cppchar_t ch = decoded_char.m_ch;
551 parser.on_char (ch);
555 styled_string::styled_string (cppchar_t cppchar, bool emoji)
557 m_chars.push_back (styled_unichar (cppchar, emoji, style::id_plain));
560 styled_string
561 styled_string::from_fmt_va (style_manager &sm,
562 printer_fn format_decoder,
563 const char *fmt,
564 va_list *args)
566 text_info text;
567 text.err_no = errno;
568 text.args_ptr = args;
569 text.format_spec = fmt;
570 pretty_printer pp;
571 pp_show_color (&pp) = true;
572 pp.url_format = URL_FORMAT_DEFAULT;
573 pp_format_decoder (&pp) = format_decoder;
574 pp_format (&pp, &text);
575 pp_output_formatted_text (&pp);
576 styled_string result (sm, pp_formatted_text (&pp));
577 return result;
580 styled_string
581 styled_string::from_fmt (style_manager &sm,
582 printer_fn format_decoder,
583 const char *fmt, ...)
585 va_list ap;
586 va_start (ap, fmt);
587 styled_string result = from_fmt_va (sm, format_decoder, fmt, &ap);
588 va_end (ap);
589 return result;
593 styled_string::calc_canvas_width () const
595 int result = 0;
596 for (auto ch : m_chars)
597 result += ch.get_canvas_width ();
598 return result;
601 void
602 styled_string::append (const styled_string &suffix)
604 m_chars.insert<std::vector<styled_unichar>::const_iterator> (m_chars.end (),
605 suffix.begin (),
606 suffix.end ());
609 void
610 styled_string::set_url (style_manager &sm, const char *url)
612 for (auto& ch : m_chars)
614 const style &existing_style = sm.get_style (ch.get_style_id ());
615 style with_url (existing_style);
616 with_url.set_style_url (url);
617 ch.m_style_id = sm.get_or_create_id (with_url);
621 #if CHECKING_P
623 namespace selftest {
625 static void
626 test_combining_chars ()
628 /* This really ought to be in libcpp, but we don't have
629 selftests there. */
630 ASSERT_FALSE (cpp_is_combining_char (0));
631 ASSERT_FALSE (cpp_is_combining_char ('a'));
633 /* COMBINING BREVE (U+0306). */
634 ASSERT_TRUE (cpp_is_combining_char (0x0306));
636 /* U+5B57 CJK UNIFIED IDEOGRAPH-5B57. */
637 ASSERT_FALSE (cpp_is_combining_char (0x5B57));
639 /* U+FE0F VARIATION SELECTOR-16. */
640 ASSERT_FALSE (cpp_is_combining_char (0xFE0F));
643 static void
644 test_empty ()
646 style_manager sm;
647 styled_string s (sm, "");
648 ASSERT_EQ (s.size (), 0);
649 ASSERT_EQ (s.calc_canvas_width (), 0);
652 /* Test of a pure ASCII string with no escape codes. */
654 static void
655 test_simple ()
657 const char *c_str = "hello world!";
658 style_manager sm;
659 styled_string s (sm, c_str);
660 ASSERT_EQ (s.size (), strlen (c_str));
661 ASSERT_EQ (s.calc_canvas_width (), (int)strlen (c_str));
662 for (size_t i = 0; i < strlen (c_str); i++)
664 ASSERT_EQ (s[i].get_code (), (cppchar_t)c_str[i]);
665 ASSERT_EQ (s[i].get_style_id (), 0);
669 /* Test of decoding UTF-8. */
671 static void
672 test_pi_from_utf8 ()
674 /* U+03C0 "GREEK SMALL LETTER PI". */
675 const char * const pi_utf8 = "\xCF\x80";
677 style_manager sm;
678 styled_string s (sm, pi_utf8);
679 ASSERT_EQ (s.size (), 1);
680 ASSERT_EQ (s.calc_canvas_width (), 1);
681 ASSERT_EQ (s[0].get_code (), 0x03c0);
682 ASSERT_EQ (s[0].emoji_variant_p (), false);
683 ASSERT_EQ (s[0].double_width_p (), false);
684 ASSERT_EQ (s[0].get_style_id (), 0);
687 /* Test of double-width character. */
689 static void
690 test_emoji_from_utf8 ()
692 /* U+1F642 "SLIGHTLY SMILING FACE". */
693 const char * const emoji_utf8 = "\xF0\x9F\x99\x82";
695 style_manager sm;
696 styled_string s (sm, emoji_utf8);
697 ASSERT_EQ (s.size (), 1);
698 ASSERT_EQ (s.calc_canvas_width (), 2);
699 ASSERT_EQ (s[0].get_code (), 0x1f642);
700 ASSERT_EQ (s[0].double_width_p (), true);
701 ASSERT_EQ (s[0].get_style_id (), 0);
704 /* Test of handling U+FE0F VARIATION SELECTOR-16 to select the emoji
705 variation for the previous character. */
707 static void
708 test_emoji_variant_from_utf8 ()
710 const char * const emoji_utf8
711 = (/* U+26A0 WARNING SIGN. */
712 "\xE2\x9A\xA0"
713 /* U+FE0F VARIATION SELECTOR-16 (emoji variation selector). */
714 "\xEF\xB8\x8F");
716 style_manager sm;
717 styled_string s (sm, emoji_utf8);
718 ASSERT_EQ (s.size (), 1);
719 ASSERT_EQ (s.calc_canvas_width (), 1);
720 ASSERT_EQ (s[0].get_code (), 0x26a0);
721 ASSERT_EQ (s[0].emoji_variant_p (), true);
722 ASSERT_EQ (s[0].double_width_p (), false);
723 ASSERT_EQ (s[0].get_style_id (), 0);
726 static void
727 test_emoji_from_codepoint ()
729 styled_string s ((cppchar_t)0x1f642);
730 ASSERT_EQ (s.size (), 1);
731 ASSERT_EQ (s.calc_canvas_width (), 2);
732 ASSERT_EQ (s[0].get_code (), 0x1f642);
733 ASSERT_EQ (s[0].double_width_p (), true);
734 ASSERT_EQ (s[0].get_style_id (), 0);
737 static void
738 test_from_mixed_width_utf8 ()
740 /* This UTF-8 string literal is of the form
741 before mojibake after
742 where the Japanese word "mojibake" is written as the following
743 four unicode code points:
744 U+6587 CJK UNIFIED IDEOGRAPH-6587
745 U+5B57 CJK UNIFIED IDEOGRAPH-5B57
746 U+5316 CJK UNIFIED IDEOGRAPH-5316
747 U+3051 HIRAGANA LETTER KE.
748 Each of these is 3 bytes wide when encoded in UTF-8, whereas the
749 "before" and "after" are 1 byte per unicode character. */
750 const char * const mixed_width_utf8
751 = ("before "
753 /* U+6587 CJK UNIFIED IDEOGRAPH-6587
754 UTF-8: 0xE6 0x96 0x87
755 C octal escaped UTF-8: \346\226\207. */
756 "\346\226\207"
758 /* U+5B57 CJK UNIFIED IDEOGRAPH-5B57
759 UTF-8: 0xE5 0xAD 0x97
760 C octal escaped UTF-8: \345\255\227. */
761 "\345\255\227"
763 /* U+5316 CJK UNIFIED IDEOGRAPH-5316
764 UTF-8: 0xE5 0x8C 0x96
765 C octal escaped UTF-8: \345\214\226. */
766 "\345\214\226"
768 /* U+3051 HIRAGANA LETTER KE
769 UTF-8: 0xE3 0x81 0x91
770 C octal escaped UTF-8: \343\201\221. */
771 "\343\201\221"
773 " after");
775 style_manager sm;
776 styled_string s (sm, mixed_width_utf8);
777 ASSERT_EQ (s.size (), 6 + 1 + 4 + 1 + 5);
778 ASSERT_EQ (sm.get_num_styles (), 1);
780 // We expect the Japanese characters to be double width.
781 ASSERT_EQ (s.calc_canvas_width (), 6 + 1 + (2 * 4) + 1 + 5);
783 ASSERT_EQ (s[0].get_code (), 'b');
784 ASSERT_EQ (s[0].double_width_p (), false);
785 ASSERT_EQ (s[1].get_code (), 'e');
786 ASSERT_EQ (s[2].get_code (), 'f');
787 ASSERT_EQ (s[3].get_code (), 'o');
788 ASSERT_EQ (s[4].get_code (), 'r');
789 ASSERT_EQ (s[5].get_code (), 'e');
790 ASSERT_EQ (s[6].get_code (), ' ');
791 ASSERT_EQ (s[7].get_code (), 0x6587);
792 ASSERT_EQ (s[7].double_width_p (), true);
793 ASSERT_EQ (s[8].get_code (), 0x5B57);
794 ASSERT_EQ (s[9].get_code (), 0x5316);
795 ASSERT_EQ (s[10].get_code (), 0x3051);
796 ASSERT_EQ (s[11].get_code (), ' ');
797 ASSERT_EQ (s[12].get_code (), 'a');
798 ASSERT_EQ (s[13].get_code (), 'f');
799 ASSERT_EQ (s[14].get_code (), 't');
800 ASSERT_EQ (s[15].get_code (), 'e');
801 ASSERT_EQ (s[16].get_code (), 'r');
803 ASSERT_EQ (s[0].get_style_id (), 0);
806 static void
807 assert_style_urleq (const location &loc,
808 const style &s,
809 const char *expected_str)
811 ASSERT_EQ_AT (loc, s.m_url.size (), strlen (expected_str));
812 for (size_t i = 0; i < s.m_url.size (); i++)
813 ASSERT_EQ_AT (loc, s.m_url[i], (cppchar_t)expected_str[i]);
816 #define ASSERT_STYLE_URLEQ(STYLE, EXPECTED_STR) \
817 assert_style_urleq ((SELFTEST_LOCATION), (STYLE), (EXPECTED_STR))
819 static void
820 test_url ()
822 // URL_FORMAT_ST
824 style_manager sm;
825 styled_string s
826 (sm, "\33]8;;http://example.com\33\\This is a link\33]8;;\33\\");
827 const char *expected = "This is a link";
828 ASSERT_EQ (s.size (), strlen (expected));
829 ASSERT_EQ (s.calc_canvas_width (), (int)strlen (expected));
830 ASSERT_EQ (sm.get_num_styles (), 2);
831 for (size_t i = 0; i < strlen (expected); i++)
833 ASSERT_EQ (s[i].get_code (), (cppchar_t)expected[i]);
834 ASSERT_EQ (s[i].get_style_id (), 1);
836 ASSERT_STYLE_URLEQ (sm.get_style (1), "http://example.com");
839 // URL_FORMAT_BEL
841 style_manager sm;
842 styled_string s
843 (sm, "\33]8;;http://example.com\aThis is a link\33]8;;\a");
844 const char *expected = "This is a link";
845 ASSERT_EQ (s.size (), strlen (expected));
846 ASSERT_EQ (s.calc_canvas_width (), (int)strlen (expected));
847 ASSERT_EQ (sm.get_num_styles (), 2);
848 for (size_t i = 0; i < strlen (expected); i++)
850 ASSERT_EQ (s[i].get_code (), (cppchar_t)expected[i]);
851 ASSERT_EQ (s[i].get_style_id (), 1);
853 ASSERT_STYLE_URLEQ (sm.get_style (1), "http://example.com");
857 static void
858 test_from_fmt ()
860 style_manager sm;
861 styled_string s (styled_string::from_fmt (sm, NULL, "%%i: %i", 42));
862 ASSERT_EQ (s[0].get_code (), '%');
863 ASSERT_EQ (s[1].get_code (), 'i');
864 ASSERT_EQ (s[2].get_code (), ':');
865 ASSERT_EQ (s[3].get_code (), ' ');
866 ASSERT_EQ (s[4].get_code (), '4');
867 ASSERT_EQ (s[5].get_code (), '2');
868 ASSERT_EQ (s.size (), 6);
869 ASSERT_EQ (s.calc_canvas_width (), 6);
872 static void
873 test_from_fmt_qs ()
875 auto_fix_quotes fix_quotes;
876 open_quote = "\xe2\x80\x98";
877 close_quote = "\xe2\x80\x99";
879 style_manager sm;
880 styled_string s (styled_string::from_fmt (sm, NULL, "%qs", "msg"));
881 ASSERT_EQ (sm.get_num_styles (), 2);
882 ASSERT_EQ (s[0].get_code (), 0x2018);
883 ASSERT_EQ (s[0].get_style_id (), 0);
884 ASSERT_EQ (s[1].get_code (), 'm');
885 ASSERT_EQ (s[1].get_style_id (), 1);
886 ASSERT_EQ (s[2].get_code (), 's');
887 ASSERT_EQ (s[2].get_style_id (), 1);
888 ASSERT_EQ (s[3].get_code (), 'g');
889 ASSERT_EQ (s[3].get_style_id (), 1);
890 ASSERT_EQ (s[4].get_code (), 0x2019);
891 ASSERT_EQ (s[4].get_style_id (), 0);
892 ASSERT_EQ (s.size (), 5);
895 // Test of parsing SGR codes.
897 static void
898 test_from_str_with_bold ()
900 style_manager sm;
901 /* This is the result of pp_printf (pp, "%qs", "foo")
902 with auto_fix_quotes. */
903 styled_string s (sm, "`\33[01m\33[Kfoo\33[m\33[K'");
904 ASSERT_EQ (s[0].get_code (), '`');
905 ASSERT_EQ (s[0].get_style_id (), 0);
906 ASSERT_EQ (s[1].get_code (), 'f');
907 ASSERT_EQ (s[1].get_style_id (), 1);
908 ASSERT_EQ (s[2].get_code (), 'o');
909 ASSERT_EQ (s[2].get_style_id (), 1);
910 ASSERT_EQ (s[3].get_code (), 'o');
911 ASSERT_EQ (s[3].get_style_id (), 1);
912 ASSERT_EQ (s[4].get_code (), '\'');
913 ASSERT_EQ (s[4].get_style_id (), 0);
914 ASSERT_EQ (s.size (), 5);
915 ASSERT_TRUE (sm.get_style (1).m_bold);
918 static void
919 test_from_str_with_underscore ()
921 style_manager sm;
922 styled_string s (sm, "\33[04m\33[KA");
923 ASSERT_EQ (s[0].get_code (), 'A');
924 ASSERT_EQ (s[0].get_style_id (), 1);
925 ASSERT_TRUE (sm.get_style (1).m_underscore);
928 static void
929 test_from_str_with_blink ()
931 style_manager sm;
932 styled_string s (sm, "\33[05m\33[KA");
933 ASSERT_EQ (s[0].get_code (), 'A');
934 ASSERT_EQ (s[0].get_style_id (), 1);
935 ASSERT_TRUE (sm.get_style (1).m_blink);
938 // Test of parsing SGR codes.
940 static void
941 test_from_str_with_color ()
943 style_manager sm;
945 styled_string s (sm,
946 ("0"
947 SGR_SEQ (COLOR_FG_RED)
949 SGR_RESET
951 SGR_SEQ (COLOR_FG_GREEN)
953 SGR_RESET
954 "4"));
955 ASSERT_EQ (s.size (), 5);
956 ASSERT_EQ (sm.get_num_styles (), 3);
957 ASSERT_EQ (s[0].get_code (), '0');
958 ASSERT_EQ (s[0].get_style_id (), 0);
959 ASSERT_EQ (s[1].get_code (), 'R');
960 ASSERT_EQ (s[1].get_style_id (), 1);
961 ASSERT_EQ (s[2].get_code (), '2');
962 ASSERT_EQ (s[2].get_style_id (), 0);
963 ASSERT_EQ (s[3].get_code (), 'G');
964 ASSERT_EQ (s[3].get_style_id (), 2);
965 ASSERT_EQ (s[4].get_code (), '4');
966 ASSERT_EQ (s[4].get_style_id (), 0);
967 ASSERT_EQ (sm.get_style (1).m_fg_color, style::named_color::RED);
968 ASSERT_EQ (sm.get_style (2).m_fg_color, style::named_color::GREEN);
971 static void
972 test_from_str_with_named_color ()
974 style_manager sm;
975 styled_string s (sm,
976 ("F"
977 SGR_SEQ (COLOR_FG_BLACK) "F"
978 SGR_SEQ (COLOR_FG_RED) "F"
979 SGR_SEQ (COLOR_FG_GREEN) "F"
980 SGR_SEQ (COLOR_FG_YELLOW) "F"
981 SGR_SEQ (COLOR_FG_BLUE) "F"
982 SGR_SEQ (COLOR_FG_MAGENTA) "F"
983 SGR_SEQ (COLOR_FG_CYAN) "F"
984 SGR_SEQ (COLOR_FG_WHITE) "F"
985 SGR_SEQ (COLOR_FG_BRIGHT_BLACK) "F"
986 SGR_SEQ (COLOR_FG_BRIGHT_RED) "F"
987 SGR_SEQ (COLOR_FG_BRIGHT_GREEN) "F"
988 SGR_SEQ (COLOR_FG_BRIGHT_YELLOW) "F"
989 SGR_SEQ (COLOR_FG_BRIGHT_BLUE) "F"
990 SGR_SEQ (COLOR_FG_BRIGHT_MAGENTA) "F"
991 SGR_SEQ (COLOR_FG_BRIGHT_CYAN) "F"
992 SGR_SEQ (COLOR_FG_BRIGHT_WHITE) "F"
993 SGR_SEQ (COLOR_BG_BLACK) "B"
994 SGR_SEQ (COLOR_BG_RED) "B"
995 SGR_SEQ (COLOR_BG_GREEN) "B"
996 SGR_SEQ (COLOR_BG_YELLOW) "B"
997 SGR_SEQ (COLOR_BG_BLUE) "B"
998 SGR_SEQ (COLOR_BG_MAGENTA) "B"
999 SGR_SEQ (COLOR_BG_CYAN) "B"
1000 SGR_SEQ (COLOR_BG_WHITE) "B"
1001 SGR_SEQ (COLOR_BG_BRIGHT_BLACK) "B"
1002 SGR_SEQ (COLOR_BG_BRIGHT_RED) "B"
1003 SGR_SEQ (COLOR_BG_BRIGHT_GREEN) "B"
1004 SGR_SEQ (COLOR_BG_BRIGHT_YELLOW) "B"
1005 SGR_SEQ (COLOR_BG_BRIGHT_BLUE) "B"
1006 SGR_SEQ (COLOR_BG_BRIGHT_MAGENTA) "B"
1007 SGR_SEQ (COLOR_BG_BRIGHT_CYAN) "B"
1008 SGR_SEQ (COLOR_BG_BRIGHT_WHITE) "B"));
1009 ASSERT_EQ (s.size (), 33);
1010 for (size_t i = 0; i < s.size (); i++)
1011 ASSERT_EQ (s[i].get_style_id (), i);
1012 for (size_t i = 0; i < 17; i++)
1013 ASSERT_EQ (s[i].get_code (), 'F');
1014 for (size_t i = 17; i < 33; i++)
1015 ASSERT_EQ (s[i].get_code (), 'B');
1018 static void
1019 test_from_str_with_8_bit_color ()
1022 style_manager sm;
1023 styled_string s (sm,
1024 ("\e[38;5;232m\e[KF"));
1025 ASSERT_EQ (s.size (), 1);
1026 ASSERT_EQ (s[0].get_code (), 'F');
1027 ASSERT_EQ (s[0].get_style_id (), 1);
1028 ASSERT_EQ (sm.get_style (1).m_fg_color, style::color (232));
1031 style_manager sm;
1032 styled_string s (sm,
1033 ("\e[48;5;231m\e[KB"));
1034 ASSERT_EQ (s.size (), 1);
1035 ASSERT_EQ (s[0].get_code (), 'B');
1036 ASSERT_EQ (s[0].get_style_id (), 1);
1037 ASSERT_EQ (sm.get_style (1).m_bg_color, style::color (231));
1041 static void
1042 test_from_str_with_24_bit_color ()
1045 style_manager sm;
1046 styled_string s (sm,
1047 ("\e[38;2;243;250;242m\e[KF"));
1048 ASSERT_EQ (s.size (), 1);
1049 ASSERT_EQ (s[0].get_code (), 'F');
1050 ASSERT_EQ (s[0].get_style_id (), 1);
1051 ASSERT_EQ (sm.get_style (1).m_fg_color, style::color (243, 250, 242));
1054 style_manager sm;
1055 styled_string s (sm,
1056 ("\e[48;2;253;247;231m\e[KB"));
1057 ASSERT_EQ (s.size (), 1);
1058 ASSERT_EQ (s[0].get_code (), 'B');
1059 ASSERT_EQ (s[0].get_style_id (), 1);
1060 ASSERT_EQ (sm.get_style (1).m_bg_color, style::color (253, 247, 231));
1064 static void
1065 test_from_str_combining_characters ()
1067 style_manager sm;
1068 styled_string s (sm,
1069 /* CYRILLIC CAPITAL LETTER U (U+0423). */
1070 "\xD0\xA3"
1071 /* COMBINING BREVE (U+0306). */
1072 "\xCC\x86");
1073 ASSERT_EQ (s.size (), 1);
1074 ASSERT_EQ (s[0].get_code (), 0x423);
1075 ASSERT_EQ (s[0].get_combining_chars ().size (), 1);
1076 ASSERT_EQ (s[0].get_combining_chars ()[0], 0x306);
1079 /* Run all selftests in this file. */
1081 void
1082 text_art_styled_string_cc_tests ()
1084 test_combining_chars ();
1085 test_empty ();
1086 test_simple ();
1087 test_pi_from_utf8 ();
1088 test_emoji_from_utf8 ();
1089 test_emoji_variant_from_utf8 ();
1090 test_emoji_from_codepoint ();
1091 test_from_mixed_width_utf8 ();
1092 test_url ();
1093 test_from_fmt ();
1094 test_from_fmt_qs ();
1095 test_from_str_with_bold ();
1096 test_from_str_with_underscore ();
1097 test_from_str_with_blink ();
1098 test_from_str_with_color ();
1099 test_from_str_with_named_color ();
1100 test_from_str_with_8_bit_color ();
1101 test_from_str_with_24_bit_color ();
1102 test_from_str_combining_characters ();
1105 } // namespace selftest
1108 #endif /* #if CHECKING_P */