Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/lydown/core_ext.rb
Constant Summary collapse
- UNESCAPES =
String unescaping code from here: stackoverflow.com/a/20131717
{ 'a' => "\x07", 'b' => "\x08", 't' => "\x09", 'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c", 'r' => "\x0d", 'e' => "\x1b", "\\\\" => "\x5c", "\"" => "\x22", "'" => "\x27" }
Instance Method Summary collapse
- #calculate_line_indexes ⇒ Object
- #camelize ⇒ Object
- #find_line_and_column(index) ⇒ Object
- #titlize(all_capitals = false) ⇒ Object
- #unescape ⇒ Object
- #uri_escape ⇒ Object
Instance Method Details
#calculate_line_indexes ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/lydown/core_ext.rb', line 150 def calculate_line_indexes i = -1 indexes = {0 => 0} line = 1 while i = index("\n", i + 1) indexes[line] = i + 1 # first character after line break line += 1 end indexes end |
#camelize ⇒ Object
124 125 126 |
# File 'lib/lydown/core_ext.rb', line 124 def camelize split('_').collect(&:capitalize).join end |
#find_line_and_column(index) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/lydown/core_ext.rb', line 161 def find_line_and_column(index) @line_indexes ||= calculate_line_indexes line = @line_indexes.reverse_each {|l, i| break l if i <= index} # return line and column if line.nil? [nil, nil] else [(line + 1), (index - @line_indexes[line] + 1)] end end |
#titlize(all_capitals = false) ⇒ Object
118 119 120 121 122 |
# File 'lib/lydown/core_ext.rb', line 118 def titlize(all_capitals = false) all_capitals ? self.gsub("-", " ").gsub(/\b('?[a-z])/) {$1.capitalize} : self.gsub("-", " ").capitalize end |
#unescape ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lydown/core_ext.rb', line 137 def unescape # Escape all the things gsub(/\\(?:([#{UNESCAPES.keys.join}])|u([\da-fA-F]{4}))|\\0?x([\da-fA-F]{2})/) { if $1 if $1 == '\\' then '\\' else UNESCAPES[$1] end elsif $2 # escape \u0000 unicode ["#$2".hex].pack('U*') elsif $3 # escape \0xff or \xff [$3].pack('H2') end } end |
#uri_escape ⇒ Object
173 174 175 |
# File 'lib/lydown/core_ext.rb', line 173 def uri_escape EscapeUtils.escape_uri(self) end |