DateTime-Format-Lite.xs

filter: Statementstmt 78.8% 41 / 52 Branchbran 55.5% 20 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.3% 61 / 88 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/DateTime/Format/Lite/Exception.pm ↑ Summary lib/DateTime/Format/Lite/PP.pm »
1 /*---------------------------------------------------------------------------
2 * DateTime::Format::Lite - DateTime-Format-Lite.xs
3 * Version v0.1.0
4 * Copyright(c) 2026 DEGUEST Pte. Ltd.
5 * Author: Jacques Deguest <jack@deguest.jp>
6 * Created 2026/04/14
7 * Modified 2026/04/16
8 *
9 * XS implementations of hot-path functions for DateTime::Format::Lite.
10 *
11 * _match_and_extract( self, regex, fields_aref, string )
12 * Executes the pre-compiled pattern regex against the input string and
13 * returns a hashref { field => captured_value, ... } on success, or
14 * undef on no match. Called on every parse_datetime(), so keeping it
15 * in XS avoids the per-field Perl allocation loop.
16 *
17 * format_datetime( self, dt )
18 * Calls DateTime::Lite->strftime( $pattern ) directly on the dt object
19 * without the clone() + set_locale() round-trip that Strptime 1.80
20 * performs. strftime() is already XS-accelerated in DateTime::Lite.
21 *
22 * This program is free software; you can redistribute it and/or modify it
23 * under the same terms as Perl itself.
24 *---------------------------------------------------------------------------*/
25 #define PERL_NO_GET_CONTEXT
26 #include "EXTERN.h"
27 #include "perl.h"
28 #define NO_XSLOCKS
29 #include "XSUB.h"
30 #include "ppport.h"
31  
32 /* RX_OFFS was removed in Perl 5.37.10 (first shipped in 5.38.0).
33 * RX_OFFSp(rx) returns a pointer to the offsets array from that version on.
34 * On older perls RX_OFFS(rx) returns the array directly.
35 * We normalise to a single macro RX_OFFS_PTR(rx) that always yields a pointer. */
36 #if PERL_VERSION_GE(5,37,10)
37 # define RX_OFFS_PTR(rx) RX_OFFSp(rx)
38 #else
39 # define RX_OFFS_PTR(rx) RX_OFFS(rx)
40 #endif
41  
42  
43 #include <stdlib.h>
44 #include <string.h>
45  
46 MODULE = DateTime::Format::Lite PACKAGE = DateTime::Format::Lite
47  
48 PROTOTYPES: ENABLE
49  
50 # ---------------------------------------------------------------------------
51 # _match_and_extract( self, regex, fields_aref, string )
52 #
53 # Executes the pre-compiled regex against the input string.
54 # Returns a hashref mapping each field name in fields_aref to its captured
55 # substring, or undef if the string does not match.
56 # ---------------------------------------------------------------------------
57 SV *
58 _match_and_extract(self, regex, fields_aref, string)
59 SV *self;
60 SV *regex;
61 AV *fields_aref;
62 SV *string;
63  
64 PREINIT:
65 REGEXP *rx;
66 char *str;
67 STRLEN str_len;
68 I32 num_fields;
69 I32 i;
70 HV *result;
71 SV **field_svp;
72  
73 CODE:
74 (void)self;
75  
76 /* Extract the compiled regexp from the regex SV */
77 209 rx = SvRX( regex );
78 209 ▶ if( !rx )
Branch: 1 branch
BranchTrueFalseTotal
if( !rx ) 0 209 209
79 {
80 0 warn( "_match_and_extract: argument is not a compiled regexp\n" );
81 0 RETVAL = &PL_sv_undef;
82 0 goto mae_done;
83 }
84  
85 209 str = SvPVutf8( string, str_len );
86 209 num_fields = (I32)( av_len( fields_aref ) + 1 );
87  
88 /* Run the regex against the string */
89 209 ▶ if( !pregexec( rx, str, str + str_len, str, 0, string, REXEC_COPY_STR ) )
Branch: 1 branch
BranchTrueFalseTotal
if( !pregexec( rx, str, str + str_len, str, 0, string, REXEC_COPY_STR ) ) 12 197 209
90 {
91 12 RETVAL = &PL_sv_undef;
92 12 goto mae_done;
93 }
94  
95 /* Build result hashref from capture groups keyed by field names */
96 197 result = (HV *)sv_2mortal( (SV *)newHV() );
97 874 ▶ for( i = 0; i < num_fields; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < num_fields; i++ ) 677 197 874
98 {
99 677 I32 cap_idx = i + 1; /* $1 = index 1, $2 = index 2, ... */
100  
101 677 field_svp = av_fetch( fields_aref, (SSize_t)i, 0 );
102 677 ▶ if( !field_svp || !SvOK( *field_svp ) )
Branch: 2 branches
BranchTrueFalseTotal
if( !field_svp || !SvOK( *field_svp ) ) 677 0 677
if( !field_svp || !SvOK( *field_svp ) ) 0 677 677
103 0 continue;
104  
105 677 ▶ if( cap_idx > (I32)RX_NPARENS( rx ) )
Branch: 1 branch
BranchTrueFalseTotal
if( cap_idx > (I32)RX_NPARENS( rx ) ) 0 677 677
106 0 continue;
107  
108 {
109 677 I32 start = RX_OFFS_PTR( rx )[ cap_idx ].start;
110 677 I32 end = RX_OFFS_PTR( rx )[ cap_idx ].end;
111  
112 677 ▶ if( start < 0 || end < start )
Branch: 2 branches
BranchTrueFalseTotal
if( start < 0 || end < start ) 677 0 677
if( start < 0 || end < start ) 0 677 677
113 0 continue;
114  
115 {
116 677 SV *val = newSVpvn( str + start, (STRLEN)( end - start ) );
117 677 ▶ if( SvUTF8( string ) )
Branch: 1 branch
BranchTrueFalseTotal
if( SvUTF8( string ) ) 677 0 677
118 677 SvUTF8_on( val );
119 /* hv_store_ent increments refcount of the key SV */
120 677 hv_store_ent( result, *field_svp, val, 0 );
121 }
122 }
123 }
124  
125 197 RETVAL = newRV( (SV *)result );
126  
127 209 mae_done: ;
128  
129 OUTPUT:
130 RETVAL
131  
132 # ---------------------------------------------------------------------------
133 # format_datetime( self, dt )
134 #
135 # Formats a DateTime::Lite object using $self->{pattern}, delegating to
136 # DateTime::Lite->strftime() without cloning the object first.
137 # Returns the formatted string, or undef if $self->{pattern} is missing.
138 # ---------------------------------------------------------------------------
139 SV *
140 format_datetime(self, dt)
141 SV *self;
142 SV *dt;
143  
144 PREINIT:
145 HV *self_hv;
146 SV **pattern_svp;
147 SV *result;
148 I32 count;
149  
150 CODE:
151 19 ▶ if( !SvROK( self ) || SvTYPE( SvRV( self ) ) != SVt_PVHV )
Branch: 2 branches
BranchTrueFalseTotal
if( !SvROK( self ) || SvTYPE( SvRV( self ) ) != SVt_PVHV ) 19 0 19
if( !SvROK( self ) || SvTYPE( SvRV( self ) ) != SVt_PVHV ) 0 19 19
152 {
153 0 warn( "format_datetime: self is not a blessed hashref\n" );
154 0 RETVAL = &PL_sv_undef;
155 0 goto fmt_done;
156 }
157  
158 19 self_hv = (HV *)SvRV( self );
159 19 pattern_svp = hv_fetchs( self_hv, "pattern", 0 );
160  
161 19 ▶ if( !pattern_svp || !SvOK( *pattern_svp ) )
Branch: 2 branches
BranchTrueFalseTotal
if( !pattern_svp || !SvOK( *pattern_svp ) ) 19 0 19
if( !pattern_svp || !SvOK( *pattern_svp ) ) 0 19 19
162 {
163 0 RETVAL = &PL_sv_undef;
164 0 goto fmt_done;
165 }
166  
167 /* Call $dt->strftime( $self->{pattern} ) */
168 {
169 19 dSP;
170 19 ENTER;
171 19 SAVETMPS;
172 19 ▶ PUSHMARK( SP );
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK( SP ); 0 19 19
173 19 ▶ XPUSHs( dt );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( dt ); 0 19 19
174 19 ▶ XPUSHs( *pattern_svp );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( *pattern_svp ); 0 19 19
175 19 PUTBACK;
176  
177 19 count = call_method( "strftime", G_SCALAR );
178 18 SPAGAIN;
179  
180 18 ▶ result = ( count > 0 ) ? SvREFCNT_inc( POPs ) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
result = ( count > 0 ) ? SvREFCNT_inc( POPs ) : &PL_sv_undef; 18 0 18
181 18 PUTBACK;
182 18 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 18 0 18
183 18 LEAVE;
184 }
185  
186 18 RETVAL = result;
187  
188 18 fmt_done: ;
189  
190 OUTPUT:
191 RETVAL
« lib/DateTime/Format/Lite/Exception.pm ↑ Summary lib/DateTime/Format/Lite/PP.pm »