Class: String

Inherits:
Object
  • Object
show all
Includes:
Walrus::Grammar::ParsletCombining
Defined in:
lib/walrus/additions/string.rb,
lib/walrus/grammar/additions/string.rb,
lib/walrus/grammar/additions/string.rb

Overview

class String

Direct Known Subclasses

Walrus::Grammar::StringResult

Instance Method Summary collapse

Methods included from Walrus::Grammar::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 = Walrus::NoParameterMarker.instance) ⇒ Object

multi-byte friendly [] implementation



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/walrus/grammar/additions/string.rb', line 30

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

#charsObject

Returns an array of Unicode characters.



23
24
25
# File 'lib/walrus/grammar/additions/string.rb', line 23

def chars
  scan(/./m)
end

#enumeratorObject

Returns a character-level enumerator for the receiver.



43
44
45
# File 'lib/walrus/grammar/additions/string.rb', line 43

def enumerator
  Walrus::Grammar::StringEnumerator.new(self)
end

#jindex(substring[, offset]) ⇒ Fixnum? #jindex(fixnum[, offset]) ⇒ Fixnum? #jindex(regexp[, offset]) ⇒ Fixnum?

Multibyte-friendly equivalent of the String#index method. If $KCODE is appropriately set will return an accurate index based on character count rather than byte counts.

Overloads:

  • #jindex(substring[, offset]) ⇒ Fixnum?

    Returns:

    • (Fixnum, nil)
  • #jindex(fixnum[, offset]) ⇒ Fixnum?

    Returns:

    • (Fixnum, nil)
  • #jindex(regexp[, offset]) ⇒ Fixnum?

    Returns:

    • (Fixnum, nil)


66
67
68
69
# File 'ext/jindex.c', line 66

static VALUE walrus_str_jindex_m(int argc, VALUE *argv, VALUE str)
{
    return walrus_str_index_common(rb_intern("index"), argc, argv, str);
}

#jrindex(substring[, offset]) ⇒ Fixnum? #jrindex(fixnum[, offset]) ⇒ Fixnum? #jrindex(regexp[, offset]) ⇒ Fixnum?

Multibyte-friendly equivalent of the String#rindex method. If $KCODE is appropriately set will return an accurate index based on character count rather than byte counts.

Overloads:

  • #jrindex(substring[, offset]) ⇒ Fixnum?

    Returns:

    • (Fixnum, nil)
  • #jrindex(fixnum[, offset]) ⇒ Fixnum?

    Returns:

    • (Fixnum, nil)
  • #jrindex(regexp[, offset]) ⇒ Fixnum?

    Returns:

    • (Fixnum, nil)


83
84
85
86
# File 'ext/jindex.c', line 83

static VALUE walrus_str_jrindex_m(int argc, VALUE *argv, VALUE str)
{
    return walrus_str_index_common(rb_intern("rindex"), argc, argv, str);
}

#old_rangeObject



27
# File 'lib/walrus/grammar/additions/string.rb', line 27

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. Note that this method cannot recover information lost during a conversion using the require_name_from_classname method; for example, “EOL”, when converted to “token”, would be transformed back to “EolToken”. Likewise, “Foo__bar” would be reduced to “foo__bar” and then in the reverse conversion would become “FooBar”.



34
35
36
# File 'lib/walrus/additions/string.rb', line 34

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

#to_parseableObject

Returns a StringParslet based on the receiver



54
55
56
# File 'lib/walrus/grammar/additions/string.rb', line 54

def to_parseable
  Walrus::Grammar::StringParslet.new(self)
end

#to_require_nameObject

Converts the receiver of the form “FooBar” to “foo_bar”. Concretely, the receiver is split into words, each word lowercased, and the words are joined together using a lower-case separator. “Words” are considered to be runs of characters starting with an initial capital letter (note that words may begin with consecutive capital letters), and numbers may mark the start or the end of a word. Note that some information loss may be incurred; for example, “EOLToken” would be reduced to “eol_token”.



23
24
25
26
27
28
29
# File 'lib/walrus/additions/string.rb', line 23

def to_require_name
  base = self.gsub(/([^A-Z_])([A-Z])/, '\1_\2')       # insert an underscore before any initial capital letters
  base.gsub!(/([A-Z])([A-Z])([^A-Z0-9_])/, '\1_\2\3') # consecutive capitals are words too, excluding any following capital that belongs to the next word
  base.gsub!(/([^0-9_])(\d)/, '\1_\2')                # numbers mark the start of a new word
  base.gsub!(/(\d)([^0-9_])/, '\1_\2')                # numbers also mark the end of a word
  base.downcase                                       # lowercase everything
end

#to_source_stringObject

Returns a copy of the receiver with occurrences of \ replaced with \, and occurrences of ‘ replaced with '



39
40
41
# File 'lib/walrus/additions/string.rb', line 39

def to_source_string
  gsub(/[\\']/, '\\\\\&')
end