Class: MODL::Parser::UnicodeEscapes
- Inherits:
-
Object
- Object
- MODL::Parser::UnicodeEscapes
- Defined in:
- lib/modl/parser/unicode_escapes.rb
Overview
Escape-sequence replacements for MODL files.
Class Method Summary collapse
-
.process(str) ⇒ Object
Replace all unicode escape sequences in the supplied string and return the new value.
Class Method Details
.process(str) ⇒ Object
Replace all unicode escape sequences in the supplied string and return the new value.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/modl/parser/unicode_escapes.rb', line 31 def self.process(str) return str unless str.is_a? String start = 0 result = str loop do backslash_u = result.index('\u', start) tilde_u = result.index('~u', start) break if tilde_u.nil? && backslash_u.nil? if tilde_u.nil? uni_str_idx = backslash_u elsif backslash_u.nil? uni_str_idx = tilde_u else uni_str_idx = [backslash_u, tilde_u].min end break if uni_str_idx + 6 > result.length start = uni_str_idx + 1 next if uni_str_idx > 0 && result[uni_str_idx - 1] == '~' next if uni_str_idx > 0 && result[uni_str_idx - 1] == '\\' value = result.slice(uni_str_idx + 2, 4).to_i(16) uni_val = value.chr(Encoding::UTF_8) left = result.slice(0, uni_str_idx) right = result.slice(uni_str_idx + 6, result.length) result = left + uni_val + right unless right.nil? result = left + uni_val if right.nil? end result end |