/Vector/Util.pm
package Vector::Util;
use Exporter 'import';
our @EXPORT_OK = qw/url_encode simplify_uri xmlescape/;
use strict;

sub url_encode {
	local $_ = shift;
	s/([^A-Za-z0-9_.-])/sprintf("%%%02X", ord($1))/seg;
	return $_;
}

sub simplify_uri {
	my ($uri) = @_;

	if ($uri =~ m'https?://(?:([a-zA-Z0-9-.]+)\.)?([a-zA-Z0-9-]+\.[a-z]{2,5})/([^/]+)?') {
		if ($1 && $3) {
			if ($1 eq 'id') {
				return "$3 [$2]";
			} else {
				return "$1.$2/$3";
			}
		} elsif ($1) {
			return "$1 [$2]";
		} elsif ($3) {
			return "$3 [$2]";
		} else {
			return $2;
		}
	} else {
		return $uri;
	}
}

sub linebreak {
	local $_ = shift;
	s/(\015\012|\012|\015)/<br>$1/g;
	return $_;
}

sub xmlescape {
	local $_ = shift;
	s/&/&amp;/g;
	s/</&lt;/g;
	s/>/&gt;/g;
	s/"/&quot;/g;

	s/([\x80-\x{FFFFFF}])/sprintf("&#x%x;", ord($1))/eg;

	return $_;
}

1;