-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMarkdownTableCore.cpp
More file actions
2619 lines (2331 loc) · 73.2 KB
/
Copy pathMarkdownTableCore.cpp
File metadata and controls
2619 lines (2331 loc) · 73.2 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2026 krotname
#include "MarkdownTableCore.h"
#include <algorithm>
#include <cctype>
#include <clocale>
#include <cstdlib>
#include <utility>
namespace MarkdownTable
{
namespace
{
enum class Align
{
None,
Left,
Center,
Right
};
struct Row
{
std::vector<std::string> cells;
bool separator = false;
std::size_t id = 0;
};
struct Table
{
std::vector<Row> rows;
std::vector<Align> alignments;
std::size_t columns = 0;
std::size_t separatorRow = static_cast<std::size_t>(-1);
bool leadingPipe = true;
bool trailingPipe = true;
};
struct FormatResult
{
std::vector<std::string> lines;
std::size_t targetRow = 0;
std::size_t targetColumn = 0;
std::size_t targetColumnOffset = 0;
};
struct SortKey
{
bool numeric = false;
double number = 0;
std::string foldedText;
std::string text;
};
struct SortEntry
{
Row row;
SortKey key;
};
struct CodePointRange
{
unsigned int first;
unsigned int last;
};
const std::size_t hardWrapCellWidth = 26;
const std::size_t minimumAutoWrapCellWidth = 1;
std::size_t nextRowId(const Table &table);
std::size_t unwrapContinuationRows(Table &table, std::size_t originalTargetRow);
bool isSpace(unsigned char ch)
{
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
}
std::string trim(const std::string &value)
{
std::size_t first = 0;
while (first < value.size() && isSpace(static_cast<unsigned char>(value[first])))
++first;
std::size_t last = value.size();
while (last > first && isSpace(static_cast<unsigned char>(value[last - 1])))
--last;
return value.substr(first, last - first);
}
std::string trimRange(const std::string &value, std::size_t first, std::size_t last)
{
while (first < last && isSpace(static_cast<unsigned char>(value[first])))
++first;
while (last > first && isSpace(static_cast<unsigned char>(value[last - 1])))
--last;
return value.substr(first, last - first);
}
bool isEscaped(const std::string &line, std::size_t pos)
{
std::size_t slashCount = 0;
while (pos > slashCount && line[pos - slashCount - 1] == '\\')
++slashCount;
return (slashCount % 2) == 1;
}
bool endsWithUnescapedPipe(const std::string &line)
{
if (line.empty() || line[line.size() - 1] != '|')
return false;
return !isEscaped(line, line.size() - 1);
}
bool endsWithUnescapedPipeTrimmed(const std::string &line)
{
std::size_t last = line.size();
while (last > 0 && isSpace(static_cast<unsigned char>(line[last - 1])))
--last;
return last > 0 && line[last - 1] == '|' && !isEscaped(line, last - 1);
}
bool startsWithUnescapedPipe(const std::string &line)
{
std::size_t pos = 0;
while (pos < line.size() && isSpace(static_cast<unsigned char>(line[pos])))
++pos;
return pos < line.size() && line[pos] == '|' && !isEscaped(line, pos);
}
std::vector<std::string> splitCells(const std::string &line)
{
std::size_t first = 0;
while (first < line.size() && isSpace(static_cast<unsigned char>(line[first])))
++first;
std::size_t last = line.size();
while (last > first && isSpace(static_cast<unsigned char>(line[last - 1])))
--last;
if (first < last && line[first] == '|')
++first;
if (last > first && line[last - 1] == '|' && !isEscaped(line, last - 1))
--last;
std::vector<std::string> cells;
cells.reserve(8);
std::size_t start = first;
for (std::size_t i = first; i < last; ++i)
{
if (line[i] == '|' && !isEscaped(line, i))
{
cells.push_back(trimRange(line, start, i));
start = i + 1;
}
}
cells.push_back(trimRange(line, start, last));
return cells;
}
bool isSeparatorCell(const std::string &cell)
{
const std::string value = trim(cell);
if (value.empty())
return false;
bool hasDash = false;
for (std::size_t i = 0; i < value.size(); ++i)
{
const char ch = value[i];
if (ch == '-')
{
hasDash = true;
continue;
}
if (ch == ':' || isSpace(static_cast<unsigned char>(ch)))
continue;
return false;
}
return hasDash;
}
bool isSeparatorRow(const Row &row)
{
if (row.cells.empty())
return false;
for (std::size_t i = 0; i < row.cells.size(); ++i)
{
if (!isSeparatorCell(row.cells[i]))
return false;
}
return true;
}
bool isShortSeparatorLine(const std::string &line)
{
const std::string value = trim(line);
if (value.empty() || value[0] != '|')
return false;
bool hasRule = false;
for (std::size_t i = 1; i < value.size(); ++i)
{
const char ch = value[i];
if (ch == '-' || ch == '=')
hasRule = true;
if (ch != '-' && ch != '=' && ch != '|' && ch != ':' && !isSpace(static_cast<unsigned char>(ch)))
return false;
}
return hasRule;
}
Align parseAlignment(const std::string &cell)
{
const std::string value = trim(cell);
const bool left = !value.empty() && value[0] == ':';
const bool right = !value.empty() && value[value.size() - 1] == ':';
if (left && right)
return Align::Center;
if (left)
return Align::Left;
if (right)
return Align::Right;
return Align::None;
}
std::size_t tableRangeEnd(const std::vector<std::string> &lines, std::size_t firstRow, std::size_t separatorRow)
{
const bool requireLeadingPipe = startsWithUnescapedPipe(lines[firstRow]) && startsWithUnescapedPipe(lines[separatorRow]);
std::size_t lastRow = separatorRow;
for (std::size_t row = separatorRow + 1; row < lines.size(); ++row)
{
if (!isPotentialTableLine(lines[row]))
break;
if (requireLeadingPipe && !startsWithUnescapedPipe(lines[row]))
break;
lastRow = row;
}
return lastRow;
}
Table parseTable(const std::vector<std::string> &lines, std::size_t firstRow, std::size_t lastRow)
{
Table table;
if (lines.empty() || firstRow >= lines.size() || lastRow < firstRow)
return table;
if (lastRow >= lines.size())
lastRow = lines.size() - 1;
const std::size_t rowCount = lastRow - firstRow + 1;
table.rows.reserve(rowCount);
std::size_t leadingPipeRows = 0;
std::size_t trailingPipeRows = 0;
for (std::size_t rowOffset = 0; rowOffset < rowCount; ++rowOffset)
{
const std::string &line = lines[firstRow + rowOffset];
Row row;
row.id = rowOffset;
row.cells = splitCells(line);
table.columns = (std::max)(table.columns, row.cells.size());
table.rows.push_back(std::move(row));
if (startsWithUnescapedPipe(line))
++leadingPipeRows;
if (endsWithUnescapedPipeTrimmed(line))
++trailingPipeRows;
}
if (rowCount > 0)
{
table.leadingPipe = leadingPipeRows * 2 >= rowCount;
table.trailingPipe = trailingPipeRows * 2 >= rowCount;
}
for (std::size_t i = 0; i < table.rows.size(); ++i)
{
if (isSeparatorRow(table.rows[i]) || (i == 1 && isShortSeparatorLine(lines[firstRow + i])))
{
table.separatorRow = i;
table.rows[i].separator = true;
break;
}
}
if (table.columns == 0)
table.columns = 1;
for (std::size_t i = 0; i < table.rows.size(); ++i)
{
while (table.rows[i].cells.size() < table.columns)
table.rows[i].cells.push_back(std::string());
}
table.alignments.assign(table.columns, Align::None);
if (table.separatorRow != static_cast<std::size_t>(-1))
{
const Row &separator = table.rows[table.separatorRow];
for (std::size_t i = 0; i < table.columns; ++i)
table.alignments[i] = parseAlignment(separator.cells[i]);
}
return table;
}
bool isMarkdownTable(const Table &table)
{
return table.separatorRow != static_cast<std::size_t>(-1) && table.separatorRow > 0;
}
bool isUtf8Continuation(unsigned char ch);
unsigned int readUtf8CodePoint(const std::string &value, std::size_t &offset);
bool isWideCodePoint(unsigned int cp)
{
return cp == 0x2329 || cp == 0x232A ||
(cp >= 0x1100 && cp <= 0x115F) ||
(cp >= 0x2E80 && cp <= 0xA4CF && cp != 0x303F) ||
(cp >= 0xAC00 && cp <= 0xD7A3) ||
(cp >= 0xF900 && cp <= 0xFAFF) ||
(cp >= 0xFE10 && cp <= 0xFE19) ||
(cp >= 0xFE30 && cp <= 0xFE6F) ||
(cp >= 0xFF00 && cp <= 0xFF60) ||
(cp >= 0xFFE0 && cp <= 0xFFE6) ||
(cp >= 0x20000 && cp <= 0x3FFFD);
}
bool isEmojiPresentationCodePoint(unsigned int cp)
{
return (cp >= 0x1F000 && cp <= 0x1FAFF) ||
(cp >= 0x231A && cp <= 0x231B) ||
(cp >= 0x23E9 && cp <= 0x23EC) ||
cp == 0x23F0 || cp == 0x23F3 ||
(cp >= 0x25FD && cp <= 0x25FE) ||
(cp >= 0x2614 && cp <= 0x2615) ||
(cp >= 0x2648 && cp <= 0x2653) ||
cp == 0x267F || cp == 0x2693 || cp == 0x26A1 ||
(cp >= 0x26AA && cp <= 0x26AB) ||
(cp >= 0x26BD && cp <= 0x26BE) ||
(cp >= 0x26C4 && cp <= 0x26C5) ||
cp == 0x26CE || cp == 0x26D4 || cp == 0x26EA ||
(cp >= 0x26F2 && cp <= 0x26F3) ||
cp == 0x26F5 || cp == 0x26FA || cp == 0x26FD ||
cp == 0x2705 || (cp >= 0x270A && cp <= 0x270B) ||
cp == 0x2728 || cp == 0x274C || cp == 0x274E ||
(cp >= 0x2753 && cp <= 0x2755) ||
cp == 0x2757 || (cp >= 0x2795 && cp <= 0x2797) ||
cp == 0x27B0 || cp == 0x27BF ||
(cp >= 0x2B1B && cp <= 0x2B1C) ||
cp == 0x2B50 || cp == 0x2B55 ||
cp == 0x3030 || cp == 0x303D || cp == 0x3297 || cp == 0x3299;
}
bool isEmojiVariationBase(unsigned int cp)
{
return isEmojiPresentationCodePoint(cp) ||
cp == 0x00A9 || cp == 0x00AE || cp == 0x203C || cp == 0x2049 ||
cp == 0x2122 || cp == 0x2139 ||
(cp >= 0x2194 && cp <= 0x2199) ||
(cp >= 0x21A9 && cp <= 0x21AA) ||
cp == 0x2328 || cp == 0x23CF ||
(cp >= 0x23ED && cp <= 0x23EF) ||
(cp >= 0x23F1 && cp <= 0x23F2) ||
(cp >= 0x23F8 && cp <= 0x23FA) ||
cp == 0x24C2 || (cp >= 0x25AA && cp <= 0x25AB) ||
cp == 0x25B6 || cp == 0x25C0 ||
(cp >= 0x25FB && cp <= 0x25FE) ||
(cp >= 0x2600 && cp <= 0x27BF) ||
(cp >= 0x2934 && cp <= 0x2935) ||
(cp >= 0x2B05 && cp <= 0x2B07) ||
(cp >= 0x2B1B && cp <= 0x2B1C) ||
cp == 0x2B50 || cp == 0x2B55 ||
cp == 0x3030 || cp == 0x303D || cp == 0x3297 || cp == 0x3299;
}
bool isVariationSelector(unsigned int cp)
{
return (cp >= 0xFE00 && cp <= 0xFE0F) || (cp >= 0xE0100 && cp <= 0xE01EF);
}
bool isEmojiModifier(unsigned int cp)
{
return cp >= 0x1F3FB && cp <= 0x1F3FF;
}
bool isEmojiTag(unsigned int cp)
{
return cp >= 0xE0020 && cp <= 0xE007F;
}
bool isRegionalIndicator(unsigned int cp)
{
return cp >= 0x1F1E6 && cp <= 0x1F1FF;
}
bool isKeycapBase(unsigned int cp)
{
return cp == '#' || cp == '*' || (cp >= '0' && cp <= '9');
}
bool inCodePointRanges(unsigned int cp, const CodePointRange *ranges, std::size_t count)
{
for (std::size_t i = 0; i < count; ++i)
{
if (cp < ranges[i].first)
return false;
if (cp <= ranges[i].last)
return true;
}
return false;
}
bool isCombiningCodePoint(unsigned int cp)
{
static const CodePointRange ranges[] =
{
{0x0300, 0x036F}, {0x0483, 0x0489}, {0x0591, 0x05BD}, {0x05BF, 0x05BF},
{0x05C1, 0x05C2}, {0x05C4, 0x05C5}, {0x05C7, 0x05C7}, {0x0610, 0x061A},
{0x064B, 0x065F}, {0x0670, 0x0670}, {0x06D6, 0x06DC}, {0x06DF, 0x06E4},
{0x06E7, 0x06E8}, {0x06EA, 0x06ED}, {0x0711, 0x0711}, {0x0730, 0x074A},
{0x07A6, 0x07B0}, {0x07EB, 0x07F3}, {0x0816, 0x0819}, {0x081B, 0x0823},
{0x0825, 0x0827}, {0x0829, 0x082D}, {0x0859, 0x085B}, {0x08D3, 0x08E1},
{0x08E3, 0x0903}, {0x093A, 0x093C}, {0x093E, 0x094F}, {0x0951, 0x0957},
{0x0962, 0x0963}, {0x0981, 0x0983}, {0x09BC, 0x09BC}, {0x09BE, 0x09C4},
{0x09C7, 0x09C8}, {0x09CB, 0x09CD}, {0x09D7, 0x09D7}, {0x09E2, 0x09E3},
{0x0A01, 0x0A03}, {0x0A3C, 0x0A3C}, {0x0A3E, 0x0A42}, {0x0A47, 0x0A48},
{0x0A4B, 0x0A4D}, {0x0A51, 0x0A51}, {0x0A70, 0x0A71}, {0x0A75, 0x0A75},
{0x0A81, 0x0A83}, {0x0ABC, 0x0ABC}, {0x0ABE, 0x0AC5}, {0x0AC7, 0x0AC9},
{0x0ACB, 0x0ACD}, {0x0AE2, 0x0AE3}, {0x0B01, 0x0B03}, {0x0B3C, 0x0B3C},
{0x0B3E, 0x0B44}, {0x0B47, 0x0B48}, {0x0B4B, 0x0B4D}, {0x0B56, 0x0B57},
{0x0B62, 0x0B63}, {0x0B82, 0x0B82}, {0x0BBE, 0x0BC2}, {0x0BC6, 0x0BC8},
{0x0BCA, 0x0BCD}, {0x0BD7, 0x0BD7}, {0x0C00, 0x0C04}, {0x0C3E, 0x0C44},
{0x0C46, 0x0C48}, {0x0C4A, 0x0C4D}, {0x0C55, 0x0C56}, {0x0C62, 0x0C63},
{0x0C81, 0x0C83}, {0x0CBC, 0x0CBC}, {0x0CBE, 0x0CC4}, {0x0CC6, 0x0CC8},
{0x0CCA, 0x0CCD}, {0x0CD5, 0x0CD6}, {0x0CE2, 0x0CE3}, {0x0D00, 0x0D03},
{0x0D3B, 0x0D3C}, {0x0D3E, 0x0D44}, {0x0D46, 0x0D48}, {0x0D4A, 0x0D4D},
{0x0D57, 0x0D57}, {0x0D62, 0x0D63}, {0x0D82, 0x0D83}, {0x0DCA, 0x0DCA},
{0x0DCF, 0x0DD4}, {0x0DD6, 0x0DD6}, {0x0DD8, 0x0DDF}, {0x0DF2, 0x0DF3},
{0x0E31, 0x0E31}, {0x0E34, 0x0E3A}, {0x0E47, 0x0E4E}, {0x0EB1, 0x0EB1},
{0x0EB4, 0x0EBC}, {0x0EC8, 0x0ECD}, {0x0F18, 0x0F19}, {0x0F35, 0x0F35},
{0x0F37, 0x0F37}, {0x0F39, 0x0F39}, {0x0F3E, 0x0F3F}, {0x0F71, 0x0F84},
{0x0F86, 0x0F87}, {0x0F8D, 0x0F97}, {0x0F99, 0x0FBC}, {0x0FC6, 0x0FC6},
{0x102B, 0x103E}, {0x1056, 0x1059}, {0x105E, 0x1060}, {0x1062, 0x1064},
{0x1067, 0x106D}, {0x1071, 0x1074}, {0x1082, 0x108D}, {0x108F, 0x108F},
{0x109A, 0x109D}, {0x135D, 0x135F}, {0x1712, 0x1715}, {0x1732, 0x1734},
{0x1752, 0x1753}, {0x1772, 0x1773}, {0x17B4, 0x17D3}, {0x17DD, 0x17DD},
{0x180B, 0x180D}, {0x1885, 0x1886}, {0x18A9, 0x18A9}, {0x1920, 0x192B},
{0x1930, 0x193B}, {0x1A17, 0x1A1B}, {0x1A55, 0x1A5E}, {0x1A60, 0x1A7C},
{0x1A7F, 0x1A7F}, {0x1AB0, 0x1ACE}, {0x1B00, 0x1B04}, {0x1B34, 0x1B44},
{0x1B6B, 0x1B73}, {0x1B80, 0x1B82}, {0x1BA1, 0x1BAD}, {0x1BE6, 0x1BF3},
{0x1C24, 0x1C37}, {0x1CD0, 0x1CD2}, {0x1CD4, 0x1CE8}, {0x1CED, 0x1CED},
{0x1CF4, 0x1CF4}, {0x1CF7, 0x1CF9}, {0x1DC0, 0x1DFF}, {0x20D0, 0x20FF},
{0x2CEF, 0x2CF1}, {0x2D7F, 0x2D7F}, {0x2DE0, 0x2DFF}, {0x302A, 0x302F},
{0x3099, 0x309A}, {0xA66F, 0xA672}, {0xA674, 0xA67D}, {0xA69E, 0xA69F},
{0xA6F0, 0xA6F1}, {0xA802, 0xA802}, {0xA806, 0xA806}, {0xA80B, 0xA80B},
{0xA823, 0xA827}, {0xA880, 0xA881}, {0xA8B4, 0xA8C5}, {0xA8E0, 0xA8F1},
{0xA926, 0xA92D}, {0xA947, 0xA953}, {0xA980, 0xA983}, {0xA9B3, 0xA9C0},
{0xA9E5, 0xA9E5}, {0xAA29, 0xAA36}, {0xAA43, 0xAA43}, {0xAA4C, 0xAA4D},
{0xAA7B, 0xAA7D}, {0xAAB0, 0xAAB0}, {0xAAB2, 0xAAB4}, {0xAAB7, 0xAAB8},
{0xAABE, 0xAABF}, {0xAAC1, 0xAAC1}, {0xAAEB, 0xAAEF}, {0xAAF5, 0xAAF6},
{0xABE3, 0xABEA}, {0xABEC, 0xABED}, {0xFB1E, 0xFB1E}, {0xFE00, 0xFE0F},
{0xFE20, 0xFE2F}, {0x101FD, 0x101FD}, {0x102E0, 0x102E0}, {0x10376, 0x1037A},
{0x10A01, 0x10A03}, {0x10A05, 0x10A06}, {0x10A0C, 0x10A0F}, {0x10A38, 0x10A3A},
{0x10A3F, 0x10A3F}, {0x10AE5, 0x10AE6}, {0x10D24, 0x10D27}, {0x10EAB, 0x10EAC},
{0x10F46, 0x10F50}, {0x11000, 0x11002}, {0x11038, 0x11046}, {0x11070, 0x11070},
{0x11073, 0x11074}, {0x1107F, 0x11082}, {0x110B0, 0x110BA}, {0x11100, 0x11102},
{0x11127, 0x11134}, {0x11145, 0x11146}, {0x11173, 0x11173}, {0x11180, 0x11182},
{0x111B3, 0x111C0}, {0x111C9, 0x111CC}, {0x1122C, 0x11237}, {0x1123E, 0x1123E},
{0x112DF, 0x112EA}, {0x11300, 0x11303}, {0x1133B, 0x1133C}, {0x1133E, 0x11344},
{0x11347, 0x11348}, {0x1134B, 0x1134D}, {0x11357, 0x11357}, {0x11362, 0x11363},
{0x11366, 0x1136C}, {0x11370, 0x11374}, {0x11435, 0x11446}, {0x1145E, 0x1145E},
{0x114B0, 0x114C3}, {0x115AF, 0x115B5}, {0x115B8, 0x115C0}, {0x11630, 0x11640},
{0x116AB, 0x116B7}, {0x1171D, 0x1172B}, {0x1182C, 0x1183A}, {0x11930, 0x11935},
{0x11937, 0x11938}, {0x1193B, 0x1193E}, {0x11940, 0x11940}, {0x11942, 0x11943},
{0x119D1, 0x119D7}, {0x119DA, 0x119E0}, {0x119E4, 0x119E4}, {0x11A01, 0x11A0A},
{0x11A33, 0x11A39}, {0x11A3B, 0x11A3E}, {0x11A47, 0x11A47}, {0x11A51, 0x11A5B},
{0x11A8A, 0x11A99}, {0x11C2F, 0x11C36}, {0x11C38, 0x11C3F}, {0x11C92, 0x11CA7},
{0x11CA9, 0x11CB6}, {0x11D31, 0x11D36}, {0x11D3A, 0x11D3A}, {0x11D3C, 0x11D3D},
{0x11D3F, 0x11D45}, {0x11D47, 0x11D47}, {0x11D8A, 0x11D8E}, {0x11D90, 0x11D91},
{0x11D93, 0x11D97}, {0x11EF3, 0x11EF6}, {0x16AF0, 0x16AF4}, {0x16B30, 0x16B36},
{0x16F4F, 0x16F4F}, {0x16F51, 0x16F87}, {0x16F8F, 0x16F92}, {0x16FE4, 0x16FE4},
{0x1BC9D, 0x1BC9E}, {0x1D165, 0x1D169}, {0x1D16D, 0x1D172}, {0x1D17B, 0x1D182},
{0x1D185, 0x1D18B}, {0x1D1AA, 0x1D1AD}, {0x1D242, 0x1D244}, {0x1DA00, 0x1DA36},
{0x1DA3B, 0x1DA6C}, {0x1DA75, 0x1DA75}, {0x1DA84, 0x1DA84}, {0x1DA9B, 0x1DA9F},
{0x1DAA1, 0x1DAAF}, {0x1E000, 0x1E006}, {0x1E008, 0x1E018}, {0x1E01B, 0x1E021},
{0x1E023, 0x1E024}, {0x1E026, 0x1E02A}, {0x1E130, 0x1E136}, {0x1E2EC, 0x1E2EF},
{0x1E8D0, 0x1E8D6}, {0x1E944, 0x1E94A}, {0xE0100, 0xE01EF}
};
return inCodePointRanges(cp, ranges, sizeof(ranges) / sizeof(ranges[0]));
}
bool isFormatCodePoint(unsigned int cp)
{
return cp == 0x00AD || cp == 0x061C || cp == 0x180E ||
(cp >= 0x200B && cp <= 0x200F) ||
(cp >= 0x202A && cp <= 0x202E) ||
(cp >= 0x2060 && cp <= 0x206F) ||
cp == 0xFEFF ||
(cp >= 0xFFF9 && cp <= 0xFFFB) ||
cp == 0x110BD || cp == 0x110CD ||
(cp >= 0xE0000 && cp <= 0xE007F);
}
bool isZeroWidthCodePoint(unsigned int cp)
{
return cp == 0 ||
cp == 0x200C || cp == 0x200D ||
cp == 0x20E3 ||
isFormatCodePoint(cp) ||
isCombiningCodePoint(cp) ||
isVariationSelector(cp) ||
isEmojiModifier(cp) ||
isEmojiTag(cp);
}
std::size_t codePointDisplayWidth(unsigned int cp)
{
if (isZeroWidthCodePoint(cp))
return 0;
if (cp < 0x1100)
return 1;
return isWideCodePoint(cp) || isEmojiPresentationCodePoint(cp) ? 2 : 1;
}
std::size_t skipClusterModifiers(const std::string &text, std::size_t offset, unsigned int baseCodePoint, std::size_t &clusterWidth)
{
while (offset < text.size())
{
std::size_t nextOffset = offset;
const unsigned int cp = readUtf8CodePoint(text, nextOffset);
if (!isVariationSelector(cp) && !isCombiningCodePoint(cp) && !isEmojiModifier(cp) && !isEmojiTag(cp))
break;
if (cp == 0xFE0F && isEmojiVariationBase(baseCodePoint))
clusterWidth = (std::max)(clusterWidth, static_cast<std::size_t>(2));
offset = nextOffset;
}
return offset;
}
std::size_t skipKeycapCluster(const std::string &text, std::size_t offset)
{
std::size_t next = offset;
while (next < text.size())
{
std::size_t after = next;
const unsigned int cp = readUtf8CodePoint(text, after);
if (!isVariationSelector(cp))
break;
next = after;
}
if (next < text.size())
{
std::size_t after = next;
const unsigned int cp = readUtf8CodePoint(text, after);
if (cp == 0x20E3)
return after;
}
return static_cast<std::size_t>(-1);
}
std::size_t displayWidth(const std::string &text)
{
std::size_t width = 0;
for (std::size_t offset = 0; offset < text.size();)
{
const unsigned int cp = readUtf8CodePoint(text, offset);
if (isZeroWidthCodePoint(cp))
continue;
if (isKeycapBase(cp))
{
const std::size_t keycapEnd = skipKeycapCluster(text, offset);
if (keycapEnd != static_cast<std::size_t>(-1))
{
width += 2;
offset = keycapEnd;
continue;
}
}
if (isRegionalIndicator(cp) && offset < text.size())
{
std::size_t nextOffset = offset;
const unsigned int next = readUtf8CodePoint(text, nextOffset);
if (isRegionalIndicator(next))
{
width += 2;
offset = nextOffset;
continue;
}
}
std::size_t clusterWidth = codePointDisplayWidth(cp);
offset = skipClusterModifiers(text, offset, cp, clusterWidth);
bool joined = false;
while (offset < text.size())
{
std::size_t joinerOffset = offset;
const unsigned int joiner = readUtf8CodePoint(text, joinerOffset);
if (joiner != 0x200D)
break;
joined = true;
offset = joinerOffset;
if (offset >= text.size())
break;
const unsigned int joinedCp = readUtf8CodePoint(text, offset);
clusterWidth = (std::max)(clusterWidth, codePointDisplayWidth(joinedCp));
offset = skipClusterModifiers(text, offset, joinedCp, clusterWidth);
}
width += joined && clusterWidth > 0 ? (std::max)(clusterWidth, static_cast<std::size_t>(2)) : clusterWidth;
}
return width;
}
std::size_t nextUtf8Offset(const std::string &text, std::size_t offset)
{
readUtf8CodePoint(text, offset);
return offset;
}
std::size_t nextDisplayClusterOffset(const std::string &text, std::size_t offset)
{
const unsigned int baseCodePoint = readUtf8CodePoint(text, offset);
if (isKeycapBase(baseCodePoint))
{
const std::size_t keycapEnd = skipKeycapCluster(text, offset);
if (keycapEnd != static_cast<std::size_t>(-1))
return keycapEnd;
}
if (isRegionalIndicator(baseCodePoint) && offset < text.size())
{
std::size_t nextOffset = offset;
const unsigned int nextCodePoint = readUtf8CodePoint(text, nextOffset);
if (isRegionalIndicator(nextCodePoint))
return nextOffset;
}
std::size_t ignoredWidth = codePointDisplayWidth(baseCodePoint);
offset = skipClusterModifiers(text, offset, baseCodePoint, ignoredWidth);
while (offset < text.size())
{
std::size_t joinerOffset = offset;
if (readUtf8CodePoint(text, joinerOffset) != 0x200D)
break;
offset = joinerOffset;
if (offset >= text.size())
break;
const unsigned int joinedCodePoint = readUtf8CodePoint(text, offset);
offset = skipClusterModifiers(text, offset, joinedCodePoint, ignoredWidth);
}
return offset;
}
bool startsMarkdownLinkAt(const std::string &text, std::size_t offset)
{
if (offset >= text.size() || text[offset] != '[')
return false;
std::size_t closeBracket = offset + 1;
while (closeBracket < text.size())
{
if (text[closeBracket] == ']' && !isEscaped(text, closeBracket))
break;
closeBracket = nextUtf8Offset(text, closeBracket);
}
return closeBracket + 1 < text.size() && text[closeBracket + 1] == '(';
}
std::size_t markdownLinkEnd(const std::string &text, std::size_t offset)
{
std::size_t closeBracket = offset + 1;
while (closeBracket < text.size())
{
if (text[closeBracket] == ']' && !isEscaped(text, closeBracket))
break;
closeBracket = nextUtf8Offset(text, closeBracket);
}
if (closeBracket + 1 >= text.size() || text[closeBracket + 1] != '(')
return offset + 1;
std::size_t pos = closeBracket + 2;
int depth = 1;
while (pos < text.size())
{
if (text[pos] == '(' && !isEscaped(text, pos))
++depth;
else if (text[pos] == ')' && !isEscaped(text, pos))
{
--depth;
if (depth == 0)
return pos + 1;
}
pos = nextUtf8Offset(text, pos);
}
return offset + 1;
}
std::size_t markdownCodeSpanEnd(const std::string &text, std::size_t offset)
{
if (offset >= text.size() || text[offset] != '`')
return offset + 1;
std::size_t tickCount = 0;
while (offset + tickCount < text.size() && text[offset + tickCount] == '`')
++tickCount;
std::size_t pos = offset + tickCount;
while (pos < text.size())
{
if (text[pos] == '`')
{
std::size_t closingTicks = 0;
while (pos + closingTicks < text.size() && text[pos + closingTicks] == '`')
++closingTicks;
if (closingTicks == tickCount)
return pos + closingTicks;
pos += closingTicks;
}
else
{
pos = nextUtf8Offset(text, pos);
}
}
return offset + tickCount;
}
std::size_t longTokenChunkEnd(const std::string &token, std::size_t offset, std::size_t width)
{
const std::size_t targetWidth = (std::max)(static_cast<std::size_t>(1), width);
std::size_t end = offset;
std::size_t chunkWidth = 0;
while (end < token.size())
{
const std::size_t before = end;
const std::size_t after = nextDisplayClusterOffset(token, before);
const std::size_t clusterWidth = displayWidth(token.substr(before, after - before));
if (before > offset && chunkWidth + clusterWidth > targetWidth)
return before;
chunkWidth += clusterWidth;
end = after;
if (chunkWidth >= targetWidth)
return end;
}
return end > offset ? end : nextDisplayClusterOffset(token, offset);
}
void appendWrappedToken(std::vector<std::string> &segments, std::string ¤t, const std::string &token, std::size_t width)
{
const std::size_t tokenWidth = displayWidth(token);
const std::size_t currentWidth = displayWidth(current);
const std::size_t candidateWidth = current.empty() ? tokenWidth : currentWidth + 1 + tokenWidth;
if (tokenWidth <= width)
{
if (!current.empty() && candidateWidth > width)
{
segments.push_back(current);
current.clear();
}
if (!current.empty())
current.push_back(' ');
current += token;
return;
}
if (!current.empty())
{
segments.push_back(current);
current.clear();
}
std::size_t offset = 0;
while (offset < token.size())
{
const std::size_t end = longTokenChunkEnd(token, offset, width);
const std::string chunk = token.substr(offset, end - offset);
if (end < token.size())
{
segments.push_back(chunk);
}
else
{
current = chunk;
}
offset = end;
}
}
std::vector<std::string> wrapCellSegments(const std::string &cell, std::size_t width)
{
const std::string value = trim(cell);
if (value.empty())
return { "" };
std::vector<std::string> segments;
std::string current;
std::size_t offset = 0;
while (offset < value.size())
{
while (offset < value.size() && isSpace(static_cast<unsigned char>(value[offset])))
++offset;
if (offset >= value.size())
break;
std::size_t end = offset;
if (value[offset] == '`')
{
end = markdownCodeSpanEnd(value, offset);
}
else if (startsMarkdownLinkAt(value, offset))
{
end = markdownLinkEnd(value, offset);
}
else
{
while (end < value.size() && !isSpace(static_cast<unsigned char>(value[end])))
end = nextUtf8Offset(value, end);
}
appendWrappedToken(segments, current, value.substr(offset, end - offset), width);
offset = end;
}
if (!current.empty())
segments.push_back(current);
if (segments.empty())
segments.push_back("");
return segments;
}
std::size_t wrapLongCells(Table &table, std::size_t originalTargetRow)
{
if (table.separatorRow == static_cast<std::size_t>(-1))
return originalTargetRow;
std::vector<Row> wrappedRows;
wrappedRows.reserve(table.rows.size());
std::size_t wrappedTargetRow = originalTargetRow;
std::size_t nextId = nextRowId(table);
for (std::size_t rowIndex = 0; rowIndex < table.rows.size(); ++rowIndex)
{
const Row &row = table.rows[rowIndex];
if (row.separator || rowIndex <= table.separatorRow)
{
if (rowIndex == originalTargetRow)
wrappedTargetRow = wrappedRows.size();
wrappedRows.push_back(row);
continue;
}
std::vector<std::vector<std::string>> cellSegments;
cellSegments.reserve(table.columns);
std::size_t segmentCount = 1;
for (std::size_t column = 0; column < table.columns; ++column)
{
cellSegments.push_back(wrapCellSegments(row.cells[column], hardWrapCellWidth));
segmentCount = (std::max)(segmentCount, cellSegments.back().size());
}
if (rowIndex == originalTargetRow)
wrappedTargetRow = wrappedRows.size();
for (std::size_t segmentIndex = 0; segmentIndex < segmentCount; ++segmentIndex)
{
Row wrapped;
wrapped.id = segmentIndex == 0 ? row.id : nextId++;
wrapped.cells.reserve(table.columns);
for (std::size_t column = 0; column < table.columns; ++column)
{
const std::vector<std::string> &segments = cellSegments[column];
wrapped.cells.push_back(segmentIndex < segments.size() ? segments[segmentIndex] : "");
}
wrappedRows.push_back(std::move(wrapped));
}
}
table.rows = std::move(wrappedRows);
return wrappedTargetRow;
}
std::vector<std::size_t> naturalColumnWidths(const Table &table)
{
std::vector<std::size_t> widths(table.columns, 3);
for (std::size_t rowIndex = 0; rowIndex < table.rows.size(); ++rowIndex)
{
const Row &row = table.rows[rowIndex];
if (row.separator)
continue;
for (std::size_t column = 0; column < table.columns; ++column)
widths[column] = (std::max)(widths[column], displayWidth(row.cells[column]));
}
return widths;
}
std::vector<std::size_t> currentColumnWidths(const Table &table)
{
std::vector<std::size_t> widths(table.columns, 1);
for (std::size_t rowIndex = 0; rowIndex < table.rows.size(); ++rowIndex)
{
const Row &row = table.rows[rowIndex];
for (std::size_t column = 0; column < table.columns && column < row.cells.size(); ++column)
widths[column] = (std::max)(widths[column], displayWidth(row.cells[column]));
}
return widths;
}
std::vector<std::size_t> headerColumnWidths(const Table &table)
{
std::vector<std::size_t> widths(table.columns, 3);
const std::size_t headerEnd = table.separatorRow == static_cast<std::size_t>(-1)
? table.rows.size()
: table.separatorRow;
for (std::size_t rowIndex = 0; rowIndex < headerEnd && rowIndex < table.rows.size(); ++rowIndex)
{
const Row &row = table.rows[rowIndex];
for (std::size_t column = 0; column < table.columns; ++column)
widths[column] = (std::max)(widths[column], displayWidth(row.cells[column]));
}
return widths;
}
std::size_t formattedTableOverhead(const Table &table)
{
std::size_t overhead = table.leadingPipe ? 1 : 0;
for (std::size_t column = 0; column < table.columns; ++column)
{
if (column > 0)
overhead += 2;
if (table.leadingPipe || column > 0)
++overhead;
if (table.trailingPipe && column + 1 == table.columns)
overhead += 2;
}
return overhead;
}
std::size_t widthSum(const std::vector<std::size_t> &widths)
{
std::size_t sum = 0;
for (std::size_t i = 0; i < widths.size(); ++i)
sum += widths[i];
return sum;
}
bool containsSpace(const std::string &value)
{
for (std::size_t i = 0; i < value.size(); ++i)
{
if (isSpace(static_cast<unsigned char>(value[i])))
return true;
}
return false;
}
std::vector<bool> wrappableColumns(const Table &table, const std::vector<std::size_t> &headerWidths)
{
std::vector<bool> result(table.columns, false);
for (std::size_t rowIndex = 0; rowIndex < table.rows.size(); ++rowIndex)
{
const Row &row = table.rows[rowIndex];
if (row.separator || rowIndex < table.separatorRow)
continue;
for (std::size_t column = 0; column < table.columns; ++column)
{
const std::size_t width = displayWidth(row.cells[column]);
const std::size_t minimum = column < headerWidths.size() ? headerWidths[column] : static_cast<std::size_t>(3);
if (width > minimum && containsSpace(row.cells[column]))
result[column] = true;
}
}
return result;
}
std::size_t reductionToSlackCap(
const std::vector<std::size_t> &widths,
const std::vector<std::size_t> &minimums,
const std::vector<bool> &allowed,
std::size_t slackCap)
{
std::size_t reduction = 0;
for (std::size_t column = 0; column < widths.size(); ++column)
{
if (!allowed[column])
continue;
const std::size_t slack = widths[column] > minimums[column] ? widths[column] - minimums[column] : 0;
if (slack > slackCap)
reduction += slack - slackCap;
}
return reduction;