summaryrefslogtreecommitdiff
path: root/lib/VNDB/Func.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2021-02-12 10:16:02 +0100
committerYorhel <git@yorhel.nl>2021-03-01 10:16:33 +0100
commitfc3721171f021807d1c8b23a5257fc1ac1809ea5 (patch)
treebcd63e797e26e17d73be02d90ea52b4f5c2da497 /lib/VNDB/Func.pm
parent86d0191251fc80205dce92369b9b661cb40a3707 (diff)
SQL: vndbid data type conversion for most DB entries
I had wanted to split this up into multiple commits and roll out in stages, but couldn't really find a natural way to do so. There are several places that take a generic identifier and expect it to work the same for all entries they support, so changing one entry at a time wasn't going to be any easier. Only the tags & traits haven't been updated yet, I'll convert those later. While this is a major change and affects a lot of code, the individual changes are all pretty simple. I'm surprised how much code did not have to be updated at all. No doubt I've missed a few places, though, so this commit will almost certainly break something.
Diffstat (limited to 'lib/VNDB/Func.pm')
-rw-r--r--lib/VNDB/Func.pm23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm
index 3211f346..9e051fae 100644
--- a/lib/VNDB/Func.pm
+++ b/lib/VNDB/Func.pm
@@ -12,6 +12,8 @@ use VNDB::Config;
use VNDB::Types;
use VNDB::BBCode;
our @EXPORT = ('bb_format', qw|
+ in
+ idcmp
shorten
resolution
gtintype
@@ -27,6 +29,27 @@ our @EXPORT = ('bb_format', qw|
|);
+# Simple "is this element in the array?" function, using 'eq' to test equality.
+# Supports both an @array and \@array.
+# Usage:
+#
+# my $contains_hi = in 'hi', qw/ a b hi c /; # true
+#
+sub in {
+ my($q, @a) = @_;
+ $_ eq $q && return 1 for map ref $_ eq 'ARRAY' ? @$_ : ($_), @a;
+ 0
+}
+
+
+# Compare two vndbids, using proper numeric order
+sub idcmp($$) {
+ my($a1, $a2) = $_[0] =~ /^([a-z]+)([0-9]+)$/;
+ my($b1, $b2) = $_[1] =~ /^([a-z]+)([0-9]+)$/;
+ $a1 cmp $b1 || $a2 <=> $b2
+}
+
+
sub shorten {
my($str, $len) = @_;
return length($str) > $len ? substr($str, 0, $len-3).'...' : $str;