3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-23 12:29:27 +01:00
pbot/PBot/Utils/ValidateString.pm

39 lines
865 B
Perl
Raw Normal View History

2017-09-05 09:18:03 +02:00
package PBot::Utils::ValidateString;
2018-04-09 04:34:24 +02:00
use warnings;
use strict;
2017-09-05 09:18:03 +02:00
require Exporter;
our @ISA = qw/Exporter/;
our @EXPORT = qw/validate_string/;
2018-04-09 04:34:24 +02:00
use JSON;
2017-09-05 09:18:03 +02:00
sub validate_string {
my ($string, $max_length) = @_;
2018-04-09 04:34:24 +02:00
2017-09-05 09:18:03 +02:00
return $string if not defined $string or not length $string;
2018-04-09 04:34:24 +02:00
$max_length = 1024 * 8 if not defined $max_length;
eval {
my $h = decode_json($string);
foreach my $k (keys %$h) {
$h->{$k} = substr $h->{$k}, 0, $max_length unless $max_length <= 0;
}
$string = encode_json($h);
};
if ($@) {
# not a json string
$string = substr $string, 0, $max_length unless $max_length <= 0;
}
2017-11-15 00:30:05 +01:00
# $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;
2018-04-09 04:34:24 +02:00
2017-09-05 09:18:03 +02:00
return $string;
}
1;