indented_io 0.8.0 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '097c526b0eeabff0b3be7d1483a6bac19232b803ddb630457c56ca4e7d180cea'
4
- data.tar.gz: 0a6dc9f1bdf3dbc325ea52d06298b67dd7be389297baadc85d773e4f23edd329
3
+ metadata.gz: c36c671ed19295dbc8816818028ce7d561f572c8aacc340c5d84236ce19a1e1e
4
+ data.tar.gz: 87b38c79f9ccce609c0a165438f62b2dd5194e1cb00df4b98623856f134019a1
5
5
  SHA512:
6
- metadata.gz: 516212abf50023dd89352d55f2bf2745e367cf3e0646d3f0dc1c5d6a8193cf0fdc8053beec7259e978ad7bd94791cd6e8476c9ba101e2a066b93b311d9589b84
7
- data.tar.gz: 4b8ead522fa1830c8c31888cded99ae00679737dd6f3560ebe9214190b27c38b2af2ff224437cb8c7be5e2de7fe338ca06525d5d7d980eb088f5dbc996380e6b
6
+ metadata.gz: b04768c215fa7fd6baf00c6f99915dd43bcaa36db6bb3f8c140f0d43f2cd8b721c246f6cdb1e380e0f8bf718dbcc26961dff665a1163a1eccf5a0395113b9178
7
+ data.tar.gz: 8d689f7775d01772ece81d43cc8100d8e7e484c406e61ac84a84b0204a4708cd7436477d8af12c1c3ed3a28b8acfb6938ec24df3f7b9c8b41e9628a6ebe7396b
data/README.md CHANGED
@@ -11,9 +11,9 @@ adds to the previous indendation
11
11
  ```ruby
12
12
  require 'indented_io'
13
13
 
14
- puts "Not indented"
15
- indent { puts "Indented one level" }
16
- indent(2, "* ").puts "Indented two levels"
14
+ puts 'Not indented'
15
+ indent { puts 'Indented one level' }
16
+ indent(2, '* ').puts 'Indented two levels'
17
17
  ```
18
18
 
19
19
  outputs
@@ -32,9 +32,9 @@ If given a block, the block will be called with the IndentedIO object as
32
32
  argument:
33
33
 
34
34
  ```ruby
35
- $stdout.puts "Not indented"
36
- $stdout.indent.puts "Indented"
37
- $stdout.indent { |f| f.puts "Indented" }
35
+ $stdout.puts 'Not indented'
36
+ $stdout.indent.puts 'Indented'
37
+ $stdout.indent { |f| f.puts 'Indented' }
38
38
 
39
39
  # Not indented
40
40
  # Indented
@@ -51,10 +51,10 @@ symbolic argument `:string`. If level is negative, the text will be outdented
51
51
  instead:
52
52
 
53
53
  ```ruby
54
- $stdout.puts "Not indented"
55
- $stdout.indent(2, "> ") do |f|
56
- f.indent(string: "* ").puts "Indented three levels"
57
- f.indent(-1).puts "Indented one level"
54
+ $stdout.puts 'Not indented'
55
+ $stdout.indent(2, '> ') do |f|
56
+ f.indent(string: '* ').puts 'Indented three levels'
57
+ f.indent(-1).puts 'Indented one level'
58
58
  end
59
59
 
60
60
  # Not indented
@@ -71,15 +71,15 @@ that `Kernel#print`, `Kernel#printf`, `Kernel#puts`, and `Kernel#p` will output
71
71
  indented within that block:
72
72
 
73
73
  ```ruby
74
- puts "Not indented"
74
+ puts 'Not indented'
75
75
  indent do
76
- puts "Indented one level"
76
+ puts 'Indented one level'
77
77
  indent do
78
- puts "Indented two levels"
78
+ puts 'Indented two levels'
79
79
  end
80
- puts "Indented one level"
80
+ puts 'Indented one level'
81
81
  end
82
- puts "Not indented"
82
+ puts 'Not indented'
83
83
 
84
84
  # Not indented
85
85
  # Indented one level
@@ -95,8 +95,8 @@ def legacy(phrase)
95
95
  puts phrase
96
96
  end
97
97
 
98
- legacy("Not indented")
99
- indent { legacy("Indented" }
98
+ legacy('Not indented')
99
+ indent { legacy('Indented' }
100
100
 
101
101
  # Not indented
102
102
  # Indented
@@ -111,8 +111,8 @@ that specify if the output device is at the beginning of a line and that printin
111
111
  should start with an indentation string:
112
112
 
113
113
  ```ruby
114
- indent(1, bol: true).puts "Indented"
115
- indent(1, bol: false).puts "Not indented\nIndented"
114
+ indent(1, bol: true).puts 'Indented'
115
+ indent(1, bol: false).puts 'Not indented\nIndented'
116
116
 
117
117
  # Indented
118
118
  # Not indented
@@ -124,7 +124,7 @@ indent(1, bol: false).puts "Not indented\nIndented"
124
124
  The default indentation string is defined in `IndentedIO`:
125
125
 
126
126
  ```ruby
127
- IndentedIO.default_indent = ">> "
127
+ IndentedIO.default_indent = '>> '
128
128
  indent.puts "Indented by #{IndentedIO.default_indent.inspect}"
129
129
 
130
130
  # >> Indented by ">> "
@@ -153,8 +153,8 @@ class MyIO
153
153
  end
154
154
 
155
155
  my_io = MyIO.new
156
- my_io.puts "Not indented"
157
- my_io.indent.puts "It works!"
156
+ my_io.puts 'Not indented'
157
+ my_io.indent.puts 'It works!'
158
158
 
159
159
  # Not indented
160
160
  # It works!
@@ -168,13 +168,13 @@ with a block without parameters manipulates `$stdout`, replacing it with an
168
168
  `IndentedIO` object for the duration of the block
169
169
 
170
170
  The implementation carries no overhead if it is not used but the core
171
- indentation mechanism processes characters one-by-one which is about 10-15
172
- times slower than a handwritten implementation (scripts/perf.rb is a script to
173
- check performance). It would be much faster if the inner loop was implemented
174
- in C. However, we're talking micro-seconds here: Printing without using
175
- IndentedIO range from around 0.25ms to 1ms while using IndentedIO slows it down
176
- to between 4 and 12 microseconds, so IndentedIO won't cause a noticeable slow
177
- down of your application unless you do a lot of output
171
+ indentation mechanism processes characters one-by-one which is about 7-8 times
172
+ slower than a handwritten implementation (scripts/perf.rb is a script to check
173
+ performance). It would be much faster if the inner loop was implemented in C.
174
+ However, we're talking micro-seconds here: Printing without using IndentedIO
175
+ range from around 0.25us to 1us while using IndentedIO slows it down to between
176
+ 2us and 8us, so IndentedIO won't cause a noticeable slow down of your
177
+ application unless you do a lot of output
178
178
 
179
179
  ## Installation
180
180
 
data/TODO CHANGED
@@ -7,8 +7,8 @@ TODO
7
7
  o Transmogrify instances variables and protected methods in IndentedIO to
8
8
  avoid collision with names from the underlying device
9
9
  o Explain name collision issues in the docs
10
- o Remove IndentedIO#dump
11
10
 
11
+ + Remove IndentedIO#dump
12
12
  + Change dependency in IndentedIOInterface from #print to #write and bump
13
13
  version to 0.8 as this is the last user-visible change to be made
14
14
  + Oops. #print reimplemention doesn't do this:
@@ -23,17 +23,11 @@ module IndentedIO
23
23
  # Indent and print args to the underlying device. #write has the same semantic
24
24
  # as IO#write
25
25
  def write(*args)
26
- n_bytes = 0
27
- args.join.each_char { |c|
28
- if c == "\n"
29
- self.bol = true
30
- elsif bol
31
- n_bytes += @device.write(@combined_indent)
32
- self.bol = false
33
- end
34
- n_bytes += @device.write(c)
35
- }
36
- n_bytes
26
+ str = args.join
27
+ return if str.empty?
28
+ s = (bol && str[0] != "\n" ? @combined_indent : "") + str.gsub(/\n([^\n])/m, "\n#{@combined_indent}\\1")
29
+ self.bol = (s[-1] == "\n")
30
+ @device.write(s)
37
31
  end
38
32
 
39
33
  # Indent and print args to the underlying device. #print has the same semantic
@@ -1,4 +1,4 @@
1
1
  module IndentedIO
2
2
  # Version number
3
- VERSION = '0.8.0'
3
+ VERSION = '0.8.1'
4
4
  end
@@ -7,15 +7,24 @@ RUNS = 1_000_000
7
7
  saved_stdout = $stdout
8
8
  $stdout = File.open("/dev/null", "w")
9
9
 
10
- def timeit(&block)
10
+ $base = 0.0
11
+
12
+ def timeit_impl(&block)
11
13
  t0 = Time.now
12
14
  RUNS.times {
13
15
  yield
14
16
  }
15
17
  t1 = Time.now
16
- (1000 * (t1 - t0)).round(0)
18
+ t1 - t0 - $base
17
19
  end
18
20
 
21
+ $base = timeit_impl {}
22
+
23
+ def timeit(&block)
24
+ (1000 * timeit_impl(&block)).round(0)
25
+ end
26
+
27
+
19
28
  def report(title, wo_indent, w_indent)
20
29
  times = (w_indent / wo_indent).round(1)
21
30
  if times < 1.1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indented_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-13 00:00:00.000000000 Z
11
+ date: 2018-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler