Skip to content

Commit 05e9741

Browse files
committed
🎨 Whitespace and formatting only
This was split from the prior commit to make the diffs easier to read.
1 parent 127af38 commit 05e9741

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

lib/net/imap/stringprep/saslprep.rb

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,65 @@ module Net
44
class IMAP
55
module StringPrep
66

7-
# SASLprep#saslprep can be used to prepare a string according to [RFC4013].
8-
#
9-
# \SASLprep maps characters three ways: to nothing, to space, and Unicode
10-
# normalization form KC. \SASLprep prohibits codepoints from nearly all
11-
# standard StringPrep tables (RFC3454, Appendix "C"), and uses \StringPrep's
12-
# standard bidirectional characters requirements (Appendix "D"). \SASLprep
13-
# also uses \StringPrep's definition of "Unassigned" codepoints (Appendix "A").
14-
module SASLprep
7+
# SASLprep#saslprep can be used to prepare a string according to [RFC4013].
8+
#
9+
# \SASLprep maps characters three ways: to nothing, to space, and Unicode
10+
# normalization form KC. \SASLprep prohibits codepoints from nearly all
11+
# standard StringPrep tables (RFC3454, Appendix "C"), and uses
12+
# \StringPrep's standard bidirectional characters requirements (Appendix
13+
# "D"). \SASLprep also uses \StringPrep's definition of "Unassigned"
14+
# codepoints (Appendix "A").
15+
module SASLprep
1516

16-
# Used to short-circuit strings that don't need preparation.
17-
ASCII_NO_CTRLS = /\A[\x20-\x7e]*\z/u.freeze
17+
# Used to short-circuit strings that don't need preparation.
18+
ASCII_NO_CTRLS = /\A[\x20-\x7e]*\z/u.freeze
1819

19-
# Avoid loading these tables unless they are needed (they are only
20-
# needed for non-ASCII).
21-
saslprep_tables = File.expand_path("saslprep_tables", __dir__)
22-
autoload :MAP_TO_NOTHING, saslprep_tables
23-
autoload :MAP_TO_SPACE, saslprep_tables
24-
autoload :PROHIBITED, saslprep_tables
25-
autoload :PROHIBITED_STORED, saslprep_tables
26-
autoload :TABLES_PROHIBITED, saslprep_tables
27-
autoload :TABLES_PROHIBITED_STORED, saslprep_tables
20+
# Avoid loading these tables unless they are needed (they are only
21+
# needed for non-ASCII).
22+
saslprep_tables = File.expand_path("saslprep_tables", __dir__)
23+
autoload :MAP_TO_NOTHING, saslprep_tables
24+
autoload :MAP_TO_SPACE, saslprep_tables
25+
autoload :PROHIBITED, saslprep_tables
26+
autoload :PROHIBITED_STORED, saslprep_tables
27+
autoload :TABLES_PROHIBITED, saslprep_tables
28+
autoload :TABLES_PROHIBITED_STORED, saslprep_tables
2829

29-
module_function
30+
module_function
3031

31-
# Prepares a UTF-8 +string+ for comparison, using the \SASLprep profile
32-
# RFC4013 of the StringPrep algorithm RFC3454.
33-
#
34-
# By default, prohibited strings will return +nil+. When +exception+ is
35-
# +true+, a StringPrepError describing the violation will be raised.
36-
#
37-
# When +stored+ is +true+, "unassigned" codepoints will be prohibited. For
38-
# \StringPrep and the \SASLprep profile, "unassigned" refers to Unicode 3.2,
39-
# and not later versions. See RFC3454 §7 for more information.
40-
#
41-
def saslprep(str, stored: false, exception: false)
42-
return str if ASCII_NO_CTRLS.match?(str) # raises on incompatible encoding
43-
str = str.encode("UTF-8") # also dups (and raises for invalid encoding)
44-
str.gsub!(MAP_TO_SPACE, " ")
45-
str.gsub!(MAP_TO_NOTHING, "")
46-
str.unicode_normalize!(:nfkc)
47-
# These regexps combine the prohibited and bidirectional checks
48-
return str unless str.match?(stored ? PROHIBITED_STORED : PROHIBITED)
49-
return nil unless exception
50-
# raise helpful errors to indicate *why* it failed:
51-
tables = stored ? TABLES_PROHIBITED_STORED : TABLES_PROHIBITED
52-
StringPrep.check_prohibited! str, *tables, bidi: true, profile: "SASLprep"
53-
raise InvalidStringError.new(
54-
"unknown error", string: string, profile: "SASLprep"
55-
)
56-
rescue ArgumentError, Encoding::CompatibilityError => ex
57-
if /invalid byte sequence|incompatible encoding/.match? ex.message
58-
return nil unless exception
59-
raise StringPrepError.new(ex.message, string: str, profile: "saslprep")
60-
end
61-
raise ex
62-
end
32+
# Prepares a UTF-8 +string+ for comparison, using the \SASLprep profile
33+
# RFC4013 of the StringPrep algorithm RFC3454.
34+
#
35+
# By default, prohibited strings will return +nil+. When +exception+ is
36+
# +true+, a StringPrepError describing the violation will be raised.
37+
#
38+
# When +stored+ is +true+, "unassigned" codepoints will be prohibited.
39+
# For \StringPrep and the \SASLprep profile, "unassigned" refers to
40+
# Unicode 3.2, and not later versions. See RFC3454 §7 for more
41+
# information.
42+
def saslprep(str, stored: false, exception: false)
43+
return str if ASCII_NO_CTRLS.match?(str) # incompatible encoding raises
44+
str = str.encode("UTF-8") # also dups (and raises for invalid encoding)
45+
str.gsub!(MAP_TO_SPACE, " ")
46+
str.gsub!(MAP_TO_NOTHING, "")
47+
str.unicode_normalize!(:nfkc)
48+
# These regexps combine the prohibited and bidirectional checks
49+
return str unless str.match?(stored ? PROHIBITED_STORED : PROHIBITED)
50+
return nil unless exception
51+
# raise helpful errors to indicate *why* it failed:
52+
tables = stored ? TABLES_PROHIBITED_STORED : TABLES_PROHIBITED
53+
StringPrep.check_prohibited! str, *tables, bidi: true, profile: "SASLprep"
54+
raise InvalidStringError.new(
55+
"unknown error", string: string, profile: "SASLprep"
56+
)
57+
rescue ArgumentError, Encoding::CompatibilityError => ex
58+
if /invalid byte sequence|incompatible encoding/.match? ex.message
59+
return nil unless exception
60+
raise StringPrepError.new(ex.message, string: str, profile: "saslprep")
61+
end
62+
raise ex
63+
end
6364

64-
end
65+
end
6566

6667
end
6768
end

0 commit comments

Comments
 (0)