Skip to content

Commit

Permalink
Merge pull request #395 from CocoaPods/seg-consistent-string-processing
Browse files Browse the repository at this point in the history
[YAMLHelper] Implement consistent string processing
  • Loading branch information
endocrimes committed Aug 6, 2017
2 parents 6679ce3 + ca6cace commit 3ada5e7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/cocoapods-core/yaml_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def process_according_to_class(value, hash_keys_hint = nil)
case value
when Array then process_array(value)
when Hash then process_hash(value, hash_keys_hint)
when String then process_string(value)
else YAML.dump(value, :line_width => 2**31 - 1).sub(/\A---/, '').sub(/[.]{3}\s*\Z/, '')
end.strip
end
Expand Down Expand Up @@ -270,6 +271,35 @@ def sorting_string(value)
when Symbol then sorting_string(value.to_s)
when Array then sorting_string(value.first)
when Hash then value.keys.map { |key| key.to_s.downcase }.sort.first
else raise "Cannot sort #{value.inspect}"
end
end

RESOLVED_TAGS = [
'null', 'Null', 'NULL', '~', '', # resolve to null
'true', 'True', 'TRUE', 'false', 'False', 'FALSE', # bool
/[-+]?[0-9]+/, # base 10 int
/00[0-7]+/, # base 8 int
/0x[0-9a-fA-F]+/, # base 16 int
/[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?/, # float
/[-+]?\.(inf|Inf|INF)/, # infinity
/\.(nan|NaN|NAN)/, # NaN
].freeze
private_constant :RESOLVED_TAGS

RESOLVED_TAGS_PATTERN = /\A#{Regexp.union(RESOLVED_TAGS)}\z/
private_constant :RESOLVED_TAGS_PATTERN

def process_string(string)
case string
when /\A\s*\z/
string.inspect
when RESOLVED_TAGS_PATTERN
"'#{string}'"
when %r{\A\w[\w/ \(\)~<>=\.-]*\z}
string
else
string.inspect
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions spec/yaml_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ module Pod
YAMLHelper.load_string(result).should == value
end

it 'converts weird strings' do
{
'true' => "'true'",
'false' => "'false'",
'null' => "'null'",
'-1' => "'-1'",
'' => '""',
'!' => '"!"',
'~' => "'~'",
}.each do |given, expected|
converted = YAMLHelper.convert(given)
converted[0..-2].should == expected
YAMLHelper.load_string("---\n#{converted}").should == given
end
end

it 'converts a symbol' do
value = :value
result = YAMLHelper.convert(value)
Expand Down Expand Up @@ -94,6 +110,28 @@ module Pod
EOT
end

it 'converts a hash with complex keys' do
value = { 'Key' => {
"\n\t \r\t\b\r\n " => 'spaces galore',
'!abc' => 'abc',
'!ABC' => 'ABC',
'123' => '123',
"a # 'comment'?" => "a # 'comment'?",
%q('"' lotsa '"""'''" quotes) => %q('"' lotsa '"""'''" quotes),
} }
result = YAMLHelper.convert(value)
result.should == <<-EOT.strip_heredoc
Key:
"\\n\\t \\r\\t\\b\\r\\n ": spaces galore
"!abc": abc
"!ABC": ABC
"'\\"' lotsa '\\"\\"\\"'''\\" quotes": "'\\"' lotsa '\\"\\"\\"'''\\" quotes"
'123': '123'
"a # 'comment'?": "a # 'comment'?"
EOT
YAMLHelper.load_string(result).should == value
end

it 'handles nil' do
value = { 'foo' => nil }
result = YAMLHelper.convert(value)
Expand Down

0 comments on commit 3ada5e7

Please sign in to comment.