-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounterString.rb
executable file
·53 lines (49 loc) · 1.27 KB
/
counterString.rb
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/ruby
#
# This class generates a counterstring
#
class CounterString
attr_reader :length
def initialize(length, pip = "*")
@length = length
@pip = pip
end
# This is the same algorithm used in perlclip. Despite the frequent reverse
# operations, this has good performance because it builds the reversed
# string by appending to it.
def text
pipRev = @pip.reverse # might be more than one character
pos = @length
text = ""
while true
remaining = @length - text.length
if remaining - pos.to_s.length - pipRev.length < 0
# last pip, if needed
text << pipRev[0, remaining]
break
end
# add the pip and counter (when reversed, the pip will follow the counter)
text << pipRev << pos.to_s.reverse
pos -= pos.to_s.length + pipRev.length
redo
end
return text.reverse
end
# This algorithm is easier to understand than the text method, but the prepend
# is much slower on very large strings.
def textAltAlgorithm
string = ""
marker = @length
while (string.length < @length)
remaining = @length - string.length
toAdd = remaining.to_s + @pip
# Add a counter and pip until there's no more room
if (remaining >= toAdd.length)
string.prepend(toAdd)
else
string.prepend(@pip)
end
end
return string
end
end