3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 22:09:26 +01:00

Add ValidateString utilities

This commit is contained in:
Pragmatic Software 2017-09-05 00:18:03 -07:00
parent a805fe26e4
commit c3938ed8bb

View File

@ -0,0 +1,18 @@
package PBot::Utils::ValidateString;
use 5.010; use warnings;
require Exporter;
our @ISA = qw/Exporter/;
our @EXPORT = qw/validate_string/;
sub validate_string {
my ($string, $max_length) = @_;
return $string if not defined $string or not length $string;
$max_length = 400 if not defined $max_length;
$string = substr $string, 0, $max_length unless $max_length <= 0;
$string =~ s/(\P{PosixGraph})/my $ch = $1; if ($ch =~ m{[\s\x03\x02\x1d\x1f\x16\x0f]}) { $ch } else { sprintf "\\x%02X", ord $ch }/ge;
$string = substr $string, 0, $max_length unless $max_length <= 0;
return $string;
}
1;