class StringPrepTablesGenerator

Generator for stringprep regexps.

Combines Unicode character classes with generated tables. Generated regexps are still used to test that the written regexps conform to the specification. Some tables don’t match up well with any character properties available to ruby’s regexp engine. Those use the table-generated regexps.

Constants

SASL_TABLES_PROHIBITED
SASL_TABLES_PROHIBITED_STORED
STRINGPREP_JSON_FILE
STRINGPREP_RFC_FILE
SURROGATES_RANGE

valid UTF-8 can’t contain these codepoints checking for them anyway, using /p{Cs}/ ;)

Attributes

json_filename[R]
rfc_filename[R]

Public Class Methods

new(rfc_filename: STRINGPREP_RFC_FILE, json_filename: STRINGPREP_JSON_FILE) click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 19
def initialize(rfc_filename:  STRINGPREP_RFC_FILE,
               json_filename: STRINGPREP_JSON_FILE)
  @rfc_filename  = rfc_filename
  @json_filename = json_filename
end

Public Instance Methods

arrays() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 42
def arrays;  @arrays  ||= ranges.transform_values{|t| t.flat_map(&:to_a) } end
asgn_regexps() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 45
def asgn_regexps; @asgn_regexps || asgn_regexps! end
clean_deps() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 28
def clean_deps; Rake::FileList.new           STRINGPREP_JSON_FILE end
generate_json_data_file() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 30
def generate_json_data_file
  require "json"
  rfc_filename
    .then(&File.method(:read))
    .then(&method(:parse_rfc_text))
    .then(&JSON.method(:pretty_generate))
    .then {|data| File.write json_filename, data }
end
json_deps() click to toggle source

for rake deps

# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 26
def json_deps;  Rake::FileList.new __FILE__, STRINGPREP_RFC_FILE  end
merged_tables_regex(*table_names, negate: false) click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 47
def merged_tables_regex(*table_names, negate: false)
  table_names
    .flat_map(&arrays.method(:fetch))
    .then {|array| to_regexp(array, negate: negate) }
end
ranges() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 41
def ranges;  @ranges  ||= tables.transform_values(&method(:to_ranges)) end
rb_deps() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 27
def rb_deps;    Rake::FileList.new __FILE__, STRINGPREP_JSON_FILE end
regexp_for(*names, negate: false) click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 53
def regexp_for(*names, negate: false)
  asgn_regexps[[*names, negate]] ||= merged_tables_regex(*names, negate: negate)
end
regexps() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 44
def regexps; @regexps ||= arrays.transform_values(&method(:to_regexp)) end
saslprep_rb() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 152
  def saslprep_rb
    <<~RUBY
      # frozen_string_literal: true

      #--
      # This file is generated from RFC3454, by rake.  Don't edit directly.
      #++

      module Net::IMAP::SASL

        module SASLprep

          # RFC4013 §2.1 Mapping - mapped to space
          # * non-ASCII space characters (\\StringPrep\\[\\"C.1.2\\"]) that can be
          #   mapped to SPACE (U+0020), and
          #
          # Equal to \\StringPrep\\[\\"C.1.2\\"].
          # Redefined here to avoid loading the StringPrep module.
          MAP_TO_SPACE = #{regex_str "C.1.2"}

          # RFC4013 §2.1 Mapping - mapped to nothing
          #   the "commonly mapped to nothing" characters (\\StringPrep\\[\\"B.1\\"])
          #   that can be mapped to nothing.
          #
          # Equal to \\StringPrep\\[\\"B.1\\"].
          # Redefined here to avoid loading the StringPrep module.
          MAP_TO_NOTHING = #{regex_str "B.1"}

          # RFC4013 §2.3 Prohibited Output::
          # * Non-ASCII space characters — \\StringPrep\\[\\"C.1.2\\"]
          # * ASCII control characters — \\StringPrep\\[\\"C.2.1\\"]
          # * Non-ASCII control characters — \\StringPrep\\[\\"C.2.2\\"]
          # * Private Use characters — \\StringPrep\\[\\"C.3\\"]
          # * Non-character code points — \\StringPrep\\[\\"C.4\\"]
          # * Surrogate code points — \\StringPrep\\[\\"C.5\\"]
          # * Inappropriate for plain text characters — \\StringPrep\\[\\"C.6\\"]
          # * Inappropriate for canonical representation characters — \\StringPrep\\[\\"C.7\\"]
          # * Change display properties or deprecated characters — \\StringPrep\\[\\"C.8\\"]
          # * Tagging characters — \\StringPrep\\[\\"C.9\\"]
          TABLES_PROHIBITED = #{SASL_TABLES_PROHIBITED.inspect}.freeze

          # Adds unassigned (by Unicode 3.2) codepoints to TABLES_PROHIBITED.
          #
          # RFC4013 §2.5 Unassigned Code Points::
          #   This profile specifies the \\StringPrep\\[\\"A.1\\"] table as its list of
          #   unassigned code points.
          TABLES_PROHIBITED_STORED = ["A.1", *TABLES_PROHIBITED].freeze

          # Matches codepoints prohibited by RFC4013 §2.3.
          #
          # See TABLES_PROHIBITED.
          #
          # Equal to +Regexp.union+ of the TABLES_PROHIBITED tables.  Redefined
          # here to avoid loading the StringPrep module unless necessary.
          PROHIBITED_OUTPUT = #{regex_str(*SASL_TABLES_PROHIBITED)}

          # RFC4013 §2.5 Unassigned Code Points::
          #   This profile specifies the \\StringPrep\\[\\"A.1\\"] table as its list of
          #   unassigned code points.
          UNASSIGNED = #{regex_str "A.1"}

          # Matches codepoints prohibited by RFC4013 §2.3 and §2.5.
          #
          # See TABLES_PROHIBITED_STORED.
          PROHIBITED_OUTPUT_STORED = Regexp.union(
            UNASSIGNED, PROHIBITED_OUTPUT
          ).freeze

          # Bidirectional Characters [StringPrep, §6]
          BIDI_FAILURE = #{bidi_failure_regexp.inspect}.freeze

          # Matches strings prohibited by RFC4013 §2.3 and §2.4.
          #
          # This checks prohibited output and bidirectional characters.
          PROHIBITED = Regexp.union(
            PROHIBITED_OUTPUT, BIDI_FAILURE,
          )

          # Matches strings prohibited by RFC4013 §2.3, §2.4, and §2.5.
          #
          # This checks prohibited output, bidirectional characters, and
          # unassigned codepoints.
          PROHIBITED_STORED = Regexp.union(
            PROHIBITED_OUTPUT_STORED, BIDI_FAILURE,
          )

        end
      end
    RUBY
  end
sets() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 43
def sets;    @sets    ||= arrays.transform_values(&:to_set) end
stringprep_rb() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 57
  def stringprep_rb
    <<~RUBY
      # frozen_string_literal: true

      #--
      # This file is generated from RFC3454, by rake.  Don't edit directly.
      #++

      module Net::IMAP::SASL

        module StringPrep

          #{asgn_table "A.1"}

          #{asgn_table "B.1"}

          #{asgn_table "B.2"}

          #{asgn_table "B.3"}

          #{asgn_table "C.1.1"}

          #{asgn_table "C.1.2"}

          #{asgn_table "C.2.1"}

          #{asgn_table "C.2.2"}

          #{asgn_table "C.3"}

          #{asgn_table "C.4"}

          #{asgn_table "C.5"}

          #{asgn_table "C.6"}

          #{asgn_table "C.7"}

          #{asgn_table "C.8"}

          #{asgn_table "C.9"}

          #{asgn_table "D.1"}

          # Used to check req3 of bidirectional checks
          #{asgn_table "D.1", negate: true}

          #{asgn_table "D.2"}

          BIDI_DESC_REQ2 = "A string with RandALCat characters must not contain LCat characters."

          # Bidirectional Characters [StringPrep, §6], Requirement 2::
          #   If a string contains any RandALCat character, the string MUST NOT
          #   contain any LCat character.
          BIDI_FAILS_REQ2 = #{bidi_fails_req2.inspect}.freeze

          BIDI_DESC_REQ3 = "A string with RandALCat characters must start and end with RandALCat characters."

          # Bidirectional Characters [StringPrep, §6], Requirement 3::
          #   If a string contains any RandALCat character, a RandALCat
          #   character MUST be the first character of the string, and a
          #   RandALCat character MUST be the last character of the string.
          BIDI_FAILS_REQ3 = #{bidi_fails_req3.inspect}.freeze

          # Bidirectional Characters [StringPrep, §6]
          BIDI_FAILURE = #{bidi_failure_regexp.inspect}.freeze

          # Names of each codepoint table in the RFC-3454 appendices
          TABLE_TITLES = {
            #{table_titles_rb}
          }.freeze

          # Regexps matching each codepoint table in the RFC-3454 appendices
          TABLE_REGEXPS = {
            #{table_regexps_rb}
          }.freeze

        end
      end
    RUBY
  end
table_regexps_rb(indent = 3) click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 145
def table_regexps_rb(indent = 3)
  asgn_regexps # => { ["A.1", false] => regexp, ... }
    .reject {|(_, n), _| n }
    .map {|(t, _), _| "%p => %s," % [t, regexp_const_name(t)] }
    .join("\n#{"  "*indent}")
end
table_titles_rb(indent = 3) click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 139
def table_titles_rb(indent = 3)
  titles
    .map{|t| "%p => %p," % t }
    .join("\n#{"  "*indent}")
end
tables() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 39
def tables;  @tables  ||= load_tables_and_titles_from_json!.first end
titles() click to toggle source
# File net-imap-0.3.4/rakelib/string_prep_tables_generator.rb, line 40
def titles;  @titles  ||= load_tables_and_titles_from_json!.last end