This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move EU::ParseXS::CountLines to its own file to simplify editing
[perl5.git] / dist / ExtUtils-ParseXS / lib / ExtUtils / ParseXS / Utilities.pm
CommitLineData
a65c06db
S
1package ExtUtils::ParseXS::Utilities;
2use strict;
3use warnings;
4use Exporter;
f3aadd09 5use File::Spec;
a65c06db
S
6our (@ISA, @EXPORT_OK);
7@ISA = qw(Exporter);
8@EXPORT_OK = qw(
9 standard_typemap_locations
1d40e528 10 trim_whitespace
73e91d5a 11 tidy_type
a65c06db
S
12);
13
f3aadd09
S
14=head1 NAME
15
16ExtUtils::ParseXS::Utilities - Subroutines used with ExtUtils::ParseXS
17
18=head1 SYNOPSIS
19
20 use ExtUtils::ParseXS::Utilities qw(
21 standard_typemap_locations
1d40e528 22 trim_whitespace
73e91d5a 23 tidy_type
f3aadd09
S
24 );
25
26=head1 SUBROUTINES
27
28The following functions are not considered to be part of the public interface.
29They are documented here for the benefit of future maintainers of this module.
30
31=head2 C<standard_typemap_locations()>
32
33=over 4
34
35=item * Purpose
36
37Provide a list of filepaths where F<typemap> files may be found. The
38filepaths -- relative paths to files (not just directory paths) -- appear in this list in lowest-to-highest priority.
39
40The highest priority is to look in the current directory.
41
42 'typemap'
43
44The second and third highest priorities are to look in the parent of the
45current directory and a directory called F<lib/ExtUtils> underneath the parent
46directory.
47
48 '../typemap',
49 '../lib/ExtUtils/typemap',
50
51The fourth through ninth highest priorities are to look in the corresponding
52grandparent, great-grandparent and great-great-grandparent directories.
53
54 '../../typemap',
55 '../../lib/ExtUtils/typemap',
56 '../../../typemap',
57 '../../../lib/ExtUtils/typemap',
58 '../../../../typemap',
59 '../../../../lib/ExtUtils/typemap',
60
61The tenth and subsequent priorities are to look in directories named
62F<ExtUtils> which are subdirectories of directories found in C<@INC> --
63I<provided> a file named F<typemap> actually exists in such a directory.
64Example:
65
66 '/usr/local/lib/perl5/5.10.1/ExtUtils/typemap',
67
68However, these filepaths appear in the list returned by
69C<standard_typemap_locations()> in reverse order, I<i.e.>, lowest-to-highest.
70
71 '/usr/local/lib/perl5/5.10.1/ExtUtils/typemap',
72 '../../../../lib/ExtUtils/typemap',
73 '../../../../typemap',
74 '../../../lib/ExtUtils/typemap',
75 '../../../typemap',
76 '../../lib/ExtUtils/typemap',
77 '../../typemap',
78 '../lib/ExtUtils/typemap',
79 '../typemap',
80 'typemap'
81
82=item * Arguments
83
84 my @stl = standard_typemap_locations( \@INC );
85
86Reference to C<@INC>.
87
88=item * Return Value
89
90Array holding list of directories to be searched for F<typemap> files.
91
92=back
93
94=cut
95
a65c06db
S
96sub standard_typemap_locations {
97 my $include_ref = shift;
a65c06db
S
98 my @tm = qw(typemap);
99
f3aadd09
S
100 my $updir = File::Spec->updir();
101 foreach my $dir (
102 File::Spec->catdir(($updir) x 1),
103 File::Spec->catdir(($updir) x 2),
104 File::Spec->catdir(($updir) x 3),
105 File::Spec->catdir(($updir) x 4),
106 ) {
a65c06db
S
107 unshift @tm, File::Spec->catfile($dir, 'typemap');
108 unshift @tm, File::Spec->catfile($dir, lib => ExtUtils => 'typemap');
109 }
110 foreach my $dir (@{ $include_ref}) {
111 my $file = File::Spec->catfile($dir, ExtUtils => 'typemap');
112 unshift @tm, $file if -e $file;
113 }
114 return @tm;
115}
116
1d40e528
JK
117=head2 C<trim_whitespace()>
118
119=over 4
120
121=item * Purpose
122
123Perform an in-place trimming of leading and trailing whitespace from the
124first argument provided to the function.
125
126=item * Argument
127
128 trim_whitespace($arg);
129
130=item * Return Value
131
132None. Remember: this is an I<in-place> modification of the argument.
133
134=back
135
136=cut
137
138sub trim_whitespace {
139 $_[0] =~ s/^\s+|\s+$//go;
140}
141
73e91d5a
JK
142=head2 C<tidy_type()>
143
144=over 4
145
146=item * Purpose
147
148Rationalize any asterisks (C<*>) by joining them into bunches, removing
149interior whitespace, then trimming leading and trailing whitespace.
150
151=item * Arguments
152
153 ($ret_type) = tidy_type($_);
154
155String to be cleaned up.
156
157=item * Return Value
158
159String cleaned up.
160
161=back
162
163=cut
164
165sub tidy_type {
166 local ($_) = @_;
167
168 # rationalise any '*' by joining them into bunches and removing whitespace
169 s#\s*(\*+)\s*#$1#g;
170 s#(\*+)# $1 #g;
171
172 # change multiple whitespace into a single space
173 s/\s+/ /g;
174
175 # trim leading & trailing whitespace
176 trim_whitespace($_);
177
178 $_;
179}
180
a65c06db 1811;