summaryrefslogtreecommitdiffstats
path: root/lib/Cepis/CSV.pm
blob: 6fabc0ccbab5b22a53eef9a025a5c6a07eff7e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package Cepis::CSV;

use strict;
use warnings;

our $VERSION = '0.1.0';

sub parse_rows {
    my ($text) = @_;

    return [] unless defined $text && length $text;
    $text =~ s/\A\x{FEFF}//;

    my @rows;
    my @row;
    my $field   = '';
    my $quoted  = 0;
    my $started = 0;
    my $length  = length $text;
    my $i       = 0;

    while ( $i < $length ) {
        my $char = substr( $text, $i, 1 );

        if ($quoted) {
            if ( $char eq '"' ) {
                if ( $i + 1 < $length && substr( $text, $i + 1, 1 ) eq '"' ) {
                    $field .= '"';
                    $i += 2;
                    next;
                }
                $quoted = 0;
                $i++;
                next;
            }
            $field .= $char;
            $i++;
            next;
        }

        if ( $char eq '"' && $field eq '' ) {
            $quoted  = 1;
            $started = 1;
            $i++;
            next;
        }

        if ( $char eq ',' ) {
            push @row, $field;
            $field   = '';
            $started = 1;
            $i++;
            next;
        }

        if ( $char eq "\r" || $char eq "\n" ) {
            $i++;
            $i++
                if $char eq "\r"
                && $i < $length
                && substr( $text, $i, 1 ) eq "\n";
            if ( $started || @row ) {
                push @row, $field;
                push @rows, [@row];
            }
            @row     = ();
            $field   = '';
            $started = 0;
            next;
        }

        $field .= $char;
        $started = 1;
        $i++;
    }

    if ( $started || @row ) {
        push @row, $field;
        push @rows, [@row];
    }

    return \@rows;
}

sub parse_hashes {
    my ($text) = @_;

    my $rows = parse_rows($text);
    return ( [], [] ) unless @$rows;

    my $header = shift @$rows;
    my @out;
    for my $row (@$rows) {
        my %record;
        for my $index ( 0 .. $#$header ) {
            my $value = $index <= $#$row ? $row->[$index] : '';
            $record{ $header->[$index] } = defined $value ? $value : '';
        }
        push @out, \%record;
    }

    return ( $header, \@out );
}

1;

__END__

=encoding UTF-8

=head1 NAME

Cepis::CSV - a small RFC 4180 reader

=head1 SYNOPSIS

    use Cepis::CSV;

    my ( $header, $records ) = Cepis::CSV::parse_hashes($text);
    print $records->[0]{localidade};

=head1 DESCRIPTION

Perl ships no CSV parser in its core, and this distribution has no non-core
dependencies, so it carries its own. It handles what RFC 4180 requires and what
the C<GET /csv> endpoint actually emits: quoted fields, embedded commas, embedded
newlines, C<""> as an escaped quote, C<CRLF> or C<LF> line endings, and a leading
byte order mark.

The input is a character string, not bytes — decode the response body before
calling in.

This module is internal to L<Cepis>. It is documented because it is tested
separately, not because it is meant to be a general-purpose CSV library: it has
no writer, no configurable separator and no streaming interface.

=head1 FUNCTIONS

=head2 parse_rows

    my $rows = Cepis::CSV::parse_rows($text);

Returns an arrayref of arrayrefs, one per record, fields in order. Blank lines
are skipped, so a trailing newline does not produce a phantom final record.

=head2 parse_hashes

    my ( $header, $records ) = Cepis::CSV::parse_hashes($text);

Treats the first record as a header row and returns it as an arrayref, plus an
arrayref of hashrefs keyed by column name. A record with fewer fields than the
header gets empty strings for the ones it lacks; fields beyond the header are
dropped.

Returns two empty arrayrefs for empty input.

=head1 SEE ALSO

L<Cepis::Client>

=head1 LICENCE

ISC. Copyright (c) 2026 Murilo Ijanc E<lt>[email protected]<gt>.

=cut