Class: Puppet::Pops::Types::PRegexpType

Inherits:
PScalarType show all
Defined in:
lib/puppet/pops/types/types.rb

Constant Summary collapse

DEFAULT =
PRegexpType.new(nil)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #generalize, #iterable?, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type

Constructor Details

#initialize(pattern) ⇒ PRegexpType

Returns a new instance of PRegexpType.



1667
1668
1669
1670
1671
1672
1673
1674
# File 'lib/puppet/pops/types/types.rb', line 1667

def initialize(pattern)
  if pattern.is_a?(Regexp)
    @regexp = pattern
    @pattern = PRegexpType.regexp_to_s(pattern)
  else
    @pattern = pattern
  end
end

Instance Attribute Details

#patternObject (readonly)



1611
1612
1613
# File 'lib/puppet/pops/types/types.rb', line 1611

def pattern
  @pattern
end

Class Method Details

.append_flags_group(rx_string, options) ⇒ Object



1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
# File 'lib/puppet/pops/types/types.rb', line 1648

def self.append_flags_group(rx_string, options)
  if options == 0
    rx_string
  else
    bld = '(?'
    bld << 'i' if (options & Regexp::IGNORECASE) != 0
    bld << 'm' if (options & Regexp::MULTILINE) != 0
    bld << 'x' if (options & Regexp::EXTENDED) != 0
    unless options == (Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED)
      bld << '-'
      bld << 'i' if (options & Regexp::IGNORECASE) == 0
      bld << 'm' if (options & Regexp::MULTILINE) == 0
      bld << 'x' if (options & Regexp::EXTENDED) == 0
    end
    bld << ':' << rx_string << ')'
    bld.freeze
  end
end

.new_function(type) ⇒ Object

Returns a new function that produces a Regexp instance



1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
# File 'lib/puppet/pops/types/types.rb', line 1599

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_float, type.loader) do
    dispatch :from_string do
      param 'String', :pattern
    end

    def from_string(pattern)
      Regexp.new(pattern)
    end
  end
end

.regexp_to_s(regexp) ⇒ String

Returns the Regexp as a string without escaped slash.

Parameters:

  • regexp (Regexp)

    the regular expression

Returns:

  • (String)

    the Regexp as a string without escaped slash



1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
# File 'lib/puppet/pops/types/types.rb', line 1621

def self.regexp_to_s(regexp)
  # Rubies < 2.0.0 retains escaped delimiters in the source string.
  @source_retains_escaped_slash ||= Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0')
  source = regexp.source
  if @source_retains_escaped_slash && source.include?('\\')
    # Restore corrupt string in rubies <2.0.0, i.e. turn '\/' into '/' but
    # don't touch valid escapes such as '\s', '\{' etc.
    escaped = false
    bld = ''
    source.each_codepoint do |codepoint|
      if escaped
        bld << 0x5c unless codepoint == 0x2f # '/'
        bld << codepoint
        escaped = false
      elsif codepoint == 0x5c # '\'
        escaped = true
      elsif codepoint <= 0x7f
        bld << codepoint
      else
        bld << [codepoint].pack('U')
      end
    end
    source = bld
  end
  append_flags_group(source, regexp.options)
end

.regexp_to_s_with_delimiters(regexp) ⇒ String

Returns the Regexp as a slash delimited string with slashes escaped.

Parameters:

  • regexp (Regexp)

    the regular expression

Returns:

  • (String)

    the Regexp as a slash delimited string with slashes escaped



1615
1616
1617
# File 'lib/puppet/pops/types/types.rb', line 1615

def self.regexp_to_s_with_delimiters(regexp)
  regexp.options == 0 ? regexp.inspect : "/#{regexp.to_s}/"
end

.register_ptype(loader, ir) ⇒ Object



1588
1589
1590
1591
1592
1593
1594
# File 'lib/puppet/pops/types/types.rb', line 1588

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarType',
    'pattern' => {
      KEY_TYPE => PVariantType.new([PUndefType::DEFAULT, PStringType::DEFAULT, PRegexpType::DEFAULT]),
      KEY_VALUE => nil
    })
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1684
1685
1686
# File 'lib/puppet/pops/types/types.rb', line 1684

def eql?(o)
  self.class == o.class && @pattern == o.pattern
end

#hashObject



1680
1681
1682
# File 'lib/puppet/pops/types/types.rb', line 1680

def hash
  @pattern.hash
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1688
1689
1690
# File 'lib/puppet/pops/types/types.rb', line 1688

def instance?(o, guard=nil)
  o.is_a?(Regexp) && @pattern.nil? || regexp == o
end

#regexpObject



1676
1677
1678
# File 'lib/puppet/pops/types/types.rb', line 1676

def regexp
  @regexp ||= Regexp.new(@pattern || '')
end