From 0707750cd8d9d28a295cbf2c6111de7857b04684 Mon Sep 17 00:00:00 2001 From: siliconfeces <124937394+siliconfeces@users.noreply.github.com> Date: Tue, 23 Jul 2024 19:02:24 +0200 Subject: [PATCH] Avoid puking out warning when calling trim_crlf with undef --- lib/LANraragi/Utils/String.pm | 7 +++++-- tests/LANraragi/Utils/String.t | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/LANraragi/Utils/String.pm b/lib/LANraragi/Utils/String.pm index 5dd4675a2..acb1e5828 100644 --- a/lib/LANraragi/Utils/String.pm +++ b/lib/LANraragi/Utils/String.pm @@ -41,8 +41,11 @@ sub trim ($s) { # Remove all newlines in a string sub trim_CRLF ($s) { - $s =~ s/\R//g; - return $s; + if (defined($s)) { + $s =~ s/\R//g; + return $s; + } + return; } # Fixes up a URL string for use in the DL system. diff --git a/tests/LANraragi/Utils/String.t b/tests/LANraragi/Utils/String.t index e4a470ae7..580ac70b1 100644 --- a/tests/LANraragi/Utils/String.t +++ b/tests/LANraragi/Utils/String.t @@ -49,5 +49,11 @@ note('testing string similarity detection...'); is( LANraragi::Utils::String::most_similar( "orange", () ), undef, "Empty set" ); } +note('testing trim_crlf...'); +{ + is( LANraragi::Utils::String::trim_CRLF( undef ), undef, "Undef should return undef"); + is( LANraragi::Utils::String::trim_CRLF( "a\nb" ), "ab", "newline should go bye"); +} + done_testing();