quality_extensions 1.1.6 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/License +403 -0
- data/Rakefile +2 -0
- data/Readme.rdoc +2 -3
- data/VERSION +1 -0
- data/doc_include/ReleaseNotes-0.0.10 +1 -0
- data/doc_include/ReleaseNotes-0.0.12 +1 -0
- data/doc_include/ReleaseNotes-0.0.13 +2 -0
- data/doc_include/ReleaseNotes-0.0.14 +1 -0
- data/doc_include/ReleaseNotes-0.0.15 +2 -0
- data/doc_include/ReleaseNotes-0.0.16 +1 -0
- data/doc_include/ReleaseNotes-0.0.17 +1 -0
- data/doc_include/ReleaseNotes-0.0.18 +1 -0
- data/doc_include/ReleaseNotes-0.0.19 +1 -0
- data/doc_include/ReleaseNotes-0.0.20 +1 -0
- data/doc_include/ReleaseNotes-0.0.21 +7 -0
- data/doc_include/ReleaseNotes-0.0.22 +2 -0
- data/doc_include/ReleaseNotes-0.0.23 +1 -0
- data/doc_include/ReleaseNotes-0.0.24 +3 -0
- data/doc_include/ReleaseNotes-0.0.25 +3 -0
- data/doc_include/ReleaseNotes-0.0.26 +2 -0
- data/doc_include/ReleaseNotes-0.0.27 +1 -0
- data/doc_include/ReleaseNotes-0.0.28 +1 -0
- data/doc_include/ReleaseNotes-0.0.29 +3 -0
- data/doc_include/ReleaseNotes-0.0.3 +1 -0
- data/doc_include/ReleaseNotes-0.0.30 +4 -0
- data/doc_include/ReleaseNotes-0.0.31 +1 -0
- data/doc_include/ReleaseNotes-0.0.32 +1 -0
- data/doc_include/ReleaseNotes-0.0.33 +1 -0
- data/doc_include/ReleaseNotes-0.0.34 +2 -0
- data/doc_include/ReleaseNotes-0.0.35 +1 -0
- data/doc_include/ReleaseNotes-0.0.36 +1 -0
- data/doc_include/ReleaseNotes-0.0.37 +1 -0
- data/doc_include/ReleaseNotes-0.0.38 +1 -0
- data/doc_include/ReleaseNotes-0.0.39 +1 -0
- data/doc_include/ReleaseNotes-0.0.4 +2 -0
- data/doc_include/ReleaseNotes-0.0.40 +1 -0
- data/doc_include/ReleaseNotes-0.0.41 +1 -0
- data/doc_include/ReleaseNotes-0.0.42 +2 -0
- data/doc_include/ReleaseNotes-0.0.43 +2 -0
- data/doc_include/ReleaseNotes-0.0.44 +3 -0
- data/doc_include/ReleaseNotes-0.0.45 +1 -0
- data/doc_include/ReleaseNotes-0.0.46 +1 -0
- data/doc_include/ReleaseNotes-0.0.47 +1 -0
- data/doc_include/ReleaseNotes-0.0.48 +2 -0
- data/doc_include/ReleaseNotes-0.0.49 +2 -0
- data/doc_include/ReleaseNotes-0.0.5 +1 -0
- data/doc_include/ReleaseNotes-0.0.50 +1 -0
- data/doc_include/ReleaseNotes-0.0.51 +1 -0
- data/doc_include/ReleaseNotes-0.0.52 +1 -0
- data/doc_include/ReleaseNotes-0.0.53 +3 -0
- data/doc_include/ReleaseNotes-0.0.54 +1 -0
- data/doc_include/ReleaseNotes-0.0.55 +2 -0
- data/doc_include/ReleaseNotes-0.0.56 +1 -0
- data/doc_include/ReleaseNotes-0.0.57 +3 -0
- data/doc_include/ReleaseNotes-0.0.58 +1 -0
- data/doc_include/ReleaseNotes-0.0.59 +1 -0
- data/doc_include/ReleaseNotes-0.0.6 +3 -0
- data/doc_include/ReleaseNotes-0.0.60 +3 -0
- data/doc_include/ReleaseNotes-0.0.61 +1 -0
- data/doc_include/ReleaseNotes-0.0.62 +1 -0
- data/doc_include/ReleaseNotes-0.0.63 +1 -0
- data/doc_include/ReleaseNotes-0.0.64 +1 -0
- data/doc_include/ReleaseNotes-0.0.7 +1 -0
- data/doc_include/ReleaseNotes-0.0.8 +2 -0
- data/doc_include/ReleaseNotes-0.0.9 +1 -0
- data/doc_include/ReleaseNotes-0.1.1 +1 -0
- data/doc_include/ReleaseNotes-0.1.2 +1 -0
- data/doc_include/ReleaseNotes-0.1.3 +1 -0
- data/doc_include/ReleaseNotes-0.1.4 +1 -0
- data/doc_include/ReleaseNotes-0.1.5 +0 -0
- data/doc_include/ReleaseNotes-1.0.0 +1 -0
- data/doc_include/ReleaseNotes-1.0.1 +1 -0
- data/doc_include/ReleaseNotes-1.0.2 +1 -0
- data/doc_include/ReleaseNotes-1.0.3 +1 -0
- data/doc_include/ReleaseNotes-1.1.0 +1 -0
- data/doc_include/ReleaseNotes-1.1.1 +1 -0
- data/doc_include/ReleaseNotes-1.1.6 +1 -0
- data/doc_include/assert_equal_with_difference_highlighting-there_he_is.png +0 -0
- data/doc_include/assert_equal_with_difference_highlighting-wheres_waldo.png +0 -0
- data/escape/Rakefile +62 -0
- data/escape/Readme +16 -0
- data/escape/doc_include/ReleaseNotes-0.0.1 +1 -0
- data/escape/doc_include/ReleaseNotes-0.0.3 +1 -0
- data/escape/doc_include/ReleaseNotes-0.0.4 +1 -0
- data/escape/doc_include/ReleaseNotes-0.0.5 +1 -0
- data/escape/doc_include/template/qualitysmith.rb +631 -0
- data/escape/lib/escape.rb +302 -0
- data/escape/pkg/escape-0.0.5.tgz +0 -0
- data/escape/pkg/escape-0.0.5.zip +0 -0
- data/escape/pkg/escape-0.0.5/Readme +16 -0
- data/escape/pkg/escape-0.0.5/doc_include/template/qualitysmith.rb +631 -0
- data/escape/pkg/escape-0.0.5/lib/escape.rb +302 -0
- data/lib/quality_extensions.rb +3 -1
- data/lib/quality_extensions/array/average.rb +45 -0
- data/lib/quality_extensions/array/expand_ranges.rb +2 -2
- data/lib/quality_extensions/array/shell_escape.rb +2 -2
- data/lib/quality_extensions/color/gradiate.rb +5 -5
- data/lib/quality_extensions/enumerable/every.rb +2 -2
- data/lib/quality_extensions/enumerable/grep_with_index.rb +51 -0
- data/lib/quality_extensions/enumerable/grepv.rb +1 -0
- data/lib/quality_extensions/enumerable/map_with_index.rb +2 -0
- data/lib/quality_extensions/enumerable/max_by_value.rb +72 -0
- data/lib/quality_extensions/enumerable/select_while.rb +7 -2
- data/lib/quality_extensions/enumerable/select_with_index.rb +2 -2
- data/lib/quality_extensions/float/truncate.rb +2 -1
- data/lib/quality_extensions/hash/except.rb +2 -2
- data/lib/quality_extensions/hash/to_query_string.rb +6 -6
- data/lib/quality_extensions/helpers/numbers.rb +259 -0
- data/lib/quality_extensions/kernel/die.rb +2 -2
- data/lib/quality_extensions/kernel/filter_output.rb +1 -0
- data/lib/quality_extensions/kernel/require_all.rb +7 -7
- data/lib/quality_extensions/module/guard_method.rb +3 -3
- data/lib/quality_extensions/object/non.rb +10 -4
- data/lib/quality_extensions/pathname.rb +51 -3
- data/lib/quality_extensions/regexp/named_captures.rb +7 -6
- data/lib/quality_extensions/regexp/to_plain_s.rb +116 -0
- data/lib/quality_extensions/safe_method.rb +100 -0
- data/lib/quality_extensions/safe_nil.rb +87 -11
- data/lib/quality_extensions/string/chomped_lines.rb +0 -0
- data/lib/quality_extensions/string/integer_eh.rb +2 -2
- data/lib/quality_extensions/string/prefix.rb +120 -0
- data/lib/quality_extensions/string/prefix_lines.rb +4 -69
- data/lib/quality_extensions/string/safe_numeric_conversion.rb +5 -5
- data/lib/quality_extensions/string/shell_escape.rb +3 -3
- data/lib/quality_extensions/string/to_proc.rb +119 -0
- data/lib/quality_extensions/template.rb +2 -2
- data/lib/quality_extensions/test/difference_highlighting.rb +2 -2
- data/lib/quality_extensions/version.rb +3 -0
- data/quality_extensions.gemspec +21 -0
- metadata +249 -145
- data/lib/Xfind_bug_test.rb +0 -28
@@ -0,0 +1,302 @@
|
|
1
|
+
# escape.rb - escape/unescape library for several formats
|
2
|
+
#
|
3
|
+
# Copyright (C) 2006,2007 Tanaka Akira <akr@fsij.org>
|
4
|
+
#
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
6
|
+
# modification, are permitted provided that the following conditions are met:
|
7
|
+
#
|
8
|
+
# 1. Redistributions of source code must retain the above copyright notice, this
|
9
|
+
# list of conditions and the following disclaimer.
|
10
|
+
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
# this list of conditions and the following disclaimer in the documentation
|
12
|
+
# and/or other materials provided with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote products
|
14
|
+
# derived from this software without specific prior written permission.
|
15
|
+
#
|
16
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
17
|
+
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
18
|
+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
19
|
+
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
20
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
21
|
+
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
24
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
25
|
+
# OF SUCH DAMAGE.
|
26
|
+
|
27
|
+
# Escape module provides several escape functions.
|
28
|
+
# * URI
|
29
|
+
# * HTML
|
30
|
+
# * shell command
|
31
|
+
module Escape
|
32
|
+
module_function
|
33
|
+
|
34
|
+
class StringWrapper
|
35
|
+
class << self
|
36
|
+
alias new_no_dup new
|
37
|
+
def new(str)
|
38
|
+
new_no_dup(str.dup)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize(str)
|
43
|
+
@str = str
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
@str.dup
|
48
|
+
end
|
49
|
+
|
50
|
+
def inspect
|
51
|
+
"\#<#{self.class}: #{@str}>"
|
52
|
+
end
|
53
|
+
|
54
|
+
def ==(other)
|
55
|
+
other.class == self.class && @str == other.instance_variable_get(:@str)
|
56
|
+
end
|
57
|
+
alias eql? ==
|
58
|
+
|
59
|
+
def hash
|
60
|
+
@str.hash
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class ShellEscaped < StringWrapper
|
65
|
+
end
|
66
|
+
|
67
|
+
# Escape.shell_command composes
|
68
|
+
# a sequence of words to
|
69
|
+
# a single shell command line.
|
70
|
+
# All shell meta characters are quoted and
|
71
|
+
# the words are concatenated with interleaving space.
|
72
|
+
# It returns an instance of ShellEscaped.
|
73
|
+
#
|
74
|
+
# Escape.shell_command(["ls", "/"]) #=> #<Escape::ShellEscaped: ls />
|
75
|
+
# Escape.shell_command(["echo", "*"]) #=> #<Escape::ShellEscaped: echo '*'>
|
76
|
+
#
|
77
|
+
# Note that system(*command) and
|
78
|
+
# system(Escape.shell_command(command)) is roughly same.
|
79
|
+
# There are two exception as follows.
|
80
|
+
# * The first is that the later may invokes /bin/sh.
|
81
|
+
# * The second is an interpretation of an array with only one element:
|
82
|
+
# the element is parsed by the shell with the former but
|
83
|
+
# it is recognized as single word with the later.
|
84
|
+
# For example, system(*["echo foo"]) invokes echo command with an argument "foo".
|
85
|
+
# But system(Escape.shell_command(["echo foo"])) invokes "echo foo" command without arguments (and it probably fails).
|
86
|
+
def shell_command(command)
|
87
|
+
s = command.map {|word| shell_single_word(word) }.join(' ')
|
88
|
+
ShellEscaped.new_no_dup(s)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Escape.shell_single_word quotes shell meta characters.
|
92
|
+
# It returns an instance of ShellEscaped.
|
93
|
+
#
|
94
|
+
# The result string is always single shell word, even if
|
95
|
+
# the argument is "".
|
96
|
+
# Escape.shell_single_word("") returns #<Escape::ShellEscaped: ''>.
|
97
|
+
#
|
98
|
+
# Escape.shell_single_word("") #=> #<Escape::ShellEscaped: ''>
|
99
|
+
# Escape.shell_single_word("foo") #=> #<Escape::ShellEscaped: foo>
|
100
|
+
# Escape.shell_single_word("*") #=> #<Escape::ShellEscaped: '*'>
|
101
|
+
def shell_single_word(str)
|
102
|
+
if str.empty?
|
103
|
+
ShellEscaped.new_no_dup("''")
|
104
|
+
elsif %r{\A[0-9A-Za-z+,./:=@_-]+\z} =~ str
|
105
|
+
ShellEscaped.new(str)
|
106
|
+
else
|
107
|
+
result = ''
|
108
|
+
str.scan(/('+)|[^']+/) {
|
109
|
+
if $1
|
110
|
+
result << %q{\'} * $1.length
|
111
|
+
else
|
112
|
+
result << "'#{$&}'"
|
113
|
+
end
|
114
|
+
}
|
115
|
+
ShellEscaped.new_no_dup(result)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class PercentEncoded < StringWrapper
|
120
|
+
end
|
121
|
+
|
122
|
+
# Escape.uri_segment escapes URI segment using percent-encoding.
|
123
|
+
# It returns an instance of PercentEncoded.
|
124
|
+
#
|
125
|
+
# Escape.uri_segment("a/b") #=> #<Escape::PercentEncoded: a%2Fb>
|
126
|
+
#
|
127
|
+
# The segment is "/"-splitted element after authority before query in URI, as follows.
|
128
|
+
#
|
129
|
+
# scheme://authority/segment1/segment2/.../segmentN?query#fragment
|
130
|
+
#
|
131
|
+
# See RFC 3986 for details of URI.
|
132
|
+
def uri_segment(str)
|
133
|
+
# pchar - pct-encoded = unreserved / sub-delims / ":" / "@"
|
134
|
+
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
135
|
+
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
136
|
+
s = str.gsub(%r{[^A-Za-z0-9\-._~!$&'()*+,;=:@]}n) {
|
137
|
+
'%' + $&.unpack("H2")[0].upcase
|
138
|
+
}
|
139
|
+
PercentEncoded.new_no_dup(s)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Escape.uri_path escapes URI path using percent-encoding.
|
143
|
+
# The given path should be a sequence of (non-escaped) segments separated by "/".
|
144
|
+
# The segments cannot contains "/".
|
145
|
+
# It returns an instance of PercentEncoded.
|
146
|
+
#
|
147
|
+
# Escape.uri_path("a/b/c") #=> #<Escape::PercentEncoded: a/b/c>
|
148
|
+
# Escape.uri_path("a?b/c?d/e?f") #=> #<Escape::PercentEncoded: a%3Fb/c%3Fd/e%3Ff>
|
149
|
+
#
|
150
|
+
# The path is the part after authority before query in URI, as follows.
|
151
|
+
#
|
152
|
+
# scheme://authority/path#fragment
|
153
|
+
#
|
154
|
+
# See RFC 3986 for details of URI.
|
155
|
+
#
|
156
|
+
# Note that this function is not appropriate to convert OS path to URI.
|
157
|
+
def uri_path(str)
|
158
|
+
s = str.gsub(%r{[^/]+}n) { uri_segment($&) }
|
159
|
+
PercentEncoded.new_no_dup(s)
|
160
|
+
end
|
161
|
+
|
162
|
+
# :stopdoc:
|
163
|
+
def html_form_fast(pairs, sep='&')
|
164
|
+
s = pairs.map {|k, v|
|
165
|
+
# query-chars - pct-encoded - x-www-form-urlencoded-delimiters =
|
166
|
+
# unreserved / "!" / "$" / "'" / "(" / ")" / "*" / "," / ":" / "@" / "/" / "?"
|
167
|
+
# query-char - pct-encoded = unreserved / sub-delims / ":" / "@" / "/" / "?"
|
168
|
+
# query-char = pchar / "/" / "?" = unreserved / pct-encoded / sub-delims / ":" / "@" / "/" / "?"
|
169
|
+
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
170
|
+
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
171
|
+
# x-www-form-urlencoded-delimiters = "&" / "+" / ";" / "="
|
172
|
+
k = k.gsub(%r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n) {
|
173
|
+
'%' + $&.unpack("H2")[0].upcase
|
174
|
+
}
|
175
|
+
v = v.gsub(%r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n) {
|
176
|
+
'%' + $&.unpack("H2")[0].upcase
|
177
|
+
}
|
178
|
+
"#{k}=#{v}"
|
179
|
+
}.join(sep)
|
180
|
+
PercentEncoded.new_no_dup(s)
|
181
|
+
end
|
182
|
+
# :startdoc:
|
183
|
+
|
184
|
+
# Escape.html_form composes HTML form key-value pairs as a x-www-form-urlencoded encoded string.
|
185
|
+
# It returns an instance of PercentEncoded.
|
186
|
+
#
|
187
|
+
# Escape.html_form takes an array of pair of strings or
|
188
|
+
# an hash from string to string.
|
189
|
+
#
|
190
|
+
# Escape.html_form([["a","b"], ["c","d"]]) #=> #<Escape::PercentEncoded: a=b&c=d>
|
191
|
+
# Escape.html_form({"a"=>"b", "c"=>"d"}) #=> #<Escape::PercentEncoded: a=b&c=d>
|
192
|
+
#
|
193
|
+
# In the array form, it is possible to use same key more than once.
|
194
|
+
# (It is required for a HTML form which contains
|
195
|
+
# checkboxes and select element with multiple attribute.)
|
196
|
+
#
|
197
|
+
# Escape.html_form([["k","1"], ["k","2"]]) #=> #<Escape::PercentEncoded: k=1&k=2>
|
198
|
+
#
|
199
|
+
# If the strings contains characters which must be escaped in x-www-form-urlencoded,
|
200
|
+
# they are escaped using %-encoding.
|
201
|
+
#
|
202
|
+
# Escape.html_form([["k=","&;="]]) #=> #<Escape::PercentEncoded: k%3D=%26%3B%3D>
|
203
|
+
#
|
204
|
+
# The separator can be specified by the optional second argument.
|
205
|
+
#
|
206
|
+
# Escape.html_form([["a","b"], ["c","d"]], ";") #=> #<Escape::PercentEncoded: a=b;c=d>
|
207
|
+
#
|
208
|
+
# See HTML 4.01 for details.
|
209
|
+
def html_form(pairs, sep='&')
|
210
|
+
r = ''
|
211
|
+
first = true
|
212
|
+
pairs.each {|k, v|
|
213
|
+
# query-chars - pct-encoded - x-www-form-urlencoded-delimiters =
|
214
|
+
# unreserved / "!" / "$" / "'" / "(" / ")" / "*" / "," / ":" / "@" / "/" / "?"
|
215
|
+
# query-char - pct-encoded = unreserved / sub-delims / ":" / "@" / "/" / "?"
|
216
|
+
# query-char = pchar / "/" / "?" = unreserved / pct-encoded / sub-delims / ":" / "@" / "/" / "?"
|
217
|
+
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
218
|
+
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
|
219
|
+
# x-www-form-urlencoded-delimiters = "&" / "+" / ";" / "="
|
220
|
+
r << sep if !first
|
221
|
+
first = false
|
222
|
+
k.each_byte {|byte|
|
223
|
+
ch = byte.chr
|
224
|
+
if %r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n =~ ch
|
225
|
+
r << "%" << ch.unpack("H2")[0].upcase
|
226
|
+
else
|
227
|
+
r << ch
|
228
|
+
end
|
229
|
+
}
|
230
|
+
r << '='
|
231
|
+
v.each_byte {|byte|
|
232
|
+
ch = byte.chr
|
233
|
+
if %r{[^0-9A-Za-z\-\._~:/?@!\$'()*,]}n =~ ch
|
234
|
+
r << "%" << ch.unpack("H2")[0].upcase
|
235
|
+
else
|
236
|
+
r << ch
|
237
|
+
end
|
238
|
+
}
|
239
|
+
}
|
240
|
+
PercentEncoded.new_no_dup(r)
|
241
|
+
end
|
242
|
+
|
243
|
+
class HTMLEscaped < StringWrapper
|
244
|
+
end
|
245
|
+
|
246
|
+
# :stopdoc:
|
247
|
+
HTML_TEXT_ESCAPE_HASH = {
|
248
|
+
'&' => '&',
|
249
|
+
'<' => '<',
|
250
|
+
'>' => '>',
|
251
|
+
}
|
252
|
+
# :startdoc:
|
253
|
+
|
254
|
+
# Escape.html_text escapes a string appropriate for HTML text using character references.
|
255
|
+
# It returns an instance of HTMLEscaped.
|
256
|
+
#
|
257
|
+
# It escapes 3 characters:
|
258
|
+
# * '&' to '&'
|
259
|
+
# * '<' to '<'
|
260
|
+
# * '>' to '>'
|
261
|
+
#
|
262
|
+
# Escape.html_text("abc") #=> #<Escape::HTMLEscaped: abc>
|
263
|
+
# Escape.html_text("a & b < c > d") #=> #<Escape::HTMLEscaped: a & b < c > d>
|
264
|
+
#
|
265
|
+
# This function is not appropriate for escaping HTML element attribute
|
266
|
+
# because quotes are not escaped.
|
267
|
+
def html_text(str)
|
268
|
+
s = str.gsub(/[&<>]/) {|ch| HTML_TEXT_ESCAPE_HASH[ch] }
|
269
|
+
HTMLEscaped.new_no_dup(s)
|
270
|
+
end
|
271
|
+
|
272
|
+
# :stopdoc:
|
273
|
+
HTML_ATTR_ESCAPE_HASH = {
|
274
|
+
'&' => '&',
|
275
|
+
'<' => '<',
|
276
|
+
'>' => '>',
|
277
|
+
'"' => '"',
|
278
|
+
}
|
279
|
+
# :startdoc:
|
280
|
+
|
281
|
+
class HTMLAttrValue < StringWrapper
|
282
|
+
end
|
283
|
+
|
284
|
+
# Escape.html_attr_value encodes a string as a double-quoted HTML attribute using character references.
|
285
|
+
# It returns an instance of HTMLAttrValue.
|
286
|
+
#
|
287
|
+
# Escape.html_attr_value("abc") #=> #<Escape::HTMLAttrValue: "abc">
|
288
|
+
# Escape.html_attr_value("a&b") #=> #<Escape::HTMLAttrValue: "a&b">
|
289
|
+
# Escape.html_attr_value("ab&<>\"c") #=> #<Escape::HTMLAttrValue: "ab&<>"c">
|
290
|
+
# Escape.html_attr_value("a'c") #=> #<Escape::HTMLAttrValue: "a'c">
|
291
|
+
#
|
292
|
+
# It escapes 4 characters:
|
293
|
+
# * '&' to '&'
|
294
|
+
# * '<' to '<'
|
295
|
+
# * '>' to '>'
|
296
|
+
# * '"' to '"'
|
297
|
+
#
|
298
|
+
def html_attr_value(str)
|
299
|
+
s = '"' + str.gsub(/[&<>"]/) {|ch| HTML_ATTR_ESCAPE_HASH[ch] } + '"'
|
300
|
+
HTMLAttrValue.new_no_dup(s)
|
301
|
+
end
|
302
|
+
end
|
data/lib/quality_extensions.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Tyler Rick
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?:: Yes.
|
6
|
+
# Wait, so Facets has mode (http://facets.rubyforge.org/src/doc/rdoc/classes/Enumerable.html#M001253) but it doesn't have mean/average?
|
7
|
+
# Whether or not this Array#average is included, Facets ought to have an Enumerable#mean/average similar to mode that uses each iterator rather than Array#size. (Still might want to keep this version if it's more efficient for Arrays?)
|
8
|
+
#++
|
9
|
+
|
10
|
+
class Array
|
11
|
+
# Calculates the arithmetic average (mean) of the elements in the array as a <tt>Float</tt>.
|
12
|
+
# irb -> [1, 3, 3].average
|
13
|
+
# => 2.33333333333333
|
14
|
+
def average
|
15
|
+
if self.size == 0
|
16
|
+
raise ZeroDivisionError
|
17
|
+
end
|
18
|
+
self.inject(0.0) do |sum, item|
|
19
|
+
sum + item.to_f
|
20
|
+
end / self.size
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# _____ _
|
25
|
+
# |_ _|__ ___| |_
|
26
|
+
# | |/ _ \/ __| __|
|
27
|
+
# | | __/\__ \ |_
|
28
|
+
# |_|\___||___/\__|
|
29
|
+
#
|
30
|
+
=begin test
|
31
|
+
require 'test/unit'
|
32
|
+
|
33
|
+
class TheTest < Test::Unit::TestCase
|
34
|
+
def test_average
|
35
|
+
assert_equal 2, [0, 4].average
|
36
|
+
assert_equal Float, [0, 4].average.class
|
37
|
+
|
38
|
+
assert_equal 2.5, [0, 5].average
|
39
|
+
|
40
|
+
assert_raise ZeroDivisionError do
|
41
|
+
[].average
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
=end
|
@@ -8,8 +8,8 @@
|
|
8
8
|
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
|
9
9
|
require 'rubygems'
|
10
10
|
require 'facets/symbol/to_proc'
|
11
|
-
require 'facets/kernel/
|
12
|
-
|
11
|
+
require 'facets/kernel/require_relative'
|
12
|
+
require_relative '../string/shell_escape.rb'
|
13
13
|
|
14
14
|
require 'pp'
|
15
15
|
class Array
|
@@ -8,10 +8,10 @@
|
|
8
8
|
# Changes::
|
9
9
|
#++
|
10
10
|
|
11
|
-
require 'facets/kernel/
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
require 'facets/kernel/require_relative'
|
12
|
+
require_relative 'rgb'
|
13
|
+
require_relative '../numeric/diff'
|
14
|
+
require_relative '../enumerable/every'
|
15
15
|
|
16
16
|
module Enumerable
|
17
17
|
|
@@ -59,7 +59,7 @@ module Enumerable
|
|
59
59
|
next object
|
60
60
|
end
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
end
|
64
64
|
|
65
65
|
# _____ _
|
@@ -40,8 +40,8 @@ end
|
|
40
40
|
#
|
41
41
|
=begin test
|
42
42
|
require 'spec'
|
43
|
-
require 'facets/kernel/
|
44
|
-
|
43
|
+
require 'facets/kernel/require_relative'
|
44
|
+
require_relative 'enum'
|
45
45
|
|
46
46
|
describe Enumerable.instance_method(:every) do
|
47
47
|
it "without block, every(1)" do
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Tyler Rick
|
3
|
+
# Copyright:: Copyright (c) 2009, Tyler Rick
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?::
|
6
|
+
# Developer notes::
|
7
|
+
# History::
|
8
|
+
#++
|
9
|
+
|
10
|
+
#returns array with [index (of line/element that matched) , the matched line/element]
|
11
|
+
|
12
|
+
class Regexp
|
13
|
+
# not working
|
14
|
+
def debug_triple_equals(other)
|
15
|
+
p other if $debug
|
16
|
+
#p original_tripel_equals(other)
|
17
|
+
original_tripel_equals(other)
|
18
|
+
end
|
19
|
+
alias_method :original_tripel_equals, :===
|
20
|
+
alias_method :===, :debug_triple_equals
|
21
|
+
end
|
22
|
+
|
23
|
+
module Enumerable
|
24
|
+
def grep_with_index(pattern)
|
25
|
+
$debug = true
|
26
|
+
each.with_index.grep(pattern)
|
27
|
+
$debug = false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
# _____ _
|
37
|
+
# |_ _|__ ___| |_
|
38
|
+
# | |/ _ \/ __| __|
|
39
|
+
# | | __/\__ \ |_
|
40
|
+
# |_|\___||___/\__|
|
41
|
+
#
|
42
|
+
=begin test
|
43
|
+
require 'spec/autorun'
|
44
|
+
|
45
|
+
describe 'Enumerable#grep_with_index' do
|
46
|
+
it '' do
|
47
|
+
['a', 'b'].grep_with_index(/a/).should == ['a', 0]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
=end
|
51
|
+
|