diff options
-rw-r--r-- | lib/PublicInbox/RepoBrowseGit.pm | 29 | ||||
-rw-r--r-- | t/repobrowse_git.t | 11 |
2 files changed, 40 insertions, 0 deletions
diff --git a/lib/PublicInbox/RepoBrowseGit.pm b/lib/PublicInbox/RepoBrowseGit.pm new file mode 100644 index 00000000..73a236d3 --- /dev/null +++ b/lib/PublicInbox/RepoBrowseGit.pm @@ -0,0 +1,29 @@ +# Copyright (C) 2015 all contributors <meta@public-inbox.org> +# License: AGPL-3.0+ (https://www.gnu.org/licenses/agpl-3.0.txt) + +package PublicInbox::RepoBrowseGit; +use strict; +use warnings; +use base qw(Exporter); +our @EXPORT_OK = qw(git_unquote); + +my %GIT_ESC = ( + a => "\a", + b => "\b", + f => "\f", + n => "\n", + r => "\r", + t => "\t", + v => "\013", +); + +sub git_unquote { + my ($s) = @_; + return $s unless ($s =~ /\A"(.*)"\z/); + $s = $1; + $s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g; + $s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge; + $s; +} + +1; diff --git a/t/repobrowse_git.t b/t/repobrowse_git.t new file mode 100644 index 00000000..eeda90a3 --- /dev/null +++ b/t/repobrowse_git.t @@ -0,0 +1,11 @@ +# Copyright (C) 2015 all contributors <meta@public-inbox.org> +# License: AGPL-3.0+ (https://www.gnu.org/licenses/agpl-3.0.txt) +use strict; +use warnings; +use Test::More; +use PublicInbox::RepoBrowseGit qw(git_unquote); + +is("foo\nbar", git_unquote('"foo\\nbar"'), 'unquoted newline'); +is("Eléanor", git_unquote('"El\\303\\251anor"'), 'unquoted octal'); + +done_testing(); |