brainfuck_converter 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +13 -0
- data/README.md +2 -0
- data/lib/brainfuck_converter.rb +40 -44
- metadata +16 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43df11e483a96f0a38b849a4bcb1af18707246932373b88aa213d1ff665adaa7
|
4
|
+
data.tar.gz: 27c6d1055223d616a9c23de677e9815254b1df869a827e9231713faa08cb2c33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c47111e9e6954e8c6818411e0cd5f3d67b0ce1552033f39eebe7a474342d1aef6708b96000bb97ae03a8c4f1351cb94e2c7fa63597f7cad6499da7245ae5d19e
|
7
|
+
data.tar.gz: c78ac2adf3b2d790d1423580d442e35d17360ff39de92519b0be95ce2c95d090e6b28f5203933ff6a71782ea1d880fc7158e2cbe33a3ecfd42619d768a4b076b
|
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
2
|
+
Version 2, December 2004
|
3
|
+
|
4
|
+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
5
|
+
|
6
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
7
|
+
copies of this license document, and changing it is allowed as long
|
8
|
+
as the name is changed.
|
9
|
+
|
10
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
11
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
12
|
+
|
13
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
data/README.md
ADDED
data/lib/brainfuck_converter.rb
CHANGED
@@ -1,58 +1,56 @@
|
|
1
|
-
|
1
|
+
# Class, which includes a function to convert text into Brainfuck code.
|
2
|
+
# Furthermore, it is possible to change the character set used in Brainfuck.
|
2
3
|
class BrainfuckConverter
|
3
|
-
|
4
4
|
DEFAULT_COMMANDS = {
|
5
|
-
inc_val:
|
6
|
-
dec_val:
|
7
|
-
inc_ptr:
|
8
|
-
dec_ptr:
|
9
|
-
output:
|
10
|
-
loop_begin:
|
11
|
-
loop_end:
|
12
|
-
}
|
13
|
-
|
5
|
+
inc_val: '+',
|
6
|
+
dec_val: '-',
|
7
|
+
inc_ptr: '>',
|
8
|
+
dec_ptr: '<',
|
9
|
+
output: '.',
|
10
|
+
loop_begin: '[',
|
11
|
+
loop_end: ']'
|
12
|
+
}.freeze
|
13
|
+
|
14
14
|
attr_reader :code
|
15
15
|
attr_accessor :cmds
|
16
|
-
|
16
|
+
|
17
17
|
# Initializes a new Brainfuck Converter
|
18
18
|
#
|
19
19
|
# @param bf_cmds [Hash] Command sign. If not specified, the default command
|
20
20
|
# set under BrainfuckConverter::DEFAULT_COMMANDS will be used.
|
21
|
-
def initialize
|
21
|
+
def initialize(bf_cmds = DEFAULT_COMMANDS)
|
22
22
|
@cmds = bf_cmds
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# Converts an ASCII string to Brainfuck code.
|
26
26
|
#
|
27
27
|
# @param text [String] The text to be converted into Brainfuck code.
|
28
28
|
# @param cells_num [Integer] Number of cells to be used. All values from 0
|
29
29
|
# can be used. If not specified, 8 cells are used. Regardless of the number
|
30
30
|
# specified here, an additional cell is used.
|
31
|
-
# @return [String]
|
31
|
+
# @return [String, FalseClass]
|
32
32
|
# @example
|
33
|
-
# require
|
34
|
-
#
|
33
|
+
# require 'brainfuck_converter'
|
34
|
+
#
|
35
35
|
# con = BrainfuckConverter.new
|
36
|
-
#
|
37
|
-
# con.convert
|
38
|
-
# con.code # =>
|
39
|
-
#
|
40
|
-
# con.convert
|
41
|
-
# con.code # =>
|
42
|
-
def convert
|
43
|
-
# To output a string in Brainfuck,
|
36
|
+
#
|
37
|
+
# con.convert 'Hello World!' # => 'Hello World!'
|
38
|
+
# con.code # => '++++++++++++++[>+>++>+++>++++>+++++>++++++>++++++... too long'
|
39
|
+
#
|
40
|
+
# con.convert 'Hallo Welt!', 15 # => 'Hallo Welt!'
|
41
|
+
# con.code # => '+++++++[>+>++>+++>++++>+++++>++++++>+++++++>+++++... too long'
|
42
|
+
def convert(text, cells_num = 8)
|
43
|
+
# To output a string in Brainfuck, 'auxiliary numbers' are written into
|
44
44
|
# certain cells. If a letter is to be output, the letter is converted into
|
45
45
|
# an ASCII value and the auxiliary number that is closest to it is searched for.
|
46
46
|
# This is then adjusted so that it corresponds to the ASCII value of the letter.
|
47
47
|
# The auxiliary number or the letter is then output.
|
48
|
-
|
49
|
-
if cells_num < 1
|
50
|
-
|
51
|
-
end
|
52
|
-
|
48
|
+
|
49
|
+
return false if cells_num < 1
|
50
|
+
|
53
51
|
# Code is cleared. A new Brainfuck program is started.
|
54
|
-
@code =
|
55
|
-
|
52
|
+
@code = ''
|
53
|
+
|
56
54
|
# Calculating the auxiliary numbers
|
57
55
|
space_cells = 127 / (cells_num + 1)
|
58
56
|
@cell_values = []
|
@@ -80,38 +78,36 @@ class BrainfuckConverter
|
|
80
78
|
# because it was only used as a counter for the loop.
|
81
79
|
@pointer = 0
|
82
80
|
|
83
|
-
text.each_byte
|
81
|
+
text.each_byte do |search|
|
84
82
|
# Search for the next auxiliary number
|
85
|
-
diffs = @cell_values.map
|
83
|
+
diffs = @cell_values.map do |val|
|
86
84
|
(search - val).abs
|
87
|
-
|
85
|
+
end
|
88
86
|
nearest = diffs.index(diffs.min)
|
89
|
-
diff = search - @cell_values[nearest]
|
90
87
|
|
91
88
|
# It goes to the auxiliary number. This is changed accordingly and the
|
92
89
|
# corresponding ASCII_character is output.
|
93
90
|
move_pointer @pointer, nearest
|
94
91
|
change_value search
|
95
92
|
output_cell
|
96
|
-
|
93
|
+
end
|
97
94
|
end
|
98
|
-
|
95
|
+
|
99
96
|
protected
|
100
|
-
|
101
|
-
def move_pointer
|
97
|
+
|
98
|
+
def move_pointer(cur_pos, target_pos)
|
102
99
|
move = target_pos - cur_pos
|
103
|
-
@code += (move
|
100
|
+
@code += (move.negative? ? @cmds[:dec_ptr] : @cmds[:inc_ptr]) * move.abs
|
104
101
|
@pointer = target_pos
|
105
102
|
end
|
106
103
|
|
107
|
-
def change_value
|
104
|
+
def change_value(target_value)
|
108
105
|
change = target_value - @cell_values[@pointer]
|
109
|
-
@code += (change
|
106
|
+
@code += (change.negative? ? @cmds[:dec_val] : @cmds[:inc_val]) * change.abs
|
110
107
|
@cell_values[@pointer] = target_value
|
111
108
|
end
|
112
109
|
|
113
110
|
def output_cell
|
114
111
|
@code += @cmds[:output]
|
115
112
|
end
|
116
|
-
|
117
113
|
end
|
metadata
CHANGED
@@ -1,27 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brainfuck_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Marek
|
7
|
+
- Marek
|
8
|
+
- Küthe
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2023-04-27 00:00:00.000000000 Z
|
12
13
|
dependencies: []
|
13
14
|
description: Gem, which makes it possible to convert any ASCII characters into brainfuck
|
14
15
|
code
|
15
16
|
email: m.k@mk16.de
|
16
17
|
executables: []
|
17
18
|
extensions: []
|
18
|
-
extra_rdoc_files:
|
19
|
+
extra_rdoc_files:
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
19
22
|
files:
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
20
25
|
- lib/brainfuck_converter.rb
|
21
|
-
homepage: https://
|
26
|
+
homepage: https://codeberg.org/mark22k/BrainfuckConverter
|
22
27
|
licenses:
|
23
28
|
- WTFPL
|
24
|
-
metadata:
|
29
|
+
metadata:
|
30
|
+
source_code_uri: https://codeberg.org/mark22k/BrainfuckConverter
|
31
|
+
bug_tracker_uri: https://codeberg.org/mark22k/BrainfuckConverter/issues
|
32
|
+
rubygems_mfa_required: 'true'
|
25
33
|
post_install_message:
|
26
34
|
rdoc_options: []
|
27
35
|
require_paths:
|
@@ -30,14 +38,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
41
|
+
version: '3.1'
|
34
42
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
43
|
requirements:
|
36
44
|
- - ">="
|
37
45
|
- !ruby/object:Gem::Version
|
38
46
|
version: '0'
|
39
47
|
requirements: []
|
40
|
-
rubygems_version: 3.
|
48
|
+
rubygems_version: 3.4.12
|
41
49
|
signing_key:
|
42
50
|
specification_version: 4
|
43
51
|
summary: Gem, which makes it possible to convert any ASCII characters into brainfuck
|