Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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 about the Monastery: (6)
As of 2014-07-25 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls