Skip to content

Commit 2077971

Browse files
committed
(PUP-11841) Create mutable strings
In preparation for moving to frozen/immutable strings, explicitly create mutable strings in cases where we create a string buffer and append to it. There are multiple ways of creating a mutual string: ''.dup -'' String.new Although the latter is more verbose, I think it's better to be explicit.
1 parent 16977d9 commit 2077971

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+72
-72
lines changed

lib/puppet/application/doc.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def rdoc
151151
end
152152

153153
def other
154-
text = ""
154+
text = String.new
155155
with_contents = options[:references].length <= 1
156156
exit_code = 0
157157
require_relative '../../puppet/util/reference'

lib/puppet/face/config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
end
8282

8383
when_rendering :console do |to_be_rendered|
84-
output = ''
84+
output = String.new
8585
if to_be_rendered.keys.length > 1
8686
to_be_rendered.keys.sort.each do |setting|
8787
output << "#{setting} = #{to_be_rendered[setting]}\n"

lib/puppet/face/epp.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@
366366
end
367367

368368
def dump_parse(source, filename, options, show_filename = true)
369-
output = ""
369+
output = String.new
370370
evaluating_parser = Puppet::Pops::Parser::EvaluatingParser::EvaluatingEppParser.new
371371
begin
372372
if options[:validate]
@@ -450,7 +450,7 @@ def render_inline(epp_source, compiler, options)
450450

451451
def render_file(epp_template_name, compiler, options, show_filename, file_nbr)
452452
template_args = get_values(compiler, options)
453-
output = ""
453+
output = String.new
454454
begin
455455
if show_filename && options[:header]
456456
output << "\n" unless file_nbr == 1

lib/puppet/face/module/list.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
environment = result[:environment]
7474
modules_by_path = result[:modules_by_path]
7575

76-
output = ''
76+
output = String.new
7777

7878
warn_unmet_dependencies(environment)
7979

@@ -247,7 +247,7 @@ def list_build_tree(list, ancestors=[], parent=nil, params={})
247247
# Returns a Hash
248248
#
249249
def list_build_node(mod, parent, params)
250-
str = ''
250+
str = String.new
251251
str << (mod.forge_name ? mod.forge_name.tr('/', '-') : mod.name)
252252
str << ' (' + colorize(:cyan, mod.version ? "v#{mod.version}" : '???') + ')'
253253

lib/puppet/face/parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
end
174174

175175
def dump_parse(source, filename, options, show_filename = true)
176-
output = ""
176+
output = String.new
177177
evaluating_parser = Puppet::Pops::Parser::EvaluatingParser.new
178178
begin
179179
if options[:validate]

lib/puppet/ffi/windows/api_types.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def attach_function_private(*args)
2020

2121
class ::FFI::Pointer
2222
NULL_HANDLE = 0
23-
WCHAR_NULL = "\0\0".force_encoding('UTF-16LE').freeze
23+
WCHAR_NULL = String.new("\0\0").force_encoding('UTF-16LE').freeze
2424

2525
def self.from_string_to_wide_string(str, &block)
2626
str = Puppet::Util::Windows::String.wide_string(str)

lib/puppet/file_system/posix.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def binread(path)
1212
def compare_stream(path, stream)
1313
open(path, 0, 'rb') do |this|
1414
bsize = stream_blksize(this, stream)
15-
sa = "".force_encoding('ASCII-8BIT')
16-
sb = "".force_encoding('ASCII-8BIT')
15+
sa = String.new.force_encoding('ASCII-8BIT')
16+
sb = String.new.force_encoding('ASCII-8BIT')
1717
loop do
1818
this.read(bsize, sa)
1919
stream.read(bsize, sb)

lib/puppet/indirector/file_bucket_file/file.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def list(request)
5757
end
5858
# Setting hash's default value to [], needed by the following loop
5959
bucket = Hash.new {[]}
60-
msg = ""
60+
msg = String.new
6161
# Get all files with mtime between 'from' and 'to'
6262
Pathname.new(request.options[:bucket_path]).find { |item|
6363
if item.file? and item.basename.to_s == "paths"

lib/puppet/indirector/indirection.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def expiration
8080

8181
# Generate the full doc string.
8282
def doc
83-
text = ""
83+
text = String.new
8484

8585
text << scrub(@doc) << "\n\n" if @doc
8686

lib/puppet/module_tool.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def self.is_module_root?(path)
6969
# Builds a formatted tree from a list of node hashes containing +:text+
7070
# and +:dependencies+ keys.
7171
def self.format_tree(nodes, level = 0)
72-
str = ''
72+
str = String.new
7373
nodes.each_with_index do |node, i|
7474
last_node = nodes.length - 1 == i
7575
deps = node[:dependencies] || []

lib/puppet/network/formats.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def render(datum)
155155

156156
# Simple hash to table
157157
if datum.is_a?(Hash) && datum.keys.all? { |x| x.is_a?(String) || x.is_a?(Numeric) }
158-
output = ''
158+
output = String.new
159159
column_a = datum.empty? ? 2 : datum.map{ |k,v| k.to_s.length }.max + 2
160160
datum.sort_by { |k,v| k.to_s } .each do |key, value|
161161
output << key.to_s.ljust(column_a)
@@ -168,7 +168,7 @@ def render(datum)
168168

169169
# Print one item per line for arrays
170170
if datum.is_a? Array
171-
output = ''
171+
output = String.new
172172
datum.each do |item|
173173
output << item.to_s
174174
output << "\n"
@@ -226,7 +226,7 @@ def flatten_array(array)
226226
end
227227

228228
def construct_output(data)
229-
output = ''
229+
output = String.new
230230
data.each do |key, value|
231231
output << "#{key}=#{value}"
232232
output << "\n"

lib/puppet/network/http/memory_response.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Puppet::Network::HTTP::MemoryResponse
22
attr_reader :code, :type, :body
33

44
def initialize
5-
@body = ""
5+
@body = String.new
66
end
77

88
def respond_with(code, type, body)

lib/puppet/parameter/value_collection.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def aliasvalue(name, other)
3030
#
3131
def doc
3232
unless defined?(@doc)
33-
@doc = ""
33+
@doc = String.new
3434
unless values.empty?
3535
@doc << "Valid values are "
3636
@doc << @strings.collect do |value|

lib/puppet/parser/functions.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def self.function(name, environment = Puppet.lookup(:current_environment))
257257
def self.functiondocs(environment = Puppet.lookup(:current_environment))
258258
autoloader.delegatee.loadall(environment)
259259

260-
ret = ""
260+
ret = String.new
261261

262262
merged_functions(environment).sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |name, hash|
263263
ret << "#{name}\n#{"-" * name.to_s.length}\n"

lib/puppet/pops/loader/loader_paths.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def effective_path(typed_name, start_index_in_name)
8787

8888
def typed_name(type, name_authority, relative_path, module_name)
8989
# Module name is assumed to be included in the path and therefore not added here
90-
n = ''
90+
n = String.new
9191
unless extension.empty?
9292
# Remove extension
9393
relative_path = relative_path[0..-(extension.length+1)]
@@ -152,7 +152,7 @@ def effective_path(typed_name, start_index_in_name)
152152
end
153153

154154
def typed_name(type, name_authority, relative_path, module_name)
155-
n = ''
155+
n = String.new
156156
n << module_name unless module_name.nil?
157157
unless extension.empty?
158158
# Remove extension
@@ -248,7 +248,7 @@ def relative_path
248248
end
249249

250250
def typed_name(type, name_authority, relative_path, module_name)
251-
n = ''
251+
n = String.new
252252
n << module_name unless module_name.nil?
253253

254254
# Remove the file extension, defined as everything after the *last* dot.
@@ -350,7 +350,7 @@ def typed_name(type, name_authority, relative_path, module_name)
350350
if @init_filenames.include?(relative_path) && !(module_name.nil? || module_name.empty?)
351351
TypedName.new(type, module_name, name_authority)
352352
else
353-
n = ''
353+
n = String.new
354354
n << module_name unless module_name.nil?
355355
ext = @extensions.find { |extension| relative_path.end_with?(extension) }
356356
relative_path = relative_path[0..-(ext.length+1)]

lib/puppet/pops/lookup/explainer.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_hash
1919
end
2020

2121
def explain
22-
io = ''
22+
io = String.new
2323
dump_on(io, '', '')
2424
io
2525
end
@@ -585,7 +585,7 @@ def dump_on(io, indent, first_indent)
585585
end
586586

587587
def emit_debug_info(preamble)
588-
io = ''
588+
io = String.new
589589
io << preamble << "\n"
590590
dump_on(io, ' ', ' ')
591591
Puppet.debug(io.chomp!)

lib/puppet/pops/lookup/hiera_config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def find_line_matching(regexp, start_line = 1)
227227
line_number += 1
228228
next if line_number < start_line
229229
quote = nil
230-
stripped = ''
230+
stripped = String.new
231231
line.each_codepoint do |cp|
232232
if cp == 0x22 || cp == 0x27 # double or single quote
233233
if quote == cp

lib/puppet/pops/model/factory.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ def is_interop_rewriteable?(o)
11121112
end
11131113

11141114
def self.concat(*args)
1115-
result = ''.dup
1115+
result = String.new
11161116
args.each do |e|
11171117
if e.instance_of?(Factory) && e.model_class <= LiteralString
11181118
result << e[KEY_VALUE]

lib/puppet/pops/model/tree_dumper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def indent
2020
end
2121

2222
def format(x)
23-
result = ""
23+
result = String.new
2424
parts = format_r(x)
2525
parts.each_index do |i|
2626
if i > 0

lib/puppet/pops/parser/epp_support.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def scan(skip_leading=false)
182182
@skip_leading = skip_leading
183183

184184
return nil if scanner.eos?
185-
s = ""
185+
s = String.new
186186
until scanner.eos?
187187
part = @scanner.scan_until(/(<%)|\z/)
188188
if @skip_leading

lib/puppet/pops/parser/evaluating_parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def quote(x)
124124
# @return [String] The quoted string
125125
#
126126
def self.quote(x)
127-
escaped = '"'
127+
escaped = String.new('"')
128128
p = nil
129129
x.each_char do |c|
130130
case p

lib/puppet/pops/parser/pn_parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def consume_identifier(s)
217217

218218
def consume_string
219219
s = @pos
220-
b = ''
220+
b = String.new
221221
loop do
222222
c = next_cp
223223
case c

lib/puppet/pops/pn.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def ==(o)
1919
end
2020

2121
def to_s
22-
s = ''
22+
s = String.new
2323
format(nil, s)
2424
s
2525
end

lib/puppet/pops/serialization/json_path.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module JsonPath
1010
#
1111
# @api private
1212
def self.to_json_path(path)
13-
p = '$'
13+
p = String.new('$')
1414
path.each do |seg|
1515
if seg.nil?
1616
p << '[null]'

lib/puppet/pops/serialization/to_data_converter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def convert(value)
6363
private
6464

6565
def path_to_s
66-
s = @message_prefix || ''
66+
s = String.new(@message_prefix || '')
6767
s << JsonPath.to_json_path(@path)[1..-1]
6868
s
6969
end

lib/puppet/pops/time/timespan.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ def initialize(format, segments)
544544
end
545545

546546
def format(timespan)
547-
bld = timespan.negative? ? '-' : ''
547+
bld = String.new(timespan.negative? ? '-' : '')
548548
@segments.each { |segment| segment.append_to(bld, timespan) }
549549
bld
550550
end
@@ -574,7 +574,7 @@ def regexp
574574
end
575575

576576
def build_regexp
577-
bld = '\A-?'
577+
bld = String.new('\A-?')
578578
@segments.each { |segment| segment.append_regexp(bld) }
579579
bld << '\z'
580580
Regexp.new(bld)
@@ -612,7 +612,7 @@ def bad_format_specifier(format, start, position)
612612

613613
def append_literal(bld, codepoint)
614614
if bld.empty? || !bld.last.is_a?(Format::LiteralSegment)
615-
bld << Format::LiteralSegment.new(''.concat(codepoint))
615+
bld << Format::LiteralSegment.new(String.new.concat(codepoint))
616616
else
617617
bld.last.concat(codepoint)
618618
end

lib/puppet/pops/types/ruby_generator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def create_class(obj)
5959
if cls.nil?
6060
rp = key.resolved_parent
6161
parent_class = rp.is_a?(PObjectType) ? rp.implementation_class : Object
62-
class_def = ''
62+
class_def = String.new
6363
class_body(key, EMPTY_ARRAY, class_def)
6464
cls = Class.new(parent_class)
6565
cls.class_eval(class_def)
@@ -108,7 +108,7 @@ def module_definition(types, comment, *impl_subst)
108108
end
109109

110110
# Create class definition of all contained types
111-
bld = ''
111+
bld = String.new
112112
start_module(common_prefix, comment, bld)
113113
class_names = []
114114
names_by_prefix.each_pair do |seg_array, index_and_name_array|

lib/puppet/pops/types/string_converter.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def string_PBooleanType(val_type, val, format_map, indentation)
700700
# Performs post-processing of literals to apply width and precision flags
701701
def apply_string_flags(f, literal_str)
702702
if f.left || f.width || f.prec
703-
fmt = '%'
703+
fmt = String.new('%')
704704
fmt << '-' if f.left
705705
fmt << f.width.to_s if f.width
706706
fmt << '.' << f.prec.to_s if f.prec
@@ -852,7 +852,7 @@ def puppet_quote(str, enforce_double_quotes = false)
852852
end
853853

854854
# Assume that the string can be single quoted
855-
bld = '\''
855+
bld = String.new('\'')
856856
bld.force_encoding(str.encoding)
857857
escaped = false
858858
str.each_codepoint do |codepoint|
@@ -883,7 +883,7 @@ def puppet_quote(str, enforce_double_quotes = false)
883883
end
884884

885885
def puppet_double_quote(str)
886-
bld = '"'
886+
bld = String.new('"')
887887
str.each_codepoint do |codepoint|
888888
case codepoint
889889
when 0x09
@@ -939,7 +939,7 @@ def string_PArrayType(val_type, val, format_map, indentation)
939939

940940
case format.format
941941
when :a, :s, :p
942-
buf = ''
942+
buf = String.new
943943
if indentation.breaks?
944944
buf << "\n"
945945
buf << indentation.padding
@@ -1054,7 +1054,7 @@ def string_PHashType(val_type, val, format_map, indentation)
10541054

10551055
when :h, :s, :p
10561056
indentation = indentation.indenting(format.alt? || indentation.is_indenting?)
1057-
buf = ''
1057+
buf = String.new
10581058
if indentation.breaks?
10591059
buf << "\n"
10601060
buf << indentation.padding

0 commit comments

Comments
 (0)