From 474cca04b84e14ed89f1d01aec9fe5cc05433a11 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 1 Oct 2022 21:32:25 -0700 Subject: [PATCH] Plugin/WordMorph: simplify word validation --- lib/PBot/Plugin/WordMorph.pm | 58 +++++++++++++++++------------------- lib/PBot/VERSION.pm | 2 +- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/PBot/Plugin/WordMorph.pm b/lib/PBot/Plugin/WordMorph.pm index 56f7242f..00ad7e18 100644 --- a/lib/PBot/Plugin/WordMorph.pm +++ b/lib/PBot/Plugin/WordMorph.pm @@ -68,14 +68,8 @@ sub wordmorph { return DB_UNAVAILABLE if not $self->{db}; - my $length = length $args[0]; - - if ($length < MIN_WORD_LENGTH) { - return "`$args[0]` is too short; minimum word length is ".MIN_WORD_LENGTH."."; - } elsif ($length > MAX_WORD_LENGTH) { - return "`$args[0]` is too long, maximum word length is ".MAX_WORD_LENGTH."."; - } elsif (not exists $self->{db}->{$length}->{$args[0]}) { - return "I do not know this word `$args[0]`."; + if (my $err = $self->validate_word($args[0], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) { + return $err; } my @neighbors = @{$self->{db}->{length $args[0]}->{$args[0]}}; @@ -90,17 +84,11 @@ sub wordmorph { return DB_UNAVAILABLE if not $self->{db}; - my $length = length $args[0]; - - if ($length < MIN_WORD_LENGTH) { - return "`$args[0]` is too short; minimum word length is ".MIN_WORD_LENGTH."."; - } elsif ($length > MAX_WORD_LENGTH) { - return "`$args[0]` is too long, maximum word length is ".MAX_WORD_LENGTH."."; - } elsif (not exists $self->{db}->{$length}->{$args[0]}) { - return "I do not know this word `$args[0]`."; - } else { - return "Yes, `$args[0]` is a word I know."; + if (my $err = $self->validate_word($args[0], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) { + return $err; } + + return "Yes, `$args[0]` is a word I know."; } when ('hint') { @@ -238,20 +226,12 @@ sub wordmorph { return "Usage: wordmorph custom " if @args != 2; return DB_UNAVAILABLE if not $self->{db}; - my $length = length $args[0]; - - if ($length < MIN_WORD_LENGTH) { - return "`$args[0]` is too short; minimum word length is ".MIN_WORD_LENGTH."."; - } elsif ($length > MAX_WORD_LENGTH) { - return "`$args[0]` is too long, maximum word length is ".MAX_WORD_LENGTH."."; + if (my $err = $self->validate_word($args[0], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) { + return $err; } - $length = length $args[1]; - - if ($length < MIN_WORD_LENGTH) { - return "`$args[1]` is too short; minimum word length is ".MIN_WORD_LENGTH."."; - } elsif ($length > MAX_WORD_LENGTH) { - return "`$args[1]` is too long, maximum word length is ".MAX_WORD_LENGTH."."; + if (my $err = $self->validate_word($args[1], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) { + return $err; } my $morph = eval { makemorph($self->{db}, $args[0], $args[1]) } or return $@; @@ -369,6 +349,24 @@ sub form_hint { return $hint; } +sub validate_word { + my ($self, $word, $min, $max) = @_; + + my $len = length $word; + + if ($len < $min) { + return "`$word` is too short; minimum word length is $min."; + } elsif ($len > $max) { + return "`$word` is too long, maximum word length is $max."; + } + + if (not exists $self->{db}->{$len}->{$word}) { + return "I do not know this word `$word`."; + } + + return undef; +} + sub compare_suffix { my ($word1, $word2) = @_; diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 7fd86c61..019338ad 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,7 +25,7 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4588, + BUILD_REVISION => 4590, BUILD_DATE => "2022-10-01", };