line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::ExpandEscapes; |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# String::ExpandEscapes - Expand printf-style %-escapes in a string. |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Copyright (c) 2003 Matthias Friedrich . |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# $Id: ExpandEscapes.pm,v 1.1.1.1 2003/04/03 19:10:10 matthias Exp $ |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
26650
|
use 5.008; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
51
|
|
11
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
12
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
38
|
|
13
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
596
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require Exporter; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
# Change version number for each release! |
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# |
25
|
|
|
|
|
|
|
# This allows declaration use String::ExpandEscapes ':all'; |
26
|
|
|
|
|
|
|
# |
27
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
28
|
|
|
|
|
|
|
'all' => [ qw(expand expand_handler) ] |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Export nothing by default. |
34
|
|
|
|
|
|
|
# |
35
|
|
|
|
|
|
|
our @EXPORT = qw( |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
# This is the default substitution function. |
41
|
|
|
|
|
|
|
# |
42
|
|
|
|
|
|
|
sub expand_handler($$$$$) |
43
|
|
|
|
|
|
|
{ |
44
|
0
|
|
|
0
|
0
|
|
my $flags = shift; # left or right alignment |
45
|
0
|
|
|
|
|
|
my $width = shift; # maximum field width |
46
|
0
|
|
|
|
|
|
my $precision = shift; # precision |
47
|
0
|
|
|
|
|
|
my $format = shift; # key for the conversation table |
48
|
0
|
|
|
|
|
|
my $table = shift; # conversation table (hash reference) |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
|
$precision = ".$precision" if $precision ne ''; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# rewrite '%%' to '%' |
53
|
0
|
0
|
|
|
|
|
return '%' if $format eq '%'; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# check if we have a rewrite rule for this format specifier |
56
|
0
|
0
|
|
|
|
|
return undef unless defined $table->{ $format }; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# everything worked fine, now return the string |
59
|
0
|
|
|
|
|
|
return sprintf("%${flags}${width}${precision}s", $table->{ $format } ); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# |
64
|
|
|
|
|
|
|
# expand() |
65
|
|
|
|
|
|
|
# |
66
|
|
|
|
|
|
|
# Expand printf-style escape sequences in a string according to a |
67
|
|
|
|
|
|
|
# given conversation table. |
68
|
|
|
|
|
|
|
# |
69
|
|
|
|
|
|
|
# Arguments: source string |
70
|
|
|
|
|
|
|
# hash reference to the conversation table or code ref |
71
|
|
|
|
|
|
|
# optional user data that is passed to each handler call |
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# Returns: destination string |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
# Examples: expand('%s', \&handler, ...) |
76
|
|
|
|
|
|
|
# expand('%s', \%table) |
77
|
|
|
|
|
|
|
# |
78
|
|
|
|
|
|
|
sub expand($$@) |
79
|
|
|
|
|
|
|
{ |
80
|
0
|
|
|
0
|
1
|
|
my $str = shift; # The string containing the escape sequences. |
81
|
0
|
|
|
|
|
|
my $arg = shift; # First argument: either HASH or CODE. |
82
|
0
|
|
|
|
|
|
my $handler; # Code reference for a handler function. |
83
|
|
|
|
|
|
|
my @user_data; # Arguments that are passed to the handler. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# |
86
|
|
|
|
|
|
|
# two possibilities: |
87
|
|
|
|
|
|
|
# 1. handler is code reference |
88
|
|
|
|
|
|
|
# 2. handler is hash reference |
89
|
|
|
|
|
|
|
# |
90
|
0
|
0
|
|
|
|
|
if ( ref $arg eq 'CODE' ) { |
|
|
0
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
$handler = $arg; |
92
|
0
|
|
|
|
|
|
@user_data = @_; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
elsif ( ref $arg eq 'HASH' ) { |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# If called with a hash reference, no further arguments are |
97
|
|
|
|
|
|
|
# allowed |
98
|
|
|
|
|
|
|
# |
99
|
0
|
0
|
|
|
|
|
if ( @_ != 0 ) { |
100
|
0
|
|
|
|
|
|
croak 'expand called in table mode with ' |
101
|
|
|
|
|
|
|
. 'to many arguments'; |
102
|
|
|
|
|
|
|
} |
103
|
0
|
|
|
|
|
|
$handler = \&expand_handler; |
104
|
0
|
|
|
|
|
|
@user_data = $arg; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
0
|
|
|
|
|
|
croak 'expand called with an argument that is ' |
108
|
|
|
|
|
|
|
. 'neither a code nor a hash reference'; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# |
112
|
|
|
|
|
|
|
# Parse the format string. The code below is executed for each match. |
113
|
|
|
|
|
|
|
# |
114
|
0
|
|
|
|
|
|
$str =~ s/ % |
115
|
|
|
|
|
|
|
([- +0#]*) # flags |
116
|
|
|
|
|
|
|
(\d*) # minimum field width aka width |
117
|
|
|
|
|
|
|
(\.?) |
118
|
|
|
|
|
|
|
(\d*) # maximum field width aka precision |
119
|
|
|
|
|
|
|
(.) # format selection |
120
|
|
|
|
|
|
|
/ |
121
|
|
|
|
|
|
|
# Special case: "%-10.s" means "%-10.0s". |
122
|
|
|
|
|
|
|
# |
123
|
0
|
0
|
0
|
|
|
|
my $prec = ( $3 eq '.' and $4 eq '' ) ? 0 : $4; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# The handler returns the string to substitute. |
126
|
|
|
|
|
|
|
# |
127
|
0
|
|
|
|
|
|
my $result = &$handler($1, $2, $prec, $5, @user_data); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# Leave the function if the handler returned an error. |
130
|
|
|
|
|
|
|
# |
131
|
0
|
0
|
|
|
|
|
return (undef, $&) unless defined $result; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$result; # Do the replacement. |
134
|
|
|
|
|
|
|
/gesx; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# |
137
|
|
|
|
|
|
|
# We got here without errors, return the string. |
138
|
|
|
|
|
|
|
# |
139
|
|
|
|
|
|
|
#return $str; |
140
|
0
|
|
|
|
|
|
return ($str, 0); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
__END__ |