addressable 2.9.1 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # Copyright (C) Bob Aman
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+
20
+ module Addressable
21
+ ##
22
+ # Unicode normalization in time linear in the length of the input.
23
+ #
24
+ # <code>String#unicode_normalize</code> normalizes a character and the
25
+ # combining marks after it as one unit, in time quadratic in the number of
26
+ # marks, so a few thousand marks in a row take seconds.
27
+ #
28
+ # @api private
29
+ module UnicodeNormalization
30
+ ##
31
+ # The longest run of combining marks that is normalized. This is the
32
+ # limit of the Stream-Safe Text Format in UAX #15; text in any language
33
+ # stays well below it.
34
+ MAX_COMBINING_MARKS = 30
35
+
36
+ # U+FF9E and U+FF9F aren't combining marks, but NFKC and NFKD map them
37
+ # to U+3099 and U+309A, which are.
38
+ LONG_COMBINING_RUN =
39
+ /([\p{M}゙゚]{#{MAX_COMBINING_MARKS + 1},})/
40
+ private_constant :LONG_COMBINING_RUN
41
+
42
+ ##
43
+ # Normalizes a string, leaving runs of more than
44
+ # <code>MAX_COMBINING_MARKS</code> combining marks as they are.
45
+ #
46
+ # @param [String] string The string to normalize.
47
+ # @param [Symbol] form The normalization form.
48
+ #
49
+ # @return [String] The normalized string.
50
+ def self.normalize(string, form)
51
+ return string.unicode_normalize(form) if LONG_COMBINING_RUN !~ string
52
+
53
+ string.split(LONG_COMBINING_RUN).each_slice(2).map do |text, marks|
54
+ "#{text.unicode_normalize(form)}#{marks}"
55
+ end.join
56
+ end
57
+ end
58
+ end