Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canonical encoding speedup by using str.replace #410

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions securesystemslib/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@

import binascii
import calendar
import re
import datetime
import time

Expand Down Expand Up @@ -613,7 +612,7 @@ def _canonical_string_encoder(string):
A string with the canonical-encoded 'string' embedded.
"""

string = '"%s"' % re.sub(r'(["\\])', r'\\\1', string)
string = '"%s"' % string.replace('\\', '\\\\').replace('"', '\\"')

return string

Expand Down
5 changes: 5 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ def test_encode_canonical(self):

self.assertEqual('{"x":3,"y":null}', encode({"x": 3, "y": None}))

# Test condition with escaping " and \
self.assertEqual('"\\""', encode("\""))
self.assertEqual('"\\\\"', encode("\\"))
self.assertEqual('"\\\\\\""', encode("\\\""))

# Condition where 'encode()' sends the result to the callable
# 'output'.
self.assertEqual(None, encode([1, 2, 3], output))
Expand Down