3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-27 06:19:25 +01:00
pbot/PBot/Utils/ValidateString.pm
Pragmatic Software 85693f905a Convert code-factoids to use VM
Code-factoids can now use the compiler virtual machine. Any languages installed
in the VM are valid candidates for code-factoids!

Syntax: factadd keyword /code language code here
2017-09-10 19:53:29 -07:00

19 lines
607 B
Perl

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 = 2000 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;