Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Making an LDAP filter more readable

by dakkar (Hermit)
on Jan 11, 2006 at 15:43 UTC ( #522468=note: print w/ replies, xml ) Need Help??


in reply to Making an LDAP filter more readable

What about using a FSA based parser?

#!/usr/bin/perl use strict; use warnings; use Text::Diff; { my $INDENT_STEP=4; sub indenter { my ($expr)=@_; my $indent=0; my $result=''; pos($expr)=undef; while(1) { if ($expr =~ m{\G \s* ( \( [&|!] )}smxcg) { # combinatore: print, newline, inc indent $result.=(' 'x$indent)."$1\n"; $indent+=$INDENT_STEP; } elsif ($expr =~ m{\G \s* ( \( [^)=]+ = [^)]+ \) )}smxcg) { # test: print, newline $result.=(' 'x$indent)."$1\n"; } elsif ($expr =~ m{\G \s* ( \) )}smxcg) { # fine combinatore: dec intert, print, newline $indent-=$INDENT_STEP; $result.=(' 'x$indent)."$1\n"; } else { last; } } return $result; } } my $expr=q{(&(&(&(& (mailnickname=*) (| (&(objectCategory=person)(obje +ctClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCateg +ory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*)))( +&(objectCategory=person)(objectClass=contact))(objectCategory=group)( +objectCategory=publicFolder)(objectCategory=msExchDynamicDistribution +List) )))(objectCategory=contact)(proxyAddresses=smtp:*example.com))) +}; my $expected=<<'END_EXPECTED'; (& (& (& (& (mailnickname=*) (| (& (objectCategory=person) (objectClass=user) (! (homeMDB=*) ) (! (msExchHomeServerName=*) ) ) (& (objectCategory=person) (objectClass=user) (| (homeMDB=*) (msExchHomeServerName=*) ) ) (& (objectCategory=person) (objectClass=contact) ) (objectCategory=group) (objectCategory=publicFolder) (objectCategory=msExchDynamicDistributionList) ) ) ) (objectCategory=contact) (proxyAddresses=smtp:*example.com) ) ) END_EXPECTED my $ret=indenter($expr); print "ok\n" if $ret eq $expected; print diff \$ret,\$expected, { STYLE => 'Unified', };
-- 
        dakkar - Mobilis in mobile

Most of my code is tested...

Perl is strongly typed, it just has very few types (Dan)


Comment on Re: Making an LDAP filter more readable
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://522468]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2015-07-29 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls