Class: String

Inherits:
Object
  • Object
show all
Includes:
Walrat::ParsletCombining
Defined in:
lib/walrat/additions/string.rb

Overview

Additions to String class for Unicode support. Parslet combining methods. Convenience methods (to_parseable). Conversion utility methods.

Direct Known Subclasses

Walrat::StringResult

Instance Method Summary collapse

Methods included from Walrat::ParsletCombining

#&, #>>, #and?, #and_predicate, #choice, #memoizing_parse, #merge, #not!, #not_predicate, #omission, #one_or_more, #optional, #parse, #repeat, #repeat_with_default, #repetition, #repetition_with_default, #sequence, #skip, #zero_or_more, #zero_or_one, #|

Instance Method Details

#[](range, other = Walrat::NoParameterMarker.instance) ⇒ Object

multi-byte friendly [] implementation



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/walrat/additions/string.rb', line 66

def [](range, other = Walrat::NoParameterMarker.instance)
  if other == Walrat::NoParameterMarker.instance
    if range.kind_of? Range
      chars.to_a[range].join
    else
      old_range range
    end
  else
    old_range range, other
  end
end

#enumeratorObject

Returns a character-level enumerator for the receiver.



79
80
81
# File 'lib/walrat/additions/string.rb', line 79

def enumerator
  Walrat::StringEnumerator.new self
end

#jindex_plus_length(arg) ⇒ Object

NOTE: this is a totally Walrat-specific implementation that is unlikely to be of use anywhere else. It is used in only 1 place in the codebase, and works around the fact that the MatchData made available by the index method gets clobbered by the “chars.to_a” call. The same thing happens for alternative methods of counting the chars, such as using jlength or a manual scan.

One workaround is for the caller to re-perform the index call just to get the MatchData again, but that is inefficient. So here we just do the addition before returning the result to the caller.



47
48
49
50
51
# File 'lib/walrat/additions/string.rb', line 47

def jindex_plus_length arg
  if i = index(arg)
    $~[0].length + unpack('C*')[0...i].pack('C*').chars.to_a.length
  end
end

#jlengthObject



32
33
34
# File 'lib/walrat/additions/string.rb', line 32

def jlength
  chars.to_a.length
end

#jrindex(arg, offset = Walrat::NoParameterMarker.instance) ⇒ Object

Unlike the normal rindex method, the MatchData in $~ set by the inner rindex call gets clobbered (by the “chars.to_a” call) and is not visible to the caller of this method.



56
57
58
59
60
61
62
63
# File 'lib/walrat/additions/string.rb', line 56

def jrindex arg, offset = Walrat::NoParameterMarker.instance
  if offset == Walrat::NoParameterMarker.instance
    i = rindex arg
  else
    i = rindex arg, offset
  end
  i ? unpack('C*')[0...i].pack('C*').chars.to_a.length : nil
end

#old_rangeObject



30
# File 'lib/walrat/additions/string.rb', line 30

alias old_range []

#to_class_nameObject

Converts the receiver of the form “foo_bar” to “FooBar”. Specifically, the receiver is split into pieces delimited by underscores, each component is then converted to captial case (the first letter is capitalized and the remaining letters are lowercased) and finally the components are joined.



96
97
98
# File 'lib/walrat/additions/string.rb', line 96

def to_class_name
  self.split('_').collect { |component| component.capitalize}.join
end

#to_parseableObject

Returns a StringParslet based on the receiver



88
89
90
# File 'lib/walrat/additions/string.rb', line 88

def to_parseable
  Walrat::StringParslet.new self
end