charshift 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/charshift.rb +49 -0
  3. data/lib/charshifthelper.rb +93 -0
  4. metadata +56 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e93427a0056651708796614d95babdb69ed48cce
4
+ data.tar.gz: 7e68b07ef714040b7be31de6789dc2a32b6251a1
5
+ SHA512:
6
+ metadata.gz: eb90ecb90fdfedb901cda1fade292ea7970a8e173687fa2fbbd59291ee7ebf15360ca752369e88e2cbf7a96050b21304d3dd885a554a83545d0da6c9f7e1e998
7
+ data.tar.gz: ab7c345e18f1cbfd98a57d2e229ea1bf75caff4995b88cc895da0617d6aba5f184fa87ab41e3167d3ff1964740a5302ee1c932aed571cc17cc88c6ff27c79bf9
data/lib/charshift.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'charshifthelper'
2
+
3
+ class String
4
+ def charshift char_shift_val, custom_encoding = nil
5
+ begin
6
+ CharshiftHelper.confirm_fixnum(char_shift_val)
7
+ rescue TypeError => e
8
+ raise
9
+ else
10
+ output = ""
11
+ split_string = CharshiftHelper.encoding_ind_split(self)
12
+ if custom_encoding
13
+ if CharshiftHelper.check_for_valid_array_elements(custom_encoding) == false
14
+ raise ArgumentError, "Custom encoding must only contain single character string elements"
15
+ elsif CharshiftHelper.check_for_uniqueness(custom_encoding) == false
16
+ raise ArgumentError, "All elements in custom encoding must be unique"
17
+ else
18
+ split_string.each_with_index do |char, index|
19
+ encoding_length = custom_encoding.length
20
+ current_char_ord = custom_encoding.index(char)
21
+ if current_char_ord.nil?
22
+ raise RuntimeError, "Given custom encoding does not contain all characters in the target string"
23
+ end
24
+ shifted_position = CharshiftHelper.get_shift_position(current_char_ord, char_shift_val, encoding_length)
25
+ output << custom_encoding[shifted_position]
26
+ end
27
+ end
28
+ else
29
+ split_string.each do |char|
30
+ char_encoding_type = CharshiftHelper.get_encoding(char)
31
+ encoding_length = CharshiftHelper.get_encoding_length(char_encoding_type) - 1
32
+ current_char_ord = CharshiftHelper.get_ord_by_char(char)
33
+ shifted_position = CharshiftHelper.get_shift_position(current_char_ord, char_shift_val, encoding_length)
34
+ output << CharshiftHelper.get_char_by_ord(shifted_position, char_encoding_type)
35
+ end
36
+ end
37
+ return output
38
+ end
39
+ end
40
+
41
+ def charshift! char_shift_val, custom_encoding = nil
42
+ updated_string = charshift char_shift_val, custom_encoding = nil
43
+ return self.replace(updated_string)
44
+ end
45
+
46
+ def getEncodingLength
47
+ return CharshiftHelper.get_encoding_length(self.encoding)
48
+ end
49
+ end
@@ -0,0 +1,93 @@
1
+ module CharshiftHelper
2
+
3
+ def self.confirm_fixnum input_val
4
+ if !input_val.instance_of? Fixnum
5
+ raise TypeError, 'Input value must be of type fixnum'
6
+ else
7
+ return true
8
+ end
9
+ end
10
+
11
+ def self.encoding_ind_split input_string
12
+ split_string = []
13
+
14
+ input_string.each_char do |chr|
15
+ split_string << chr
16
+ end
17
+ return split_string
18
+ end
19
+
20
+ def self.get_shift_distance_minus_loops input_val, collection_length
21
+ absloute_input_val = input_val.abs
22
+ remaining_difference = absloute_input_val % collection_length
23
+ if absloute_input_val > collection_length
24
+ return input_val > 0 ? remaining_difference : 0 - remaining_difference
25
+ else
26
+ return input_val
27
+ end
28
+ end
29
+
30
+ def self.get_shift_position starting_pos, shift_val, encoding_length
31
+ collection_length = encoding_length
32
+ shift_difference = self.get_shift_distance_minus_loops(shift_val, collection_length)
33
+ start_plus_shift = starting_pos + shift_difference
34
+ if shift_difference + starting_pos == encoding_length
35
+ return 0
36
+ elsif start_plus_shift > collection_length
37
+ return 0 + start_plus_shift - collection_length
38
+ elsif start_plus_shift < 0
39
+ return collection_length + start_plus_shift
40
+ else
41
+ return start_plus_shift
42
+ end
43
+ end
44
+
45
+ def self.get_char_by_ord ordinal_value, char_encoding_type
46
+ return ordinal_value.chr(char_encoding_type)
47
+ end
48
+
49
+ def self.get_ord_by_char character
50
+ return character.ord
51
+ end
52
+
53
+ def self.get_encoding string
54
+ return string.encoding.to_s
55
+ end
56
+
57
+ def self.get_encoding_length encoding
58
+ max = 100000000000
59
+ min = 0
60
+ guess = 50000000000
61
+
62
+ while true
63
+ begin guess.chr(encoding)
64
+ if (min > max)
65
+ return max + 1
66
+ else
67
+ min = guess + 1
68
+ guess = (max + min) / 2
69
+ end
70
+ rescue
71
+ if min > max
72
+ return max + 1
73
+ else
74
+ max = guess - 1
75
+ guess = (max + min) / 2
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def self.check_for_valid_array_elements string_array
82
+ string_array.each do |chr|
83
+ if !chr.is_a?(String) || chr.length != 1
84
+ return false
85
+ end
86
+ end
87
+ return true
88
+ end
89
+
90
+ def self.check_for_uniqueness string_array
91
+ return string_array.uniq.length == string_array.length
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: charshift
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathon Nordquist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " Charshift is a simple gem which adds functionality
14
+ to the\n String class. It's primary function is to act on a given\n
15
+ \ string, taking a fixnum parameter, then shifting each \n character
16
+ in that string to a higher or lower ordinal \n position in that
17
+ strings encoding.\n\n Charshift works with all of Ruby's included
18
+ encodings and \n also works with devloper supplied 'custom encodings.'
19
+ \ \n Simply provide an ordered set of characters as an optional\n
20
+ \ parameter and charshift will work on the string using that\n set
21
+ instead of the strings native encoding.\n\n Charshift also includes
22
+ a '.getEncodingLength' method which\n returns the number of of
23
+ characters which a given strings \n encoding contains. Finally,
24
+ strings can be shifted in place\n using the '.charshift!' method.\n\n"
25
+ email: https://github.com/cugamer/charshift
26
+ executables: []
27
+ extensions: []
28
+ extra_rdoc_files: []
29
+ files:
30
+ - lib/charshift.rb
31
+ - lib/charshifthelper.rb
32
+ homepage: http://www.jonathonnordquist.com
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.4.6
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Facilitate character change by ordinal position
56
+ test_files: []