X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FUtil.pm;h=3355d41fa1d3454c119a14dbd98d900abef70083;hb=05e0bcef1c2165c556b910314312866dc4a667b7;hp=87d87d6c2022d13e0ed61620c6bf56881fbdb3af;hpb=eb5adf956fc6b9285b43ce6965eea728573e7864;p=chaz%2Fp5-File-KDBX diff --git a/lib/File/KDBX/Util.pm b/lib/File/KDBX/Util.pm index 87d87d6..3355d41 100644 --- a/lib/File/KDBX/Util.pm +++ b/lib/File/KDBX/Util.pm @@ -7,6 +7,7 @@ use strict; use Crypt::PRNG qw(random_bytes random_string); use Encode qw(decode encode); use Exporter qw(import); +use File::KDBX::Constants qw(:bool); use File::KDBX::Error; use List::Util 1.33 qw(any all); use Module::Load; @@ -18,7 +19,6 @@ our $VERSION = '999.999'; # VERSION our %EXPORT_TAGS = ( assert => [qw(assert_64bit)], - bool => [qw(FALSE TRUE)], clone => [qw(clone clone_nomagic)], crypt => [qw(pad_pkcs7)], debug => [qw(dumper)], @@ -29,7 +29,7 @@ our %EXPORT_TAGS = ( gzip => [qw(gzip gunzip)], io => [qw(is_readable is_writable read_all)], load => [qw(load_optional load_xs try_load_optional)], - search => [qw(query search simple_expression_query)], + search => [qw(query search search_limited simple_expression_query)], text => [qw(snakify trim)], uuid => [qw(format_uuid generate_uuid is_uuid uuid)], uri => [qw(split_url uri_escape_utf8 uri_unescape_utf8)], @@ -82,17 +82,6 @@ my %OP_NEG = ( '!~' => '=~', ); -=func FALSE - -=func TRUE - -Constants appropriate for use as return values in functions claiming to return true or false. - -=cut - -sub FALSE() { !1 } -sub TRUE() { 1 } - =func load_xs $bool = load_xs(); @@ -103,23 +92,23 @@ that at least the given version is loaded. =cut +my $XS_LOADED; sub load_xs { my $version = shift; - goto IS_LOADED if File::KDBX->can('_XS_LOADED'); - - my $try_xs = 1; - $try_xs = 0 if $ENV{PERL_ONLY} || (exists $ENV{PERL_FILE_KDBX_XS} && !$ENV{PERL_FILE_KDBX_XS}); + goto IS_LOADED if defined $XS_LOADED; - my $use_xs = 0; - $use_xs = eval { require File::KDBX::XS; 1 } if $try_xs; + if ($ENV{PERL_ONLY} || (exists $ENV{PERL_FILE_KDBX_XS} && !$ENV{PERL_FILE_KDBX_XS})) { + return $XS_LOADED = FALSE; + } - *File::KDBX::_XS_LOADED = *File::KDBX::_XS_LOADED = $use_xs ? \&TRUE : \&FALSE; + $XS_LOADED = !!eval { require File::KDBX::XS; 1 }; IS_LOADED: { local $@; - return $version ? !!eval { File::KDBX::XS->VERSION($version); 1 } : File::KDBX::_XS_LOADED(); + return $XS_LOADED if !$version; + return !!eval { File::KDBX::XS->VERSION($version); 1 }; } } @@ -286,13 +275,13 @@ Overwrite the memory used by one or more string. BEGIN { if (load_xs) { - # loaded CowREFCNT + *_CowREFCNT = \&File::KDBX::XS::CowREFCNT; } elsif (eval { require B::COW; 1 }) { - *CowREFCNT = \*B::COW::cowrefcnt; + *_CowREFCNT = \&B::COW::cowrefcnt; } else { - *CowREFCNT = sub { undef }; + *_CowREFCNT = sub { undef }; } } @@ -303,7 +292,7 @@ sub erase { for (@_) { if (!is_ref($_)) { next if !defined $_ || readonly $_; - my $cowrefcnt = CowREFCNT($_); + my $cowrefcnt = _CowREFCNT($_); goto FREE_NONREF if defined $cowrefcnt && 1 < $cowrefcnt; # if (__PACKAGE__->can('erase_xs')) { # erase_xs($_); @@ -318,7 +307,7 @@ sub erase { } elsif (is_scalarref($_)) { next if !defined $$_ || readonly $$_; - my $cowrefcnt = CowREFCNT($$_); + my $cowrefcnt = _CowREFCNT($$_); goto FREE_REF if defined $cowrefcnt && 1 < $cowrefcnt; # if (__PACKAGE__->can('erase_xs')) { # erase_xs($$_); @@ -359,6 +348,7 @@ See L. =cut sub erase_scoped { + throw 'Programmer error: Cannot call erase_scoped in void context' if !defined wantarray; my @args; for (@_) { !is_ref($_) || is_arrayref($_) || is_hashref($_) || is_scalarref($_) @@ -629,7 +619,6 @@ This is the search engine described with many examples at L. sub search { my $list = shift; my $query = shift; - # my %args = @_; if (is_coderef($query) && !@_) { # already a query @@ -641,12 +630,32 @@ sub search { $query = query($query, @_); } - # my $limit = $args{limit}; + my @match; + for my $item (@$list) { + push @match, $item if $query->($item); + } + return \@match; +} + +sub search_limited { + my $list = shift; + my $query = shift; + my $limit = shift // 1; + + if (is_coderef($query) && !@_) { + # already a query + } + elsif (is_scalarref($query)) { + $query = simple_expression_query($$query, @_); + } + else { + $query = query($query, @_); + } my @match; for my $item (@$list) { push @match, $item if $query->($item); - # last if defined $limit && $limit <= @match; + last if $limit <= @match; } return \@match; }