Skip to content

Commit 0fabc6b

Browse files
committed
Merge pull request rubychan#138 from rubychan/remove-dump
Remove dump/undump functionality from Tokens
2 parents dc95284 + a6ddf46 commit 0fabc6b

File tree

5 files changed

+14
-136
lines changed

5 files changed

+14
-136
lines changed

bench/bench.rb

+14-40
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
lang = ARGV.fetch(0) do
1515
puts <<-HELP
1616
Usage:
17-
ruby bench.rb (c|ruby|dump) (null|text|tokens|count|statistic|yaml|html) [size in kB] [stream]
17+
ruby bench.rb (c|ruby) (null|text|tokens|count|statistic|yaml|html) [size in kB] [stream]
1818
1919
SIZE defaults to 100 kB (= 100,000 bytes).
2020
SIZE = 0 means the whole input.
21-
SIZE is ignored when dump is input.
2221
2322
-p generates a profile (slow! use with SIZE = 1)
2423
-o shows the output
@@ -48,10 +47,6 @@
4847
end
4948
end
5049

51-
$dump_input = lang == 'dump'
52-
$dump_output = format == 'dump'
53-
require 'coderay/helpers/gzip_simple.rb' if $dump_input
54-
5550
def here fn = nil
5651
return MYDIR unless fn
5752
File.join here, fn
@@ -66,59 +61,38 @@ def here fn = nil
6661

6762
data = nil
6863
File.open(here("#$filename." + lang), 'rb') { |f| data = f.read }
69-
if $dump_input
70-
@size = CodeRay::Tokens.load(data).text.size
71-
else
72-
raise 'Example file is empty.' if data.empty?
73-
unless @size.zero?
74-
data += data until data.size >= @size
75-
data = data[0, @size]
76-
end
77-
@size = data.size
64+
raise 'Example file is empty.' if data.empty?
65+
unless @size.zero?
66+
data += data until data.size >= @size
67+
data = data[0, @size]
7868
end
79-
69+
@size = data.size
70+
8071
options = {
8172
:tab_width => 2,
8273
# :line_numbers => :inline,
8374
:css => $style ? :style : :class,
8475
}
85-
$hl = CodeRay.encoder(format, options) unless $dump_output
76+
$hl = CodeRay.encoder(format, options)
8677
time = bm.report('CodeRay') do
8778
if $stream || true
88-
if $dump_input
89-
raise 'Can\'t stream dump.'
90-
elsif $dump_output
91-
raise 'Can\'t dump stream.'
92-
end
9379
$o = $hl.encode(data, lang, options)
9480
else
95-
if $dump_input
96-
tokens = CodeRay::Tokens.load data
97-
else
98-
tokens = CodeRay.scan(data, lang)
99-
end
81+
tokens = CodeRay.scan(data, lang)
10082
tokens.optimize! if $optimize
101-
if $dump_output
102-
$o = tokens.optimize.dump
103-
else
104-
$o = tokens.encode($hl)
105-
end
83+
$o = tokens.encode($hl)
10684
end
10785
end
108-
$file_created = here('test.' +
109-
($dump_output ? 'dump' : $hl.file_extension))
86+
$file_created = here('test.' + $hl.file_extension)
11087
File.open($file_created, 'wb') do |f|
11188
# f.write $o
11289
end
113-
Dir.chdir(here) do
114-
FileUtils.copy 'test.dump', 'example.dump' if $dump_output
115-
end
116-
90+
11791
time_real = time.real
118-
92+
11993
puts "\t%7.2f KB/s (%d.%d KB)" % [((@size / 1000.0) / time_real), @size / 1000, @size % 1000]
12094
puts $o if ARGV.include? '-o'
121-
95+
12296
end
12397
end
12498
puts "Files created: #$file_created"

lib/coderay/encoders/debug.rb

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module Encoders
99
#
1010
# You cannot fully restore the tokens information from the
1111
# output, because consecutive :space tokens are merged.
12-
# Use Tokens#dump for caching purposes.
1312
#
1413
# See also: Scanners::Debug
1514
class Debug < Encoder

lib/coderay/helpers/gzip.rb

-41
This file was deleted.

lib/coderay/tokens.rb

-45
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
module CodeRay
22

3-
# GZip library for writing and reading token dumps.
4-
autoload :GZip, coderay_path('helpers', 'gzip')
5-
63
# The Tokens class represents a list of tokens returned from
74
# a Scanner. It's actually just an Array with a few helper methods.
85
#
@@ -148,53 +145,11 @@ def split_into_parts *sizes
148145
parts
149146
end
150147

151-
# Dumps the object into a String that can be saved
152-
# in files or databases.
153-
#
154-
# The dump is created with Marshal.dump;
155-
# In addition, it is gzipped using GZip.gzip.
156-
#
157-
# The returned String object includes Undumping
158-
# so it has an #undump method. See Tokens.load.
159-
#
160-
# You can configure the level of compression,
161-
# but the default value 7 should be what you want
162-
# in most cases as it is a good compromise between
163-
# speed and compression rate.
164-
#
165-
# See GZip module.
166-
def dump gzip_level = 7
167-
dump = Marshal.dump self
168-
dump = GZip.gzip dump, gzip_level
169-
dump.extend Undumping
170-
end
171-
172148
# Return the actual number of tokens.
173149
def count
174150
size / 2
175151
end
176152

177-
# Include this module to give an object an #undump
178-
# method.
179-
#
180-
# The string returned by Tokens.dump includes Undumping.
181-
module Undumping
182-
# Calls Tokens.load with itself.
183-
def undump
184-
Tokens.load self
185-
end
186-
end
187-
188-
# Undump the object using Marshal.load, then
189-
# unzip it using GZip.gunzip.
190-
#
191-
# The result is commonly a Tokens object, but
192-
# this is not guaranteed.
193-
def Tokens.load dump
194-
dump = GZip.gunzip dump
195-
@dump = Marshal.load dump
196-
end
197-
198153
alias text_token push
199154
def begin_group kind; push :begin_group, kind end
200155
def end_group kind; push :end_group, kind end

test/unit/tokens.rb

-9
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ def test_adding_tokens
1818
assert_equal tokens.count, 4
1919
end
2020

21-
def test_dump_undump
22-
tokens = make_tokens
23-
tokens2 = nil
24-
assert_nothing_raised do
25-
tokens2 = tokens.dump.undump
26-
end
27-
assert_equal tokens, tokens2
28-
end
29-
3021
def test_to_s
3122
assert_equal 'string()', make_tokens.to_s
3223
end

0 commit comments

Comments
 (0)