| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #ifdef AMBIGUOUS_WIDTH_IS_WIDE |
| 7 |
|
| #include "charwidth_ambiguous_is_wide.h" |
| 8 |
|
| #else |
| 9 |
|
| #include "charwidth_default.h" |
| 10 |
|
| #endif |
| 11 |
|
| |
| 12 |
|
| |
| 13 |
209 |
| static int _char_width(UV codepoint) { |
| 14 |
209 |
▶ | if (codepoint < width_table[0].start) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepoint < width_table[0].start) { |
93 |
116 |
209 |
|
| 15 |
93 |
| return 1; |
| 16 |
|
| } |
| 17 |
|
| |
| 18 |
116 |
| int min = 0; |
| 19 |
116 |
| int max = width_table_len - 1; |
| 20 |
|
| int mid; |
| 21 |
|
| |
| 22 |
116 |
▶ | if (codepoint > width_table[max].end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepoint > width_table[max].end) { |
0 |
116 |
116 |
|
| 23 |
0 |
| return 1; |
| 24 |
|
| } |
| 25 |
|
| |
| 26 |
1071 |
▶ | while (max >= min) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (max >= min) { |
1071 |
0 |
1071 |
|
| 27 |
1071 |
| mid = (min + max) / 2; |
| 28 |
1071 |
▶ | if (codepoint > width_table[mid].end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepoint > width_table[mid].end) { |
589 |
482 |
1071 |
|
| 29 |
589 |
| min = mid + 1; |
| 30 |
482 |
▶ | } else if (codepoint < width_table[mid].start) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (codepoint < width_table[mid].start) { |
366 |
116 |
482 |
|
| 31 |
366 |
| max = mid - 1; |
| 32 |
|
| } else { |
| 33 |
116 |
| return width_table[mid].width; |
| 34 |
|
| } |
| 35 |
|
| } |
| 36 |
0 |
| return 1; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
|
| |
| 40 |
|
| MODULE = Term::Choose::LineFold::XS PACKAGE = Term::Choose::LineFold::XS |
| 41 |
|
| |
| 42 |
|
| PROTOTYPES: DISABLE |
| 43 |
|
| |
| 44 |
|
| |
| 45 |
|
| int |
| 46 |
|
| char_width(UV codepoint) |
| 47 |
|
| CODE: |
| 48 |
12 |
| RETVAL = _char_width(codepoint); |
| 49 |
|
| OUTPUT: |
| 50 |
|
| RETVAL |
| 51 |
|
| |
| 52 |
|
| |
| 53 |
|
| |
| 54 |
|
| SV * |
| 55 |
|
| print_columns(SV *input) |
| 56 |
|
| PREINIT: |
| 57 |
|
| STRLEN len; |
| 58 |
|
| const U8 *p, *end; |
| 59 |
|
| UV codepoint; |
| 60 |
12 |
▶ | int width = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int width = 0; |
0 |
12 |
12 |
|
| 61 |
|
| STRLEN clen; |
| 62 |
|
| CODE: |
| 63 |
12 |
▶ | if (!SvOK(input)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(input)) { |
0 |
12 |
12 |
|
| 64 |
0 |
| XSRETURN_UNDEF; |
| 65 |
|
| } |
| 66 |
12 |
▶ | if (!SvPOK(input)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvPOK(input)) { |
0 |
12 |
12 |
|
| 67 |
0 |
| input = sv_mortalcopy(input); // Ensure string |
| 68 |
|
| SvPV_force(input, len); |
| 69 |
|
| } |
| 70 |
12 |
| p = (const U8 *) SvPVutf8(input, len); |
| 71 |
12 |
| end = p + len; |
| 72 |
|
| |
| 73 |
80 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
68 |
12 |
80 |
|
| 74 |
68 |
| codepoint = utf8_to_uvchr_buf(p, end, &clen); |
| 75 |
68 |
▶ | if (clen == -1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (clen == -1 ) { |
0 |
68 |
68 |
|
| 76 |
0 |
| codepoint = *p; // Interpret the invalid byte as a single character |
| 77 |
0 |
| clen = 1; // Advance by 1 byte |
| 78 |
|
| } |
| 79 |
68 |
| p += clen; |
| 80 |
68 |
| width += _char_width(codepoint); |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
12 |
| RETVAL = newSViv(width); |
| 84 |
|
| OUTPUT: |
| 85 |
|
| RETVAL |
| 86 |
|
| |
| 87 |
|
| |
| 88 |
|
| |
| 89 |
|
| SV* |
| 90 |
|
| cut_to_printwidth(SV *input, int max_width) |
| 91 |
|
| PREINIT: |
| 92 |
|
| STRLEN len; |
| 93 |
|
| const U8 *p, *end; |
| 94 |
20 |
| const U8 *split_point = NULL; |
| 95 |
|
| const U8 *char_start; |
| 96 |
|
| UV codepoint; |
| 97 |
20 |
| int str_w = 0, this_w; |
| 98 |
|
| STRLEN clen; |
| 99 |
|
| // bool skip_padding; |
| 100 |
|
| PPCODE: |
| 101 |
20 |
▶ | if (!SvOK(input)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(input)) { |
0 |
20 |
20 |
|
| 102 |
0 |
| XSRETURN_UNDEF; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
20 |
▶ | if (!SvPOK(input)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvPOK(input)) { |
0 |
20 |
20 |
|
| 106 |
0 |
| input = sv_mortalcopy(input); |
| 107 |
|
| SvPV_force(input, len); |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
20 |
| const U8 *start = (const U8 *) SvPVutf8(input, len); |
| 111 |
20 |
| p = start; |
| 112 |
20 |
| end = p + len; |
| 113 |
|
| |
| 114 |
66 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
66 |
0 |
66 |
|
| 115 |
66 |
| char_start = p; |
| 116 |
66 |
| codepoint = utf8_to_uvchr_buf(p, end, &clen); |
| 117 |
|
| |
| 118 |
66 |
▶ | if (clen == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (clen == -1) { |
0 |
66 |
66 |
|
| 119 |
0 |
| codepoint = *p; |
| 120 |
0 |
| clen = 1; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
66 |
| this_w = _char_width(codepoint); |
| 124 |
|
| |
| 125 |
66 |
▶ | if (str_w + this_w > max_width) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_w + this_w > max_width) { |
20 |
46 |
66 |
|
| 126 |
20 |
| split_point = char_start; |
| 127 |
20 |
| break; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
46 |
| str_w += this_w; |
| 131 |
46 |
| p += clen; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
20 |
▶ | if (!split_point) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!split_point) { |
0 |
20 |
20 |
|
| 135 |
|
| // Whole string fits |
| 136 |
0 |
▶ | if (GIMME_V == G_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_ARRAY) { |
0 |
0 |
0 |
|
| 137 |
0 |
▶ | XPUSHs(sv_2mortal(newSVsv(input))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVsv(input))); |
0 |
0 |
0 |
|
| 138 |
0 |
▶ | XPUSHs(sv_2mortal(newSVpvn("", 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn("", 0))); |
0 |
0 |
0 |
|
| 139 |
|
| } else { |
| 140 |
0 |
▶ | XPUSHs(sv_2mortal(newSVsv(input))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVsv(input))); |
0 |
0 |
0 |
|
| 141 |
|
| } |
| 142 |
|
| } else { |
| 143 |
20 |
| STRLEN first_len = split_point - start; |
| 144 |
20 |
| SV *first_part = newSVpvn((const char *)start, first_len); |
| 145 |
20 |
| SvUTF8_on(first_part); |
| 146 |
|
| |
| 147 |
20 |
▶ | if (str_w == max_width - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_w == max_width - 1) { |
6 |
14 |
20 |
|
| 148 |
6 |
| sv_catpv(first_part, " "); |
| 149 |
6 |
| str_w += 1; |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
20 |
▶ | if (GIMME_V == G_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_ARRAY) { |
10 |
10 |
20 |
|
| 153 |
10 |
| STRLEN rest_len = end - split_point; |
| 154 |
10 |
| SV *rest_part = newSVpvn((const char *)split_point, rest_len); |
| 155 |
10 |
| SvUTF8_on(rest_part); |
| 156 |
|
| |
| 157 |
10 |
▶ | XPUSHs(sv_2mortal(first_part)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(first_part)); |
0 |
10 |
10 |
|
| 158 |
10 |
▶ | XPUSHs(sv_2mortal(rest_part)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(rest_part)); |
0 |
10 |
10 |
|
| 159 |
|
| } else { |
| 160 |
10 |
▶ | XPUSHs(sv_2mortal(first_part)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(first_part)); |
0 |
10 |
10 |
|
| 161 |
|
| } |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
|
| |
| 165 |
|
| |
| 166 |
|
| SV * |
| 167 |
|
| adjust_to_printwidth(SV *input, int width) |
| 168 |
|
| PREINIT: |
| 169 |
|
| STRLEN len; |
| 170 |
|
| const U8 *p, *end; |
| 171 |
|
| UV codepoint; |
| 172 |
17 |
| int str_w = 0, this_w; |
| 173 |
|
| STRLEN clen; |
| 174 |
|
| SV *result; |
| 175 |
|
| const U8 *start; |
| 176 |
|
| CODE: |
| 177 |
17 |
▶ | if (!SvOK(input)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(input)) { |
0 |
17 |
17 |
|
| 178 |
0 |
| XSRETURN_UNDEF; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
17 |
▶ | if (!SvPOK(input)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvPOK(input)) { |
0 |
17 |
17 |
|
| 182 |
0 |
| input = sv_mortalcopy(input); |
| 183 |
|
| SvPV_force(input, len); |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
17 |
| p = (const U8 *) SvPVutf8(input, len); // len is char length |
| 187 |
17 |
| end = p + len; |
| 188 |
17 |
| start = p; |
| 189 |
|
| |
| 190 |
70 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
63 |
7 |
70 |
|
| 191 |
63 |
| codepoint = utf8_to_uvchr_buf(p, end, &clen); |
| 192 |
63 |
▶ | if (clen == (STRLEN)-1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (clen == (STRLEN)-1) { |
0 |
63 |
63 |
|
| 193 |
0 |
| codepoint = *p; |
| 194 |
0 |
| clen = 1; |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
63 |
| this_w = _char_width(codepoint); |
| 198 |
63 |
▶ | if (str_w + this_w > width) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_w + this_w > width) { |
10 |
53 |
63 |
|
| 199 |
10 |
| break; |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
53 |
| str_w += this_w; |
| 203 |
53 |
| p += clen; |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
17 |
| len = (STRLEN)(p - start); // now len is byte length |
| 207 |
|
| |
| 208 |
17 |
▶ | if (str_w == width) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_w == width) { |
7 |
10 |
17 |
|
| 209 |
7 |
| RETVAL = newSVpvn((const char *)start, len); |
| 210 |
|
| } else { |
| 211 |
10 |
| result = newSVpvn((const char *)start, len); |
| 212 |
10 |
| sv_catpvf(result, "%*s", width - str_w, ""); |
| 213 |
10 |
| RETVAL = result; |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
17 |
| SvUTF8_on(RETVAL); |
| 217 |
|
| OUTPUT: |
| 218 |
|
| RETVAL |
| 219 |
|
| |