regexp_property_values 0.3.4-java → 1.2.0-java

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,63 @@
1
+ module RegexpPropertyValues
2
+ class Value
3
+ module SharedMethods
4
+ attr_reader :name
5
+
6
+ def initialize(name)
7
+ @name = name
8
+ end
9
+
10
+ def supported_by_current_ruby?
11
+ !!regexp rescue false
12
+ end
13
+
14
+ def ==(other)
15
+ identifier == other.identifier
16
+ end
17
+ alias eql? ==
18
+
19
+ def hash
20
+ @hash ||= identifier.hash
21
+ end
22
+
23
+ def identifier
24
+ @identifier ||= name.to_s.downcase.gsub(/[^0-9a-z=.]/, '')
25
+ end
26
+ alias to_s identifier
27
+
28
+ def full_name
29
+ (original = find_original) ? original.name : raise_unknown_error
30
+ end
31
+
32
+ def character_set
33
+ require 'character_set'
34
+ CharacterSet.from_ranges(*matched_ranges)
35
+ end
36
+
37
+ private
38
+
39
+ def regexp
40
+ @regexp ||= /\p{#{identifier}}/u
41
+ rescue RegexpError, SyntaxError
42
+ raise_unsupported_or_unknown_error
43
+ end
44
+
45
+ def find_original
46
+ RegexpPropertyValues.all.find { |orig| orig.eql?(self) } ||
47
+ RegexpPropertyValues.alias_hash[self]
48
+ end
49
+
50
+ def raise_unsupported_or_unknown_error
51
+ find_original ? raise_unsupported_error : raise_unknown_error
52
+ end
53
+
54
+ def raise_unsupported_error
55
+ raise Error, "Property name `#{name}` is known, but not in this Ruby"
56
+ end
57
+
58
+ def raise_unknown_error
59
+ raise Error, "Property name `#{name}` is not known in any Ruby"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ module RegexpPropertyValues
2
+ class Value
3
+ require_relative 'value/shared_methods'
4
+ include SharedMethods
5
+
6
+ if const_defined?(:OnigRegexpPropertyHelper)
7
+ require_relative 'value/ext_adapter'
8
+ include ExtAdapter
9
+ else
10
+ require_relative 'value/ruby_fallback'
11
+ include RubyFallback
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module RegexpPropertyValues
2
- VERSION = '0.3.4'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -3,77 +3,35 @@ begin
3
3
  rescue LoadError
4
4
  warn 'regexp_property_values could not load C extension, using slower Ruby'
5
5
  end
6
- require 'regexp_property_values/extension'
6
+ require 'regexp_property_values/updater'
7
+ require 'regexp_property_values/value'
7
8
  require 'regexp_property_values/version'
8
9
 
9
10
  module RegexpPropertyValues
10
- module_function
11
+ Error = Class.new(StandardError)
11
12
 
12
- LIST_URL = 'https://raw.githubusercontent.com/k-takata/Onigmo/master/doc/UnicodeProps.txt'
13
+ VALUES_PATH = File.join(__dir__, 'values')
14
+ ALIASES_PATH = File.join(__dir__, 'aliases')
13
15
 
14
- def update
15
- puts "Downloading #{LIST_URL}"
16
- require 'open-uri'
17
- File.open(file_path, 'w') { |f| IO.copy_stream(open(LIST_URL), f) }
18
- puts 'Done!'
16
+ def self.[](name)
17
+ Value.new(name)
19
18
  end
20
19
 
21
- def file_path
22
- File.expand_path('../UnicodeProps.txt', __FILE__)
20
+ def self.all_for_current_ruby
21
+ @all_for_current_ruby ||= all.select(&:supported_by_current_ruby?)
23
22
  end
24
23
 
25
- def all
26
- by_category.values.flatten
24
+ def self.all
25
+ @all ||= File.readlines(VALUES_PATH).map { |line| Value.new(line.chomp) }
27
26
  end
28
27
 
29
- def all_for_current_ruby
30
- all.select(&:supported_by_current_ruby?)
28
+ def self.alias_hash
29
+ @alias_hash ||= File.readlines(ALIASES_PATH).map do |line|
30
+ line.chomp.split(';').map { |name| Value.new(name) }
31
+ end.to_h
31
32
  end
32
33
 
33
- def by_category
34
- result = File.foreach(file_path).each_with_object({}) do |line, hash|
35
- if /^\* (?<category>\S.+)/ =~ line
36
- @current_category = category
37
- hash[@current_category] ||= []
38
- elsif /^ {4}(?<value_name>\S.*)/ =~ line
39
- hash[@current_category] << value_name.extend(Extension)
40
- end
41
- end
42
- add_oniguruma_properties(result)
43
- result
44
- end
45
-
46
- def add_oniguruma_properties(props_by_category)
47
- props_by_category['Special'] << 'Newline'.extend(Extension)
48
- end
49
-
50
- def alias_hash
51
- short_names, long_names = short_and_long_names
52
- return {} if short_names.empty?
53
-
54
- long_names -= by_category['POSIX brackets']
55
- by_matched_codepoints.each_value.each_with_object({}) do |props, hash|
56
- next if props.count < 2
57
- long_name = (props & long_names)[0] || fail("no long name for #{props}")
58
- (props & short_names).each { |short_name| hash[short_name] = long_name }
59
- end
60
- end
61
-
62
- def short_and_long_names
63
- short_name_categories = ['Major and General Categories',
64
- 'PropertyAliases',
65
- 'PropertyValueAliases (Script)']
66
- by_category.each_with_object([[], []]) do |(cat_name, props), (short, long)|
67
- (short_name_categories.include?(cat_name) ? short : long).concat(props)
68
- end
69
- end
70
-
71
- def by_matched_codepoints
72
- puts 'Establishing property codepoints, this may take a bit ...'
73
- all_for_current_ruby.group_by(&:matched_codepoints)
74
- end
75
-
76
- def [](prop)
77
- prop.extend(Extension)
34
+ def self.update(ucd_path: nil, emoji_path: nil)
35
+ Updater.call(ucd_path: ucd_path, emoji_path: emoji_path)
78
36
  end
79
37
  end