diff options
| author | Randy Morgan (@morgan_randy) <[email protected]> | 2018-02-07 11:19:54 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-02-07 11:19:54 +0900 |
| commit | 545d77371cf81c368ccfd761e5409cbc7e141711 (patch) | |
| tree | ebf7364735aab38006fe30654e9b5b4f7a954db2 | |
| parent | b1d6c0a3ef1bd50228947f48f1034b9d42a9a405 (diff) | |
| parent | b34286f6975679e9e5eb390241eb05f95759461b (diff) | |
| download | caxlsx-545d77371cf81c368ccfd761e5409cbc7e141711.tar.gz caxlsx-545d77371cf81c368ccfd761e5409cbc7e141711.zip | |
Merge pull request #565 from ezcater/safe_frozen_sanitize
Axlsx.sanitize() uses delete() vs. delete!() for frozen strings
| -rw-r--r-- | lib/axlsx.rb | 10 | ||||
| -rw-r--r-- | test/tc_axlsx.rb | 21 |
2 files changed, 28 insertions, 3 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb index c5d26c2b..586562f6 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -136,10 +136,14 @@ module Axlsx # @param [String] str The string to process # @return [String] def self.sanitize(str) - str.delete!(CONTROL_CHARS) - str + if str.frozen? + str.delete(CONTROL_CHARS) + else + str.delete!(CONTROL_CHARS) + str + end end - + # If value is boolean return 1 or 0 # else return the value # @param [Object] value The value to process diff --git a/test/tc_axlsx.rb b/test/tc_axlsx.rb index 99832f63..3d6a2e70 100644 --- a/test/tc_axlsx.rb +++ b/test/tc_axlsx.rb @@ -79,4 +79,25 @@ class TestAxlsx < Test::Unit::TestCase assert_equal([['Z5', 'AA5', 'AB5'], ['Z6', 'AA6', 'AB6']], Axlsx::range_to_a('Z5:AB6')) end + def test_sanitize_frozen_control_strippped + needs_sanitize = "legit\x08".freeze # Backspace control char + + assert_equal(Axlsx.sanitize(needs_sanitize), 'legit', 'should strip control chars') + end + + def test_sanitize_unfrozen_control_strippped + needs_sanitize = "legit\x08" # Backspace control char + sanitized_str = Axlsx.sanitize(needs_sanitize) + + assert_equal(sanitized_str, 'legit', 'should strip control chars') + assert_equal(sanitized_str.object_id, sanitized_str.object_id, 'should preserve object') + end + + def test_sanitize_unfrozen_no_sanitize + legit_str = 'legit' + sanitized_str = Axlsx.sanitize(legit_str) + + assert_equal(sanitized_str, legit_str, 'should preserve value') + assert_equal(sanitized_str.object_id, legit_str.object_id, 'should preserve object') + end end |
