Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
improved compliance for x680 isIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Coretta authored and Jesse Coretta committed Sep 27, 2024
1 parent ffe7968 commit 2feebbf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
74 changes: 41 additions & 33 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,42 +106,50 @@ func isAlnum(r rune) bool {

/*
isIdentifier scans the input string val and judges whether
it appears to qualify as an identifier, in that:
it appears to qualify as an X.680 Identifier, in that:
- it begins with a lower alpha
- it begins with a lower alpha, ends in an alphanumeric
- it contains only alphanumeric characters, hyphens or semicolons
This is used, specifically, it identify an LDAP attributeType (with
or without a tag), or an LDAP matchingRule.
- it contains no consecutive hyphens
*/
func isIdentifier(val string) bool {
if len(val) == 0 {
return false
}

// must begin with lower alpha.
if !isLower(rune(val[0])) {
return false
}

// can only end in alnum.
if !isAlnum(rune(val[len(val)-1])) {
return false
}

for i := 0; i < len(val); i++ {
ch := rune(val[i])
switch {
case isAlnum(ch):
// ok
case ch == ';', ch == '-':
// ok
default:
return false
}
}

return true
func isIdentifier(val string) bool {
if len(val) == 0 {
return false
}

// must begin with a lower alpha.
if !isLower(rune(val[0])) {
return false
}

// can only end in alnum.
if !isAlnum(rune(val[len(val)-1])) {
return false
}

// watch hyphens to avoid contiguous use
var lastHyphen bool

// iterate all characters in val, checking
// each one for validity.
for i := 0; i < len(val); i++ {
ch := rune(val[i])
switch {
case isAlnum(ch):
lastHyphen = false
case ch == '-':
if lastHyphen {
// cannot use consecutive hyphens
return false
}
lastHyphen = true
default:
// invalid character (none of [a-zA-Z0-9\-])
return false
}
}

return true
}

/*
Expand Down
2 changes: 2 additions & 0 deletions misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ func TestIsIdentifier(t *testing.T) {
``,
`itu-t`,
`itu?t`,
`dod`,
`bad--name`,
} {
var err error
is := isIdentifier(candidate)
Expand Down

0 comments on commit 2feebbf

Please # to comment.