| 1 |
|
| /*---------------------------------------------------------------------------- |
| 2 |
|
| * Person Name Format - PersonName-Format.xs |
| 3 |
|
| * Version v0.1.0 |
| 4 |
|
| * Copyright(c) 2026 DEGUEST Pte. Ltd. |
| 5 |
|
| * Author: Jacques Deguest <jack@deguest.jp> |
| 6 |
|
| * Created 2026/07/17 |
| 7 |
|
| * Modified 2026/07/19 |
| 8 |
|
| * All rights reserved |
| 9 |
|
| * |
| 10 |
|
| * XS implementations of key functions for PersonName-Format. |
| 11 |
|
| * |
| 12 |
|
| * This program is free software; you can redistribute it and/or modify it |
| 13 |
|
| * under the same terms as Perl itself. |
| 14 |
|
| *---------------------------------------------------------------------------- |
| 15 |
|
| */ |
| 16 |
|
| #include "EXTERN.h" |
| 17 |
|
| #include "perl.h" |
| 18 |
|
| #include "XSUB.h" |
| 19 |
|
| |
| 20 |
|
| /*------------------------------------------------------------------- |
| 21 |
|
| * pnf_first_grapheme( value ) |
| 22 |
|
| * |
| 23 |
|
| * Delegates to PersonName::Format::PP::_first_grapheme() to extract |
| 24 |
|
| * the first Unicode extended grapheme cluster from a Perl string SV. |
| 25 |
|
| * |
| 26 |
|
| * The pure-Perl implementation handles the two cases that \X does not |
| 27 |
|
| * cover correctly on older Perl versions: |
| 28 |
|
| * - Regional Indicator pairs (UAX #29 rule GB12/GB13, Perl < 5.18) |
| 29 |
|
| * - ZWJ emoji sequences (UAX #29 rule GB11, Perl < 5.28) |
| 30 |
|
| * |
| 31 |
|
| * Returns a new mortal SV containing the grapheme, or an empty string |
| 32 |
|
| * SV for an undefined or empty input. |
| 33 |
|
| *-------------------------------------------------------------------*/ |
| 34 |
|
| static SV* |
| 35 |
20 |
| pnf_first_grapheme(pTHX_ SV* value) |
| 36 |
|
| { |
| 37 |
20 |
| dSP; |
| 38 |
|
| SV* result; |
| 39 |
|
| |
| 40 |
20 |
▶ | if (!SvOK(value) || SvCUR(value) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(value) || SvCUR(value) == 0) |
18 |
2 |
20 |
| if (!SvOK(value) || SvCUR(value) == 0) |
2 |
16 |
18 |
|
| 41 |
|
| { |
| 42 |
4 |
| return newSVpvs(""); |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
16 |
| ENTER; |
| 46 |
16 |
| SAVETMPS; |
| 47 |
|
| |
| 48 |
16 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
16 |
16 |
|
| 49 |
16 |
▶ | XPUSHs(value); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(value); |
0 |
16 |
16 |
|
| 50 |
16 |
| PUTBACK; |
| 51 |
|
| |
| 52 |
16 |
▶ | if (call_pv("PersonName::Format::PP::_first_grapheme", G_SCALAR) != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (call_pv("PersonName::Format::PP::_first_grapheme", G_SCALAR) != 1) |
0 |
16 |
16 |
|
| 53 |
|
| { |
| 54 |
0 |
| croak("PersonName::Format::PP::_first_grapheme() did not return one value"); |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
16 |
| SPAGAIN; |
| 58 |
16 |
| result = newSVsv(POPs); |
| 59 |
16 |
| PUTBACK; |
| 60 |
|
| |
| 61 |
16 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
16 |
0 |
16 |
|
| 62 |
16 |
| LEAVE; |
| 63 |
|
| |
| 64 |
16 |
| return result; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| /*------------------------------------------------------------------- |
| 68 |
|
| * pnf_script_code_for_uv( uv ) |
| 69 |
|
| * |
| 70 |
|
| * Delegates to PersonName::Format::PP::_script_code_for_uv() to |
| 71 |
|
| * resolve a Unicode codepoint (UV) to its ISO 15924 four-letter |
| 72 |
|
| * script code (e.g. "Latn", "Hani", "Arab"). |
| 73 |
|
| * |
| 74 |
|
| * On Perl 5.16 and later the PP function uses |
| 75 |
|
| * Unicode::UCD::prop_value_aliases(); on earlier versions it falls |
| 76 |
|
| * back to the bundled ScriptNames.pl data file. |
| 77 |
|
| * |
| 78 |
|
| * Returns a new SV containing the four-letter code string, or "Zzzz" |
| 79 |
|
| * (Unknown) when no script can be determined. |
| 80 |
|
| *-------------------------------------------------------------------*/ |
| 81 |
|
| static SV* |
| 82 |
65 |
| pnf_script_code_for_uv(pTHX_ UV uv) |
| 83 |
|
| { |
| 84 |
65 |
| dSP; |
| 85 |
|
| SV* result; |
| 86 |
|
| |
| 87 |
65 |
| ENTER; |
| 88 |
65 |
| SAVETMPS; |
| 89 |
|
| |
| 90 |
65 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
65 |
65 |
|
| 91 |
65 |
▶ | XPUSHs(sv_2mortal(newSVuv(uv))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVuv(uv))); |
0 |
65 |
65 |
|
| 92 |
65 |
| PUTBACK; |
| 93 |
|
| |
| 94 |
65 |
▶ | if (call_pv("PersonName::Format::PP::_script_code_for_uv", G_SCALAR) != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (call_pv("PersonName::Format::PP::_script_code_for_uv", G_SCALAR) != 1) |
0 |
65 |
65 |
|
| 95 |
|
| { |
| 96 |
0 |
| croak("PersonName::Format::PP::_script_code_for_uv() did not return one value"); |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
65 |
| SPAGAIN; |
| 100 |
65 |
| result = newSVsv(POPs); |
| 101 |
65 |
| PUTBACK; |
| 102 |
|
| |
| 103 |
65 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
65 |
0 |
65 |
|
| 104 |
65 |
| LEAVE; |
| 105 |
|
| |
| 106 |
65 |
| return result; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
|
| /*------------------------------------------------------------------- |
| 110 |
|
| * pnf_first_significant_script( value ) |
| 111 |
|
| * |
| 112 |
|
| * Iterates the codepoints of a Perl string SV and returns a new SV |
| 113 |
|
| * holding the ISO 15924 code of the first script that is not Common |
| 114 |
|
| * (Zyyy), Inherited (Zinh), or Unknown (Zzzz). Returns NULL when no |
| 115 |
|
| * significant script is found (empty string, all ASCII punctuation, |
| 116 |
|
| * all digits, etc.). |
| 117 |
|
| * |
| 118 |
|
| * The SV is upgraded to its internal UTF-8 representation on a mortal |
| 119 |
|
| * copy before reading. This is required on Perl 5.10.1, where |
| 120 |
|
| * SvUTF8() may not be set for decoded Unicode strings in certain XS |
| 121 |
|
| * calling contexts, causing the byte-level iteration to see raw UTF-8 |
| 122 |
|
| * lead bytes (e.g. 0xE5 for U+5BAE хоо) rather than full codepoints, |
| 123 |
|
| * and thus to misidentify the script as Latin. |
| 124 |
|
| *-------------------------------------------------------------------*/ |
| 125 |
|
| static SV* |
| 126 |
57 |
| pnf_first_significant_script(pTHX_ SV* value) |
| 127 |
|
| { |
| 128 |
|
| STRLEN length; |
| 129 |
|
| const U8* current; |
| 130 |
|
| const U8* end; |
| 131 |
|
| SV* value_utf8; |
| 132 |
|
| |
| 133 |
57 |
▶ | if (!SvOK(value)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(value)) |
1 |
56 |
57 |
|
| 134 |
|
| { |
| 135 |
1 |
| return NULL; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /* Force the SV to its internal UTF-8 representation before reading bytes. |
| 139 |
|
| * On Perl 5.10.1, SvUTF8() may return 0 for decoded Unicode strings that |
| 140 |
|
| * happen to contain only Latin-1 codepoints, or in certain calling contexts. |
| 141 |
|
| * Working on a mortal copy avoids modifying a read-only or shared SV. */ |
| 142 |
56 |
| value_utf8 = sv_mortalcopy(value); |
| 143 |
56 |
| sv_utf8_upgrade(value_utf8); |
| 144 |
|
| |
| 145 |
56 |
| current = (const U8*)SvPV(value_utf8, length); |
| 146 |
56 |
▶ | if (length == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (length == 0) |
0 |
56 |
56 |
|
| 147 |
|
| { |
| 148 |
0 |
| return NULL; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
56 |
| end = current + length; |
| 152 |
|
| |
| 153 |
67 |
▶ | while (current < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (current < end) |
65 |
2 |
67 |
|
| 154 |
|
| { |
| 155 |
|
| UV uv; |
| 156 |
|
| STRLEN char_length; |
| 157 |
|
| SV* script; |
| 158 |
|
| const char* code; |
| 159 |
|
| STRLEN code_length; |
| 160 |
|
| |
| 161 |
|
| #if PERL_VERSION >= 16 |
| 162 |
65 |
| uv = utf8_to_uvchr_buf(current, end, &char_length); |
| 163 |
|
| #else |
| 164 |
|
| uv = utf8_to_uvchr(current, &char_length); |
| 165 |
|
| #endif |
| 166 |
65 |
▶ | if (char_length == 0 || current + char_length > end) |
Branch: 2 branches
| Branch | True | False | Total |
| if (char_length == 0 || current + char_length > end) |
65 |
0 |
65 |
| if (char_length == 0 || current + char_length > end) |
0 |
65 |
65 |
|
| 167 |
|
| { |
| 168 |
0 |
| croak("Invalid UTF-8 sequence in person-name field"); |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
65 |
| current += char_length; |
| 172 |
65 |
| script = pnf_script_code_for_uv(aTHX_ uv); |
| 173 |
65 |
| code = SvPV(script, code_length); |
| 174 |
|
| |
| 175 |
65 |
▶ | if (!((code_length == 4 && memEQ(code, "Zyyy", 4)) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (!((code_length == 4 && memEQ(code, "Zyyy", 4)) || |
65 |
0 |
65 |
| if (!((code_length == 4 && memEQ(code, "Zyyy", 4)) || |
55 |
10 |
65 |
|
| 176 |
55 |
▶ | (code_length == 4 && memEQ(code, "Zinh", 4)) || |
Branch: 2 branches
| Branch | True | False | Total |
| (code_length == 4 && memEQ(code, "Zinh", 4)) || |
55 |
0 |
55 |
| (code_length == 4 && memEQ(code, "Zinh", 4)) || |
54 |
1 |
55 |
|
| 177 |
54 |
▶ | (code_length == 4 && memEQ(code, "Zzzz", 4)))) |
Branch: 2 branches
| Branch | True | False | Total |
| (code_length == 4 && memEQ(code, "Zzzz", 4)))) |
54 |
0 |
54 |
| (code_length == 4 && memEQ(code, "Zzzz", 4)))) |
54 |
0 |
54 |
|
| 178 |
|
| { |
| 179 |
54 |
| return script; |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
11 |
| SvREFCNT_dec(script); |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
2 |
| return NULL; |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| MODULE = PersonName::Format PACKAGE = PersonName::Format |
| 189 |
|
| PROTOTYPES: DISABLE |
| 190 |
|
| |
| 191 |
|
| #------------------------------------------------------------------- |
| 192 |
|
| # _first_grapheme( value ) |
| 193 |
|
| # |
| 194 |
|
| # Returns the first Unicode extended grapheme cluster from the |
| 195 |
|
| # input string, or an empty string for an undefined or empty value. |
| 196 |
|
| #------------------------------------------------------------------- |
| 197 |
|
| SV* |
| 198 |
|
| _first_grapheme(value) |
| 199 |
|
| SV* value |
| 200 |
|
| |
| 201 |
|
| CODE: |
| 202 |
20 |
| RETVAL = pnf_first_grapheme(aTHX_ value); |
| 203 |
|
| |
| 204 |
|
| OUTPUT: |
| 205 |
|
| RETVAL |
| 206 |
|
| |
| 207 |
|
| #------------------------------------------------------------------- |
| 208 |
|
| # _get_name_script() |
| 209 |
|
| # |
| 210 |
|
| # Returns the ISO 15924 four-letter script code |
| 211 |
|
| # (e.g. "Hani", "Latn") of the first significant codepoint found |
| 212 |
|
| # in surname, then given. Common (Zyyy), Inherited (Zinh), and |
| 213 |
|
| # Unknown (Zzzz) codepoints are skipped. Returns "Zzzz" when no |
| 214 |
|
| # significant script is found in either field. |
| 215 |
|
| #------------------------------------------------------------------- |
| 216 |
|
| SV* |
| 217 |
|
| _get_name_script(surname, given) |
| 218 |
|
| SV* surname |
| 219 |
|
| SV* given |
| 220 |
|
| |
| 221 |
|
| PREINIT: |
| 222 |
|
| SV* script; |
| 223 |
|
| |
| 224 |
|
| CODE: |
| 225 |
55 |
| script = pnf_first_significant_script(aTHX_ surname); |
| 226 |
55 |
▶ | if( script == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( script == NULL ) |
2 |
53 |
55 |
|
| 227 |
|
| { |
| 228 |
2 |
| script = pnf_first_significant_script(aTHX_ given); |
| 229 |
|
| } |
| 230 |
55 |
▶ | if( script == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( script == NULL ) |
1 |
54 |
55 |
|
| 231 |
|
| { |
| 232 |
1 |
| RETVAL = newSVpvs("Zzzz"); |
| 233 |
|
| } |
| 234 |
|
| else |
| 235 |
|
| { |
| 236 |
54 |
| RETVAL = script; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
|
| OUTPUT: |
| 240 |
|
| RETVAL |