Re: Algorithm for right-to-left comparison of strings
- From: Hallvard B Furuseth <h.b.furuseth@xxxxxxxxxxx>
- Date: Mon, 18 Aug 2008 01:06:37 +0200
ssg31415926 writes:
Is there a standard algorithm for right-to-left comparison of
(probably unequal-length) strings?
I want to be able to sort a collection of distinguishedNames. DNs are
right-to-left significant, if you see what I mean, like DNS names.
(...)
First walk through the strings once, generating the information you need
to sort them easily.
Maybe a struct {pointer to end of string, length}.
Or {number of components, reverse array {component, length}}.
Or reverse the order of the components of each DN:
"ou=one,dc=two,dc=three" -> {length, "dc=three\0dc=two\0ou=one\0"}.
Sort with memcmp, being careful to use the smallest string length + 1.
The \0's prevent the presumably-undesired sort order
ou=one,dc=two,dc=three (reversed ...,dc=two,ou=one)
dc=one more,dc=two,dc=three (reversed ...,dc=two,dc=one more)
cn=zero,ou=one,dc=two,dc=three (reversed ...,dc=two,ou=one,cn=zero)
wich you'd get if you kept the commas.
DNs are complex beasts though. Do you e.g. need to worry about
different spellings of DNs that compare equal? If so, part of the
preparation above should be to pick a normalized format and convert them
all to it before comparing. Upper->lowercase. Pick one of the several
ways to spell special chars. E.g. a DN component can contain an escaped
comma spelled as "\," or "\2C". (At least LDAP DNs, I don't know the
current details of spelling X.500 DNs). And so on.
That can still be fooled if someone tries to though, e.g. by feeding you
a DN component in "oid=#hex" format (see RFC 4514). So if you *really*
need correct sorting, you need to convert the DNs back to data
structures ((RDN = "attr=decoded value", ...), RDN, ...) and sort those.
--
Hallvard
.
- Follow-Ups:
- Re: Algorithm for right-to-left comparison of strings
- From: ssg31415926
- Re: Algorithm for right-to-left comparison of strings
- References:
- Algorithm for right-to-left comparison of strings
- From: ssg31415926
- Algorithm for right-to-left comparison of strings
- Prev by Date: Batch file - Count number if instances program is open?
- Next by Date: Re: Whats the best language to learn...
- Previous by thread: Re: Algorithm for right-to-left comparison of strings
- Next by thread: Re: Algorithm for right-to-left comparison of strings
- Index(es):
Relevant Pages
|