diff options
| author | Markus Reiter | 2016-10-07 20:03:50 +0200 |
|---|---|---|
| committer | Markus Reiter | 2016-10-07 20:03:50 +0200 |
| commit | fca66e17b38ae7ba952e0136611ea428563f8dba (patch) | |
| tree | cbadd5d69fdaf95bc4f524cdae7e1dfe3b0a23a5 /Library/Homebrew/locale.rb | |
| parent | 7af8cdcb046c8dbb8acd487ef8be67a011b26742 (diff) | |
| download | brew-fca66e17b38ae7ba952e0136611ea428563f8dba.tar.bz2 | |
Make parsing locales more robust.
Diffstat (limited to 'Library/Homebrew/locale.rb')
| -rw-r--r-- | Library/Homebrew/locale.rb | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/Library/Homebrew/locale.rb b/Library/Homebrew/locale.rb index 1aff33dae..e749a5004 100644 --- a/Library/Homebrew/locale.rb +++ b/Library/Homebrew/locale.rb @@ -1,20 +1,28 @@ class Locale - class ParserError < ::RuntimeError + class ParserError < StandardError end - LANGUAGE_REGEX = /(?:[a-z]{2})/ - REGION_REGEX = /(?:[A-Z]{2})/ - SCRIPT_REGEX = /(?:[A-Z][a-z]{3})/ + LANGUAGE_REGEX = /(?:[a-z]{2,3})/ # ISO 639-1 or ISO 639-2 + REGION_REGEX = /(?:[A-Z]{2})/ # ISO 3166-1 + SCRIPT_REGEX = /(?:[A-Z][a-z]{3})/ # ISO 15924 - LOCALE_REGEX = /^(#{LANGUAGE_REGEX})?(?:(?:^|-)(#{REGION_REGEX}))?(?:(?:^|-)(#{SCRIPT_REGEX}))?$/ + LOCALE_REGEX = /\A((?:#{LANGUAGE_REGEX}|#{REGION_REGEX}|#{SCRIPT_REGEX})(?:\-|$)){1,3}\Z/ def self.parse(string) - language, region, script = string.to_s.scan(LOCALE_REGEX)[0] + string = string.to_s - if language.nil? && region.nil? && script.nil? - raise ParserError, "'#{string}' cannot be parsed to a #{self.class}" + if string !~ LOCALE_REGEX + raise ParserError, "'#{string}' cannot be parsed to a #{self}" + end + + scan = proc do |regex| + string.scan(/(?:\-|^)(#{regex})(?:\-|$)/).flatten.first end + language = scan.call(LANGUAGE_REGEX) + region = scan.call(REGION_REGEX) + script = scan.call(SCRIPT_REGEX) + new(language, region, script) end |
