ot-rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ff6ebc3c63eb7cddc35d2a2e974d08e27328e88615a23c3c102f4165a1ea1a80
4
+ data.tar.gz: ffd46651184fe4c2d3c6e1894900965a17ac896bdd0a591caca93f3592dcfccd
5
+ SHA512:
6
+ metadata.gz: f696afed273698e5a949149bb75b30dc547469cda8e4f6339346ec6168c80eee01b78c3778ace6b1756980b17f328635ef17d267c9b4b0acd5d5ace13ea6c1e5
7
+ data.tar.gz: 12e643d93c95accba36293795c8461499c97f4ab62474b750a2031929ec2717d295f19dfa5b1c792af576ecd695eb41804329ba18aed35841422b040caa682d4
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,45 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Style/NumericPredicate:
8
+ Exclude:
9
+ - 'lib/ot/text_operation.rb'
10
+
11
+ Style/SelfAssignment:
12
+ Exclude:
13
+ - 'lib/ot/text_operation.rb'
14
+
15
+ Style/StringLiterals:
16
+ Enabled: true
17
+ EnforcedStyle: double_quotes
18
+
19
+ Style/StringLiteralsInInterpolation:
20
+ Enabled: true
21
+ EnforcedStyle: double_quotes
22
+
23
+ Style/IfUnlessModifier:
24
+ Exclude:
25
+ - 'lib/ot/text_operation.rb'
26
+
27
+ Layout/LineLength:
28
+ Enabled: false
29
+
30
+ Lint/MissingCopEnableDirective:
31
+ Exclude:
32
+ - 'lib/ot/text_operation.rb'
33
+
34
+ Metrics:
35
+ Enabled: false
36
+
37
+ Naming:
38
+ Exclude:
39
+ - 'lib/ot/text_operation.rb'
40
+
41
+ Naming/FileName:
42
+ AllowedAcronyms:
43
+ - OT
44
+ Exclude:
45
+ - 'lib/ot-rb.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-11
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tomo.asleep@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ot-rb.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1.21"
11
+
12
+ gem "bump", "~> 0.10.0"
13
+ gem "debug", "~> 1.8"
14
+ gem "rspec-debug"
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Tomoya Chiba
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # ot-rb
2
+
3
+ ot-rb is a port of [ot.js](https://github.com/Operational-Transformation/ot.js) for Ruby.
4
+
5
+ TODO: Delete this and the text below, and describe your gem
6
+
7
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ot/rb`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ ## Installation
10
+
11
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ $ bundle add ot-rb
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ $ gem install ot-rb
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'ot'
25
+
26
+ operation1 = OT::TextOperation.new
27
+ operation1.retain(1)
28
+ operation1.delete("o")
29
+ operation1.insert("i")
30
+ operation1.retain(2)
31
+
32
+ operation1.apply("hoge") # => "hige"
33
+
34
+ operation2 = OT::TextOperation.new
35
+ operation2.delete("ho")
36
+ operation2.insert("mayu")
37
+ operation2.retain(2)
38
+
39
+ operation1t, operation2t = OT::TextOperation.transform(operation1, operation2)
40
+
41
+ operation2t.apply(operation1.apply("hoge")) # => "mayuige"
42
+ operation1t.apply(operation2.apply("hoge")) # => "mayuige"
43
+
44
+ operation1.compose(operation2t).apply("hoge") # => "mayuige"
45
+ operation2.compose(operation1t).apply("hoge") # => "mayuige"
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ 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).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tomoasleep/ot-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tomoasleep/ot-rb/blob/main/CODE_OF_CONDUCT.md).
57
+
58
+ ## Code of Conduct
59
+
60
+ Everyone interacting in the Ot::Rb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tomoasleep/ot-rb/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "bump/tasks"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ require "rubocop/rake_task"
10
+
11
+ RuboCop::RakeTask.new
12
+
13
+ task default: %i[spec rubocop]
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OT
4
+ class TextOperation
5
+ module Builder
6
+ module_function
7
+
8
+ # @param base [String]
9
+ # @param from [Integer]
10
+ # @param delete [Integer, String, nil]
11
+ # @param insert [String, nil]
12
+ # @return [TextOperation]
13
+ def replace(base, from:, delete: nil, insert: nil)
14
+ delete_length = delete.is_a?(String) ? delete.length : delete || 0
15
+ insert_length = insert.is_a?(String) ? insert.length : insert || 0
16
+
17
+ to = TextOperation.new
18
+
19
+ to.retain(from) if from.positive?
20
+
21
+ to.delete(delete_length) if delete_length.positive?
22
+
23
+ to.insert(insert) if insert_length.positive?
24
+
25
+ remain = base.length - from - delete_length
26
+
27
+ to.retain(remain) if remain.positive?
28
+
29
+ to
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OT
4
+ class TextOperation
5
+ # Simplify iteration of ops in QiitaTeam::OT::TextOperation.
6
+ #
7
+ # This class is not a part of original ot.js but is used to port ot.js to Ruby.
8
+ class Iterator
9
+ # @return [QiitaTeam::OT::TextOperation]
10
+ attr_reader :text_operation
11
+
12
+ # @param [QiitaTeam::OT::TextOperation] text_operation
13
+ def initialize(text_operation)
14
+ @text_operation = text_operation
15
+ end
16
+
17
+ # @return [String, Integer, nil]
18
+ def next
19
+ enumerator.next
20
+ rescue StopIteration
21
+ nil
22
+ end
23
+
24
+ private
25
+
26
+ # @return [Enumerator<String, Integer>]
27
+ def enumerator
28
+ @enumerator ||= text_operation.ops.to_enum
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,382 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This is a port of ot.js to Ruby.
4
+ # https://github.com/Operational-Transformation/ot.js/blob/v0.0.15/lib/text-operation.js
5
+
6
+ # Copyright (c) 2012-2014 Tim Baumann, http://timbaumann.info
7
+ # Released under the MIT license
8
+ # https://opensource.org/licenses/mit-license.php
9
+
10
+ module OT
11
+ class TextOperation
12
+ # @return [Array<String, Integer>]
13
+ attr_reader :ops
14
+
15
+ # @return [Integer]
16
+ attr_reader :base_length
17
+
18
+ # @return [Integer]
19
+ attr_reader :target_length
20
+
21
+ def initialize
22
+ @ops = []
23
+ @base_length = 0
24
+ @target_length = 0
25
+ end
26
+
27
+ class << self
28
+ # @param op [Integer, String]
29
+ def retain?(op)
30
+ op.is_a?(Integer) && op > 0
31
+ end
32
+
33
+ # @param op [Integer, String]
34
+ def insert?(op)
35
+ op.is_a?(String)
36
+ end
37
+
38
+ # @param op [Integer, String]
39
+ def delete?(op)
40
+ op.is_a?(Integer) && op < 0
41
+ end
42
+ end
43
+
44
+ # @param n [Integer]
45
+ # @return [TextOperation]
46
+ def retain(n)
47
+ raise ArgumentError, "retain expects an integer" unless n.is_a?(Integer)
48
+ return self if n == 0
49
+
50
+ @base_length += n
51
+ @target_length += n
52
+
53
+ if self.class.retain?(ops.last)
54
+ ops[-1] += n
55
+ else
56
+ ops.push(n)
57
+ end
58
+
59
+ self
60
+ end
61
+
62
+ # @param str [String]
63
+ # @return [TextOperation]
64
+ def insert(str)
65
+ raise ArgumentError, "insert expects a string" unless str.is_a?(String)
66
+ return self if str.empty?
67
+
68
+ @target_length += str.length
69
+
70
+ if self.class.insert?(ops[-1])
71
+ ops[-1] += str
72
+ elsif self.class.delete?(ops[-1])
73
+ # It doesn't matter when an operation is applied whether the operation
74
+ # is delete(3), insert("something") or insert("something"), delete(3).
75
+ # Here we enforce that in this case, the insert op always comes first.
76
+ # This makes all operations that have the same effect when applied to
77
+ # a document of the right length equal in respect to the `equals` method.
78
+ if self.class.insert?(ops[-2])
79
+ ops[-2] += str
80
+ else
81
+ ops.push(ops[-1])
82
+ ops[-2] = str
83
+ end
84
+ else
85
+ ops.push(str)
86
+ end
87
+
88
+ self
89
+ end
90
+
91
+ # @param n [Integer, String]
92
+ # @return [TextOperation]
93
+ def delete(n)
94
+ n = n.length if n.is_a?(String)
95
+ raise ArgumentError, "delete expects an integer or a string" unless n.is_a?(Integer)
96
+ return self if n == 0
97
+
98
+ n = -n if n > 0
99
+ @base_length -= n
100
+
101
+ if self.class.delete?(ops[-1])
102
+ ops[-1] += n
103
+ else
104
+ ops.push(n)
105
+ end
106
+
107
+ self
108
+ end
109
+
110
+ def noop?
111
+ ops.empty? || (ops.length == 1 && self.class.retain?(ops[0]))
112
+ end
113
+
114
+ # @return [String]
115
+ def to_s
116
+ ops.map do |op|
117
+ if self.class.retain?(op)
118
+ "retain #{op}"
119
+ elsif self.class.insert?(op)
120
+ "insert '#{op}'"
121
+ else
122
+ "delete #{-op}"
123
+ end
124
+ end.join(", ")
125
+ end
126
+
127
+ # Apply an operation to a string, returning a new string. Throws an error if
128
+ # there's a mismatch between the input string and the operation.
129
+ #
130
+ # @param text [String]
131
+ # @return [String]
132
+ def apply(text)
133
+ if text.length != base_length
134
+ raise ArgumentError, "The operation's base length must be equal to the string's length."
135
+ end
136
+
137
+ new_text = []
138
+ pos = 0
139
+
140
+ ops.each do |op|
141
+ if self.class.retain?(op)
142
+ new_text.push(text[pos...(pos + op)])
143
+ pos += op
144
+ elsif self.class.insert?(op)
145
+ new_text.push(op)
146
+ else
147
+ pos -= op
148
+ end
149
+ end
150
+
151
+ raise ArgumentError, "The operation didn't operate on the whole string." if pos != text.length
152
+
153
+ new_text.join
154
+ end
155
+
156
+ # Compose merges two consecutive operations into one operation, that
157
+ # preserves the changes of both. Or, in other words, for each input string S
158
+ # and a pair of consecutive operations A and B,
159
+ # apply(apply(S, A), B) = apply(S, compose(A, B)) must hold.
160
+ #
161
+ # @param operation2 [TextOperation]
162
+ # @return [TextOperation]
163
+ def compose(operation2)
164
+ operation1 = self
165
+ raise ArgumentError, "The base length of the second operation has to be the target length of the first operation" if operation1.target_length != operation2.base_length
166
+
167
+ operation = TextOperation.new
168
+
169
+ ops1 = TextOperation::Iterator.new(operation1)
170
+ ops2 = TextOperation::Iterator.new(operation2)
171
+
172
+ op1 = ops1.next
173
+ op2 = ops2.next
174
+
175
+ loop do
176
+ # Dispatch on the type of op1 and op2
177
+ if op1.nil? && op2.nil?
178
+ # end condition: both ops1 and ops2 have been processed
179
+ break
180
+ end
181
+
182
+ if self.class.delete?(op1)
183
+ operation.delete(op1)
184
+ op1 = ops1.next
185
+ next
186
+ end
187
+ if self.class.insert?(op2)
188
+ operation.insert(op2)
189
+ op2 = ops2.next
190
+ next
191
+ end
192
+
193
+ raise "Cannot compose operations: first operation is too short." if op1.nil?
194
+ raise "Cannot compose operations: first operation is too long." if op2.nil?
195
+
196
+ if self.class.retain?(op1) && self.class.retain?(op2)
197
+ if op1 > op2
198
+ operation.retain(op2)
199
+ op1 = op1 - op2
200
+ op2 = ops2.next
201
+ elsif op1 == op2
202
+ operation.retain(op1)
203
+ op1 = ops1.next
204
+ op2 = ops2.next
205
+ else
206
+ operation.retain(op1)
207
+ op2 = op2 - op1
208
+ op1 = ops1.next
209
+ end
210
+ elsif self.class.insert?(op1) && self.class.delete?(op2)
211
+ if op1.length > -op2
212
+ op1 = op1.slice(-op2..)
213
+ op2 = ops2.next
214
+ elsif op1.length == -op2
215
+ op1 = ops1.next
216
+ op2 = ops2.next
217
+ else
218
+ op2 = op2 + op1.length
219
+ op1 = ops1.next
220
+ end
221
+ elsif self.class.insert?(op1) && self.class.retain?(op2)
222
+ if op1.length > op2
223
+ operation.insert(op1.slice(0..(op2 - 1)))
224
+ op1 = op1.slice(op2..)
225
+ op2 = ops2.next
226
+ elsif op1.length == op2
227
+ operation.insert(op1)
228
+ op1 = ops1.next
229
+ op2 = ops2.next
230
+ else
231
+ operation.insert(op1)
232
+ op2 = op2 - op1.length
233
+ op1 = ops1.next
234
+ end
235
+ elsif self.class.retain?(op1) && self.class.delete?(op2)
236
+ if op1 > -op2
237
+ operation.delete(op2)
238
+ op1 = op1 + op2
239
+ op2 = ops2.next
240
+ elsif op1 == -op2
241
+ operation.delete(op2)
242
+ op1 = ops1.next
243
+ op2 = ops2.next
244
+ else
245
+ operation.delete(op1)
246
+ op2 = op2 + op1
247
+ op1 = ops1.next
248
+ end
249
+ else
250
+ raise "This shouldn't happen: op1: #{op1.inspect}, op2: #{op2.inspect}"
251
+ end
252
+ end
253
+
254
+ operation
255
+ end
256
+
257
+ # Transform takes two operations A and B that happened concurrently and
258
+ # produces two operations A' and B' (in an array) such that
259
+ # `apply(apply(S, A), B') = apply(apply(S, B), A')`. This function is the
260
+ # heart of OT.
261
+ #
262
+ # @param operation1 [TextOperation]
263
+ # @param operation2 [TextOperation]
264
+ # @return [(TextOperation, TextOperation))]
265
+ def self.transform(operation1, operation2)
266
+ raise ArgumentError, "Both operations have to have the same base length." if operation1.base_length != operation2.base_length
267
+
268
+ operation1_prime = TextOperation.new
269
+ operation2_prime = TextOperation.new
270
+
271
+ ops1 = TextOperation::Iterator.new(operation1)
272
+ ops2 = TextOperation::Iterator.new(operation2)
273
+
274
+ op1 = ops1.next
275
+ op2 = ops2.next
276
+
277
+ loop do
278
+ # At every iteration of the loop, the imaginary cursor that both
279
+ # operation1 and operation2 have that operates on the input string must
280
+ # have the same position in the input string.
281
+
282
+ if op1.nil? && op2.nil?
283
+ # end condition: both ops1 and ops2 have been processed
284
+ break
285
+ end
286
+
287
+ # next two cases: one or both ops are insert ops
288
+ # => insert the string in the corresponding prime operation, skip it in
289
+ # the other one. If both op1 and op2 are insert ops, prefer op1.
290
+ if insert?(op1)
291
+ operation1_prime.insert(op1)
292
+ operation2_prime.retain(op1.length)
293
+
294
+ op1 = ops1.next
295
+ next
296
+ end
297
+ if insert?(op2)
298
+ operation1_prime.retain(op2.length)
299
+ operation2_prime.insert(op2)
300
+
301
+ op2 = ops2.next
302
+ next
303
+ end
304
+
305
+ raise "Cannot compose operations: first operation is too short." if op1.nil?
306
+ raise "Cannot compose operations: first operation is too long." if op2.nil?
307
+
308
+ minl = nil
309
+ if retain?(op1) && retain?(op2)
310
+ # Simple case: retain/retain
311
+ if op1 > op2
312
+ minl = op2
313
+ op1 = op1 - op2
314
+ op2 = ops2.next
315
+ elsif op1 == op2
316
+ minl = op2
317
+ op1 = ops1.next
318
+ op2 = ops2.next
319
+ else
320
+ minl = op1
321
+ op2 = op2 - op1
322
+ op1 = ops1.next
323
+ end
324
+ operation1_prime.retain(minl)
325
+ operation2_prime.retain(minl)
326
+ elsif delete?(op1) && delete?(op2)
327
+ # Both operations delete the same string at the same position. We don't
328
+ # need to produce any operations, we just skip over the delete ops and
329
+ # handle the case that one operation deletes more than the other.
330
+ if -op1 > -op2
331
+ op1 = op1 - op2
332
+ op2 = ops2.next
333
+ elsif op1 == op2
334
+ op1 = ops1.next
335
+ op2 = ops2.next
336
+ else
337
+ op2 = op2 - op1
338
+ op1 = ops1.next
339
+ end
340
+ # next two cases: delete/retain and retain/delete
341
+ elsif delete?(op1) && retain?(op2)
342
+ if -op1 > op2
343
+ minl = op2
344
+ op1 = op1 + op2
345
+ op2 = ops2.next
346
+ elsif -op1 == op2
347
+ minl = op2
348
+ op1 = ops1.next
349
+ op2 = ops2.next
350
+ else
351
+ minl = -op1
352
+ op2 = op2 + op1
353
+ op1 = ops1.next
354
+ end
355
+ operation1_prime.delete(minl)
356
+ elsif retain?(op1) && delete?(op2)
357
+ if op1 > -op2
358
+ minl = -op2
359
+ op1 = op1 + op2
360
+ op2 = ops2.next
361
+ elsif op1 == -op2
362
+ minl = op1
363
+ op1 = ops1.next
364
+ op2 = ops2.next
365
+ else
366
+ minl = op1
367
+ op2 = op2 + op1
368
+ op1 = ops1.next
369
+ end
370
+ operation2_prime.delete(minl)
371
+ else
372
+ raise "The two operations aren't compatible"
373
+ end
374
+ end
375
+
376
+ [operation1_prime, operation2_prime]
377
+ end
378
+ end
379
+ end
380
+
381
+ require_relative "text_operation/build_dsl"
382
+ require_relative "text_operation/iterator"
data/lib/ot/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OT
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ot-rb.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ot"
data/lib/ot.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OT
4
+ end
5
+
6
+ require_relative "ot/version"
7
+ require_relative "ot/text_operation"
data/sig/ot/rb.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module OT
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: ot-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomoya Chiba
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The ruby port of ot.js
14
+ email:
15
+ - tomo.asleep@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - LICENSE.md
26
+ - README.md
27
+ - Rakefile
28
+ - lib/ot-rb.rb
29
+ - lib/ot.rb
30
+ - lib/ot/text_operation.rb
31
+ - lib/ot/text_operation/build_dsl.rb
32
+ - lib/ot/text_operation/iterator.rb
33
+ - lib/ot/version.rb
34
+ - sig/ot/rb.rbs
35
+ homepage: https://github.com/tomoasleep/ot-rb
36
+ licenses: []
37
+ metadata:
38
+ homepage_uri: https://github.com/tomoasleep/ot-rb
39
+ source_code_uri: https://github.com/tomoasleep/ot-rb
40
+ changelog_uri: https://github.com/tomoasleep/ot-rb/blob/master/CHANGELOG.md
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: The ruby port of ot.js
60
+ test_files: []