mirror of
https://git.proxmox.com/git/pve-common
synced 2025-08-14 05:55:40 +00:00
improve parse_config in JSONSchema and SectionConfig
The old code used string substitution for every line of the input string, while perl can also iterate over all matches via the /g re modifier, and can turn ^ and $ to act as beginning/end of line via the /m modifier. Effectively allowing a "match over all lines" via a simple while ($data =~ /^.*$/gm); The situation is a little different in SectionConfig because there's a nested loop invovled which doesn't work with /g. For this there are two options and I chose the safer one by simply doing a split on newlines first. The alternative would be to open the data as a filehandle-to-string and use <$fh> to read lines, however I'd have to throw in an eval{} to be sure to close the handle afterwards.
This commit is contained in:
parent
8998d715de
commit
3c4d612a70
@ -1150,13 +1150,12 @@ sub parse_config {
|
|||||||
|
|
||||||
my $cfg = {};
|
my $cfg = {};
|
||||||
|
|
||||||
while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
|
while ($raw =~ /^\s*(.+?)\s*$/gm) {
|
||||||
my $line = $1;
|
my $line = $1;
|
||||||
|
|
||||||
next if $line =~ m/^\#/; # skip comment lines
|
|
||||||
next if $line =~ m/^\s*$/; # skip empty lines
|
|
||||||
|
|
||||||
if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
|
next if $line =~ /^#/;
|
||||||
|
|
||||||
|
if ($line =~ m/^(\S+?):\s*(.*)$/) {
|
||||||
my $key = $1;
|
my $key = $1;
|
||||||
my $value = $2;
|
my $value = $2;
|
||||||
if ($schema->{properties}->{$key} &&
|
if ($schema->{properties}->{$key} &&
|
||||||
|
@ -231,19 +231,22 @@ sub parse_config {
|
|||||||
my $ids = {};
|
my $ids = {};
|
||||||
my $order = {};
|
my $order = {};
|
||||||
|
|
||||||
my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : '');
|
$raw = '' if !defined($raw);
|
||||||
|
|
||||||
|
my $digest = Digest::SHA::sha1_hex($raw);
|
||||||
|
|
||||||
my $pri = 1;
|
my $pri = 1;
|
||||||
|
|
||||||
my $lineno = 0;
|
my $lineno = 0;
|
||||||
|
my @lines = split(/\n/, $raw);
|
||||||
|
my $nextline = sub {
|
||||||
|
while (my $line = shift @lines) {
|
||||||
|
$lineno++;
|
||||||
|
return $line if $line !~ /^\s*(?:#|$)/;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
|
while (my $line = &$nextline()) {
|
||||||
my $line = $1;
|
|
||||||
$lineno++;
|
|
||||||
|
|
||||||
next if $line =~ m/^\#/;
|
|
||||||
next if $line =~ m/^\s*$/;
|
|
||||||
|
|
||||||
my $errprefix = "file $filename line $lineno";
|
my $errprefix = "file $filename line $lineno";
|
||||||
|
|
||||||
my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
|
my ($type, $sectionId, $errmsg, $config) = $class->parse_section_header($line);
|
||||||
@ -266,13 +269,7 @@ sub parse_config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
|
while ($line = &$nextline()) {
|
||||||
$line = $1;
|
|
||||||
$lineno++;
|
|
||||||
|
|
||||||
next if $line =~ m/^\#/;
|
|
||||||
last if $line =~ m/^\s*$/;
|
|
||||||
|
|
||||||
next if $ignore; # skip
|
next if $ignore; # skip
|
||||||
|
|
||||||
$errprefix = "file $filename line $lineno";
|
$errprefix = "file $filename line $lineno";
|
||||||
|
Loading…
Reference in New Issue
Block a user