injalid_dejice 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 810e98c784de94eb504872bdb8bc8dd7eadcb948c40b40b77ddc8050b787da31
4
+ data.tar.gz: fe8a847cfdcf4c8e631e1ce1759fe4d68ebf9e166d79873d223e9ebf330e060b
5
+ SHA512:
6
+ metadata.gz: 9e4ff10a85b68e80f8c099abd8083c7df0ab556ce88eba41e9ff2118ed868a341cfe9cfd2e5e0fd803e9ae801bfe5e6aee28bb0cb8e2100356ec9aa6585ade33
7
+ data.tar.gz: eb223603c6d2b4788b66dde0011a56dce5618c16ae620ad6aa501a6f69b15f6752e79d20a88bff3ef7f1c8125b9bdd42f11d27ff8016e32540a0a6723142ce74
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,21 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "rdbg",
9
+ "name": "Debug current file with rdbg",
10
+ "request": "launch",
11
+ "script": "${file}",
12
+ "args": [],
13
+ "askParameters": true
14
+ },
15
+ {
16
+ "type": "rdbg",
17
+ "name": "Attach with rdbg",
18
+ "request": "attach"
19
+ }
20
+ ]
21
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2024-01-11
2
+
3
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 8bit-m8
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # injalid_dejice.rb
2
+
3
+ Convets UTF-8 strings to the legacy KOI-7 encoding.
4
+
5
+ **KOI-7** (Russian: КОИ-7) was a 7-bit character encoding developed in the USSR and based on the US-ASCII encoding.
6
+
7
+ US-ASCII has only 128 code points, which is enough to encode the Latin alphabet (with numbers and most common punctuation marks), but not enough to encode both Latin and Cyrillic alphabets with the full set of characters (uppercase and lowercase). In order to add the Cyrillic alphabet support, but also remain the US-ASCII compatibility, KOI-7 designers came up with a “hack”, in which both Cyrillic and Latin characters were encoded with the same code points, while the ASCII control characters 0x0E and 0x0F were adapted to switch between the Cyrillic and the Latin code pages respectively.
8
+
9
+ That led to funny bugs when a message was displayed with an incorrect code page. The «**иНЖАЛИД ДЕЖИЦЕ**» message (pronounced as “injalid dejitze”) was the most memorable, and it became somewhat a meme (hence the name of the gem). In fact, it was an incorrectly displayed English message, that simply stated: “Invalid device”.
10
+
11
+ KOI-7 was widely used on the Soviet PDP-11 clones, notably the DVK systems and the Elektronika MK90 portable computer.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application"s Gemfile:
16
+
17
+ ```ruby
18
+ gem "injalid_dejice"
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install injalid_dejice
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ require "injalid_dejice"
33
+
34
+ # Encode:
35
+ str_to_encode = "Пользователи ДВК помнят 'инжалид дежице'!"
36
+ InjalidDejice.utf_to_koi(str_to_encode)
37
+ # => "\x0EpOLXZOWATELI dwk POMNQT 'INVALID DEVICE'!\x0F"
38
+
39
+ # Decode:
40
+ str_to_decode = "In KOI-7, two control characters can turn 'bhc' into '\x0Ebhc\x0F'!"
41
+ InjalidDejice.koi_to_utf(str_to_decode)
42
+ # => "In KOI-7, two control characters can turn 'bhc' into 'БХЦ'!"
43
+ ```
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/8bit-mate/injalid_dejice.rb.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/injalid_dejice/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "injalid_dejice"
7
+ spec.version = InjalidDejice::VERSION
8
+ spec.authors = ["8bit-m8"]
9
+ spec.email = ["you@example.com"]
10
+
11
+ spec.summary = "UTF-8 <-> KOI-7 encoder/decoder."
12
+ spec.homepage = "https://github.com/8bit-mate/injalid_dejice.rb"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = spec.homepage
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InjalidDejice
4
+ #
5
+ # Decodes an US-ASCII (KOI-7-compatible) string to the UTF-8 encoding.
6
+ #
7
+ class Decoder
8
+ OUTPUT_ENCODING = "UTF-8"
9
+
10
+ #
11
+ # Encode US-ANSII (KOI-7-compatible) string to the UTF-8.
12
+ #
13
+ # @param [String] input_string
14
+ #
15
+ # @return [String] modified_string
16
+ #
17
+ # @raise [ArgumentError]
18
+ # Found the beginning of the Cyrillic sub-string, but couldn't find the end.
19
+ #
20
+ def call(input_string)
21
+ modified_string = input_string.dup
22
+ start_idx = nil
23
+
24
+ input_string.each_char.with_index do |char, idx|
25
+ # Everything above the 0x7F cannot be in the KOI-7,
26
+ # so just get rid of it.
27
+ modified_string[idx] = UNKNOWN_CHAR if char.ord > 127
28
+
29
+ # Found beginning of a Cyr. sub-string.
30
+ start_idx = idx if char == CYR_CHAR
31
+
32
+ # Found end of a Cyr. sub-string. Decode everything between the
33
+ # control characters.
34
+ if char == LATIN_CHAR
35
+ # Decode characters only if 'start_idx' was set.
36
+ if start_idx
37
+ (start_idx + 1..idx).each do |sub_idx|
38
+ if modified_string[sub_idx].ord >= SHARED_CHARS_LIMIT
39
+ modified_string[sub_idx] = (LAT_TO_CYR_DICT[modified_string[sub_idx]] || UNKNOWN_CHAR)
40
+ end
41
+ end
42
+ end
43
+ start_idx = nil
44
+ end
45
+ end
46
+
47
+ # Found the beginning of the Cyr. sub-string, but couldn't find the end (the LATIN_CHAR marker),
48
+ # probably that means an unexpected end of the string. Raise an error.
49
+ raise ArgumentError, "unexpected end of the string" if start_idx
50
+
51
+ # Before the return remove control characters with the regex, and set the encoding.
52
+ modified_string.gsub(/[#{LATIN_CHAR}#{CYR_CHAR}]/, "").force_encoding(OUTPUT_ENCODING)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InjalidDejice
4
+ #
5
+ # Encodes a UTF-8 string to the KOI-7 encoding.
6
+ #
7
+ class Encoder
8
+ OUTPUT_ENCODING = "US-ASCII"
9
+
10
+ #
11
+ # Encode 'input_string' to the KOI-7 encoding.
12
+ #
13
+ # @param [String] input_string
14
+ # A UTF-8 string.
15
+ #
16
+ # @return [String]
17
+ # An ASCII string, KOI-7-compatible.
18
+ #
19
+ def call(input_string)
20
+ non_latin_indexes = _search_non_latin_indexes(input_string)
21
+
22
+ result = if non_latin_indexes.empty?
23
+ # No non-Latin characters were found, so no futher action needed.
24
+ input_string.dup
25
+ else
26
+ _insert_shift_chars(
27
+ _handle_replacements(input_string),
28
+ non_latin_indexes
29
+ )
30
+ end
31
+
32
+ result.force_encoding(OUTPUT_ENCODING)
33
+ end
34
+
35
+ private
36
+
37
+ #
38
+ # Replace all non-Latin characters in the 'input_string'.
39
+ #
40
+ # @param [String] input_string
41
+ # @param [Array<Array<Integer>>] non_latin_indexes
42
+ #
43
+ # @return [String] modified_string
44
+ #
45
+ def _handle_replacements(input_string)
46
+ encoding_options = {
47
+ invalid: :replace,
48
+ fallback: lambda { |char|
49
+ CYR_TO_LAT_DICT.fetch(char, UNKNOWN_CHAR)
50
+ }
51
+ }
52
+
53
+ input_string.encode(Encoding.find("US-ASCII"), **encoding_options)
54
+ end
55
+
56
+ #
57
+ # Insert SO & SI control characters to the string.
58
+ #
59
+ # @param [String] input_string
60
+ # @param [Array<Array<Integer>>] non_latin_indexes
61
+ #
62
+ # @return [String] input_string
63
+ # Modified string.
64
+ #
65
+ def _insert_shift_chars(input_string, non_latin_indexes)
66
+ i = 0 # Counter of insertions.
67
+
68
+ non_latin_indexes.each do |start_idx, end_idx|
69
+ # Add a Cyrillic code page marker (SO) at the beginning of the Cyrillic sub-string.
70
+ # + 2 * i since each i-th Cyrillic sub-string gets wrapped with two chars (SO + SI),
71
+ # so all indexes get shifted.
72
+ input_string.insert(start_idx + 2 * i, CYR_CHAR)
73
+
74
+ # Add a Latin code page marker (SI) at the end of the Cyrillic sub-string.
75
+ # + (end_idx + 2) since #insert method inserts a sub-string *before* the character at
76
+ # the given index, so +1 to insert it *after*. Another +1 as we've just insert SO
77
+ # character, so all indexes were shifted.
78
+ input_string.insert((end_idx + 2) + 2 * i, LATIN_CHAR)
79
+ i += 1
80
+ end
81
+
82
+ input_string
83
+ end
84
+
85
+ #
86
+ # Return start and end indexes for all so-called "non-Latin" sub-strings of the
87
+ # input string. Numbers and some punctuation characters can be part of both Latin,
88
+ # and non-Latin sub-strings. The "locale" of these characters is defined by the
89
+ # preceding character. E.g., in the string "привет-123 hello-456" the "-123 " chunk
90
+ # (note the trailing space) would be considered as a part of the non-Latin sub-
91
+ # string, while the "-456" chunk as a part of the Latin sub-string.
92
+ #
93
+ # @param [String] input_string
94
+ #
95
+ # @return [Array<Array<Integer>>] non_latin_indexes
96
+ # Pairs of non-Latin character indexes.
97
+ # Example: [[2, 5], [9, 14]], i.e. 'input_string' has two non-Latin sub-strings.
98
+ # The first one occupies characters from 2-nd to 5-th, the second one from 9-th
99
+ # to 14-th. If there's no such sub-string, the 'non_latin_indexes' is an empty
100
+ # array.
101
+ #
102
+ def _search_non_latin_indexes(input_string)
103
+ non_latin_indexes = []
104
+ start_index = nil
105
+
106
+ input_string.each_char.with_index do |char, idx|
107
+ if char.ord < SHARED_CHARS_LIMIT
108
+ # Skip, no action needed, as the character is shared by both
109
+ # Latin & Cyrillic charsets.
110
+ elsif char.ord >= SHARED_CHARS_LIMIT && char.ord <= ASCII_CHARS_LIMIT
111
+ # Latin character.
112
+
113
+ # If the current character is Latin and 'start_index' is set,
114
+ # it means the Cyrillic substring has ended.
115
+ if start_index
116
+ non_latin_indexes << [start_index, idx - 1]
117
+ start_index = nil
118
+ end
119
+ elsif start_index.nil?
120
+ start_index = idx
121
+ end
122
+ # Cyrillic character (or possibly other unsupported non-ASCII).
123
+ end
124
+
125
+ # Handle the last sub-string.
126
+ non_latin_indexes << [start_index, input_string.length - 1] if start_index
127
+
128
+ non_latin_indexes
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InjalidDejice
4
+ # Cyrillic char. => Latin char. KOI-7 conversion dictionary.
5
+ CYR_TO_LAT_DICT = {
6
+ "ю" => "@",
7
+ "а" => "A",
8
+ "б" => "B",
9
+ "ц" => "C",
10
+ "д" => "D",
11
+ "е" => "E",
12
+ "ф" => "F",
13
+ "г" => "G",
14
+ "х" => "H",
15
+ "и" => "I",
16
+ "й" => "J",
17
+ "к" => "K",
18
+ "л" => "L",
19
+ "м" => "M",
20
+ "н" => "N",
21
+ "о" => "O",
22
+ "п" => "P",
23
+ "я" => "Q",
24
+ "р" => "R",
25
+ "с" => "S",
26
+ "т" => "T",
27
+ "у" => "U",
28
+ "ж" => "V",
29
+ "в" => "W",
30
+ "ь" => "X",
31
+ "ы" => "Y",
32
+ "з" => "Z",
33
+ "ш" => "[",
34
+ "э" => "\\",
35
+ "щ" => "]",
36
+ "ч" => "^",
37
+ "ъ" => "_",
38
+ "Ю" => "`",
39
+ "А" => "a",
40
+ "Б" => "b",
41
+ "Ц" => "c",
42
+ "Д" => "d",
43
+ "Е" => "e",
44
+ "Ф" => "f",
45
+ "Г" => "g",
46
+ "Х" => "h",
47
+ "И" => "i",
48
+ "Й" => "j",
49
+ "К" => "k",
50
+ "Л" => "l",
51
+ "М" => "m",
52
+ "Н" => "n",
53
+ "О" => "o",
54
+ "П" => "p",
55
+ "Я" => "q",
56
+ "Р" => "r",
57
+ "С" => "s",
58
+ "Т" => "t",
59
+ "У" => "u",
60
+ "Ж" => "v",
61
+ "В" => "w",
62
+ "Ь" => "x",
63
+ "Ы" => "y",
64
+ "З" => "z",
65
+ "Ш" => "{",
66
+ "Э" => "|",
67
+ "Щ" => "}",
68
+ "Ч" => "~"
69
+ }.freeze
70
+
71
+ # Latin char. => Cyrillic char. KOI-7 conversion dictionary.
72
+ LAT_TO_CYR_DICT = CYR_TO_LAT_DICT.invert.freeze
73
+
74
+ # ASCII characters from 0 to 64 are shared by both Latin (KOI-7 N0) & Cyrillic (KOI-7 N1)
75
+ # KOI-7 code pages.
76
+ SHARED_CHARS_LIMIT = 64
77
+
78
+ # ASCII highest possible character code.
79
+ ASCII_CHARS_LIMIT = 127
80
+
81
+ # Character to use if no replacement was found in the CYR_TO_LAT_DICT dictionary.
82
+ UNKNOWN_CHAR = "?"
83
+
84
+ # Swithes encoding to the Cyrillic KOI-7 code page. Also known as "Shift Out" (SO) character.
85
+ CYR_CHAR = 0x0E.chr
86
+
87
+ # Swithes encoding to the Latin KOI-7 code page. Also known as "Shift In" (SI) character.
88
+ LATIN_CHAR = 0x0F.chr
89
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InjalidDejice
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "injalid_dejice/encoder"
4
+ require_relative "injalid_dejice/decoder"
5
+ require_relative "injalid_dejice/injalid_dejice"
6
+ require_relative "injalid_dejice/version"
7
+
8
+ #
9
+ # UTF-8 <-> KOI-7 encoder/decoder.
10
+ #
11
+ module InjalidDejice
12
+ class Error < StandardError; end
13
+
14
+ #
15
+ # Encode 'utf_string' to the KOI-7 encoding.
16
+ #
17
+ # @param [String] utf_string
18
+ # A UTF-8 string.
19
+ #
20
+ # @return [String]
21
+ # An US-ASCII string, KOI-7-compatible.
22
+ #
23
+ def self.utf_to_koi(utf_string)
24
+ Encoder.new.call(utf_string)
25
+ end
26
+
27
+ def self.koi_to_utf(koi_string)
28
+ Decoder.new.call(koi_string)
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ module InjalidDejice
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: injalid_dejice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 8bit-m8
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - you@example.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - ".vscode/launch.json"
22
+ - CHANGELOG.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - injalid_dejice.gemspec
27
+ - lib/injalid_dejice.rb
28
+ - lib/injalid_dejice/decoder.rb
29
+ - lib/injalid_dejice/encoder.rb
30
+ - lib/injalid_dejice/injalid_dejice.rb
31
+ - lib/injalid_dejice/version.rb
32
+ - sig/injalid_dejice.rbs
33
+ homepage: https://github.com/8bit-mate/injalid_dejice.rb
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ allowed_push_host: https://rubygems.org
38
+ homepage_uri: https://github.com/8bit-mate/injalid_dejice.rb
39
+ source_code_uri: https://github.com/8bit-mate/injalid_dejice.rb
40
+ changelog_uri: https://github.com/8bit-mate/injalid_dejice.rb
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 3.0.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.4.10
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: UTF-8 <-> KOI-7 encoder/decoder.
60
+ test_files: []