| … | |
… | |
| 392 | # $string - string to decode |
392 | # $string - string to decode |
| 393 | sub decode($) { |
393 | sub decode($) { |
| 394 | my $string = shift; |
394 | my $string = shift; |
| 395 | return unless defined $string and $string; |
395 | return unless defined $string and $string; |
| 396 | my %hash = $string =~ /(.*?)(?<!\\)=(.*?)(?:(?<!\\)&|$)/g; |
396 | my %hash = $string =~ /(.*?)(?<!\\)=(.*?)(?:(?<!\\)&|$)/g; |
| 397 | $hash{$_} =~ s/\\(.)/$1/ foreach (keys %hash); # unescape anything |
397 | $hash{$_} =~ s/\\(.)/$1/g foreach (keys %hash); # unescape anything |
| 398 | return %hash; |
398 | return %hash; |
| 399 | } |
399 | } |
| 400 | |
400 | |
| 401 | # encode(%hash) - encodes a hash as a quasi-URL-encoded string for insertion |
401 | # encode(%hash) - encodes a hash as a quasi-URL-encoded string for insertion |
| 402 | # into a hash-based webwork database. Escapes & and = in |
402 | # into a hash-based webwork database. Escapes & and = in |
| … | |
… | |
| 405 | sub encode(%) { |
405 | sub encode(%) { |
| 406 | my %hash = @_; |
406 | my %hash = @_; |
| 407 | my $string; |
407 | my $string; |
| 408 | foreach (keys %hash) { |
408 | foreach (keys %hash) { |
| 409 | $hash{$_} = "" unless defined $hash{$_}; # promote undef to "" |
409 | $hash{$_} = "" unless defined $hash{$_}; # promote undef to "" |
| 410 | $hash{$_} =~ s/(=|&)/\\$1/; # escape & and = |
410 | $hash{$_} =~ s/(=|&)/\\$1/g; # escape & and = |
| 411 | $string .= "$_=$hash{$_}&"; |
411 | $string .= "$_=$hash{$_}&"; |
| 412 | } |
412 | } |
| 413 | chop $string if $string; # remove final '&' from string for old code :p |
413 | chop $string if $string; # remove final '&' from string for old code :p |
| 414 | return $string; |
414 | return $string; |
| 415 | } |
415 | } |