+ Rewrote tests as specs. Ruby1.9 compatible escape code.

This commit is contained in:
Kaspar Schiess 2010-12-30 13:44:17 +01:00
parent a696aa89a0
commit 36904e208a
3 changed files with 51 additions and 9 deletions

View file

@ -1,3 +1,4 @@
# Encoding: UTF-8
# Copyright (C) 2006 by Francis Cianfrocca and other contributors. All
# Rights Reserved.
#
@ -243,10 +244,28 @@ class Net::LDAP::Filter
alias_method :present, :present?
alias_method :pres, :present?
# http://tools.ietf.org/html/rfc4515 lists these exceptions from UTF1
# charset for filters. All of the following must be escaped in any normal
# string using a single backslash ('\') as escape.
#
ESCAPES = {
'!' => '21', # EXCLAMATION = %x21 ; exclamation mark ("!")
'&' => '26', # AMPERSAND = %x26 ; ampersand (or AND symbol) ("&")
'*' => '2A', # ASTERISK = %x2A ; asterisk ("*")
':' => '3A', # COLON = %x3A ; colon (":")
'|' => '7C', # VERTBAR = %x7C ; vertical bar (or pipe) ("|")
'~' => '7E', # TILDE = %x7E ; tilde ("~")
}
# Compiled character class regexp using the keys from the above hash.
ESCAPE_RE = Regexp.new(
"[" +
ESCAPES.keys.map { |e| Regexp.escape(e) }.join +
"]")
##
# Escape a string for use in an LDAP filter
def escape(string)
string.gsub(/[\*\(\)\\\0]/) {|s| sprintf("\\%02x", s[0]) }
string.gsub(ESCAPE_RE) { |char| "\\" + ESCAPES[char] }
end
##