in reply to patchable settings
I tried to increase readability of the stored string (which seemed important to demerphq) by only escaping the necessary characters &, =, and %. I did not consider using unprintable characters and am a little uneasy about not escaping any there are in the data; I'd prefer it to be always all printable. This also meant bypassing calls to escape/unescape which would slow things down with a lot of vars.
cmpVars is what we need for current patch detection to work reliably; I haven't tested it at all yet.
sub packVars { my $varsref = $_[0]; # current format version: 01 return join "&", "==01", map { my $typ; my $v = $varsref->{$_}; # special data to pack? if (ref $v) { # only hash refs supported now $typ = "H"; $v = packVars( $v ); } # undef becomes empty, protect empty values elsif (!$v) { $v = $typ = ''; } join '=', map {s/([%&=])/ sprintf '%%%02x', ord($1) /ge; $_} $_, $v, (defined $typ ? $typ : ()); } sort keys %$varsref; } sub unpackVars { my $vars_str = $_[0]; my $format_version = "00"; # version 00: original format # version 01: keys are escaped, not just values $format_version = $1 if $vars_str =~ s/^==(\d\d)&//; return {} unless $vars_str; my %vars; if ($format_version eq "01") { for (split /&/, $vars_str) { my ($k,$v,$typ) = map { s/%(\w\w)/ chr(hex($1)) /ge; $_ } split /=/, $_, + -1; # special data to unpack? if ($typ) { # nested hash if ($typ eq "H") { $v = unpackVars($v); } } $vars{$k} = $v; } } # format version "00" else { %vars = map split(/=/, $_, 2), split /&/, $vars_str; unescape( values %vars ); $vars{$_} eq ' ' and $vars{$_} = '' for keys %vars; } return \%vars; } sub cmpVars { my ($var1str, $var2str) = @_; # return false immediately if strings match, # otherwise return true if both are current format version, # otherwise compare current format version. return $var1str cmp $var2str && ( $var1str =~ /^==01&/ && $var2str =~ /^==01&/ ) || packVars( unpackVars ( $var1str ) ) cmp packVars( unpackVars ( $var2str ) ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: patchable settings (big readmores)
by demerphq (Chancellor) on Oct 16, 2004 at 10:58 UTC | |
by ysth (Canon) on Oct 17, 2004 at 10:45 UTC | |
by demerphq (Chancellor) on Oct 17, 2004 at 17:13 UTC | |
by tye (Sage) on Oct 16, 2004 at 16:48 UTC | |
by demerphq (Chancellor) on Oct 17, 2004 at 07:29 UTC | |
by demerphq (Chancellor) on Oct 17, 2004 at 17:17 UTC | |
by tye (Sage) on Oct 17, 2004 at 19:18 UTC | |
by demerphq (Chancellor) on Oct 24, 2004 at 11:33 UTC | |
| |
by demerphq (Chancellor) on Nov 01, 2004 at 23:21 UTC | |
by ysth (Canon) on Oct 17, 2004 at 02:38 UTC | |
by demerphq (Chancellor) on Oct 17, 2004 at 07:31 UTC |