Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/charshift.rb

Instance Method Summary collapse

Instance Method Details

#charshift(char_shift_val, custom_encoding = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/charshift.rb', line 4

def charshift char_shift_val, custom_encoding = nil
  begin
    CharshiftHelper.confirm_fixnum(char_shift_val)
  rescue TypeError => e
    raise
  else
    output = ""
    split_string = CharshiftHelper.encoding_ind_split(self)
    if custom_encoding
      if CharshiftHelper.check_for_valid_array_elements(custom_encoding) == false
        raise ArgumentError, "Custom encoding must only contain single character string elements"
      elsif CharshiftHelper.check_for_uniqueness(custom_encoding) == false
        raise ArgumentError, "All elements in custom encoding must be unique"
      else
        split_string.each_with_index do |char, index|
          encoding_length = custom_encoding.length
          current_char_ord = custom_encoding.index(char)
          if current_char_ord.nil?
            raise RuntimeError, "Given custom encoding does not contain all characters in the target string"
          end
          shifted_position = CharshiftHelper.get_shift_position(current_char_ord, char_shift_val, encoding_length)
          output << custom_encoding[shifted_position]
        end
      end
    else
      split_string.each do |char|
        char_encoding_type = CharshiftHelper.get_encoding(char)
        encoding_length = CharshiftHelper.get_encoding_length(char_encoding_type) - 1
        current_char_ord = CharshiftHelper.get_ord_by_char(char)
        shifted_position = CharshiftHelper.get_shift_position(current_char_ord, char_shift_val, encoding_length)
        output << CharshiftHelper.get_char_by_ord(shifted_position, char_encoding_type)
      end
    end
    return output
  end
end

#charshift!(char_shift_val, custom_encoding = nil) ⇒ Object



41
42
43
44
# File 'lib/charshift.rb', line 41

def charshift! char_shift_val, custom_encoding = nil
  updated_string = charshift char_shift_val, custom_encoding = nil
  return self.replace(updated_string)
end

#get_encoding_lengthObject



46
47
48
# File 'lib/charshift.rb', line 46

def get_encoding_length
  return CharshiftHelper.get_encoding_length(self.encoding)
end