Skip to content

Commit

Permalink
rename 'is_valid_unquoted_string' to 'string_needs_quotes'
Browse files Browse the repository at this point in the history
  • Loading branch information
anthrotype committed Oct 1, 2018
1 parent 5744a01 commit f950375
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/openstep_plist/_test.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from .util cimport (
tounicode,
is_valid_unquoted_string_char as _is_valid_unquoted_string_char,
)
from .writer cimport is_valid_unquoted_string as _is_valid_unquoted_string
from .writer cimport string_needs_quotes as _string_needs_quotes
from cpython.unicode cimport (
PyUnicode_FromUnicode, PyUnicode_AS_UNICODE, PyUnicode_GET_SIZE,
)
Expand Down Expand Up @@ -83,6 +83,6 @@ def parse_plist_string(s, required=True):
return _parse_plist_string(&ctx.pi, required=required)


def is_valid_unquoted_string(s):
def string_needs_quotes(s):
cdef ParseContext ctx = ParseContext.fromstring(s)
return _is_valid_unquoted_string(ctx.pi.begin, len(s))
return _string_needs_quotes(ctx.pi.begin, len(s))
2 changes: 1 addition & 1 deletion src/openstep_plist/writer.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#cython: language_level=3


cdef bint is_valid_unquoted_string(const Py_UNICODE *a, Py_ssize_t length)
cdef bint string_needs_quotes(const Py_UNICODE *a, Py_ssize_t length)
14 changes: 7 additions & 7 deletions src/openstep_plist/writer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ cdef bint *VALID_UNQUOTED_CHARS = [
]


cdef bint is_valid_unquoted_string(const Py_UNICODE *a, Py_ssize_t length):
cdef bint string_needs_quotes(const Py_UNICODE *a, Py_ssize_t length):
# empty string is always quoted
if length == 0:
return False
return True

cdef:
Py_ssize_t i
Expand All @@ -74,7 +74,7 @@ cdef bint is_valid_unquoted_string(const Py_UNICODE *a, Py_ssize_t length):
# if non-ASCII or contains any invalid unquoted characters,
# we must write it with quotes
if ch > 0x7F or not VALID_UNQUOTED_CHARS[ch]:
return False
return True
elif is_number:
# check if the string could be confused with an integer or float;
# if so we write it with quotes to disambiguate its type
Expand All @@ -90,7 +90,7 @@ cdef bint is_valid_unquoted_string(const Py_UNICODE *a, Py_ssize_t length):
# if any characters not in ".0123456789", it's not a number
is_number = False

return not is_number
return is_number


cdef inline void escape_unicode(uint16_t ch, Py_UNICODE *dest):
Expand Down Expand Up @@ -294,11 +294,11 @@ cdef class Writer:
Py_ssize_t length = PyUnicode_GET_SIZE(string)
array.array dest = self.dest

if is_valid_unquoted_string(s, length):
if string_needs_quotes(s, length):
return self.write_quoted_string(s, length)
else:
array.extend_buffer(dest, <char *>s, length)
return length
else:
return self.write_quoted_string(s, length)

cdef Py_ssize_t write_short_float_repr(self, object py_float) except -1:
cdef:
Expand Down
56 changes: 28 additions & 28 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import absolute_import, unicode_literals
import openstep_plist
from openstep_plist.writer import Writer
from openstep_plist._test import is_narrow_unicode, is_valid_unquoted_string
from openstep_plist._test import is_narrow_unicode, string_needs_quotes
from io import StringIO, BytesIO
from collections import OrderedDict
import string
Expand Down Expand Up @@ -239,33 +239,33 @@ def test_dump():
@pytest.mark.parametrize(
"string, expected",
[
(string.ascii_uppercase, True),
(string.ascii_lowercase, True),
(string.ascii_uppercase, False),
(string.ascii_lowercase, False),
# digits are allowed unquoted if not in first position
("a" + string.digits, True),
(".appVersion", True),
("_private", True),
("$PWD", True),
("1zzz", True),
("192.168.1.1", True),
("0", False),
("1", False),
("2", False),
("3", False),
("4", False),
("5", False),
("6", False),
("7", False),
("8", False),
("9", False),
("", False),
("-", False),
("A-Z", False),
("hello world", False),
("\\backslash", False),
("http://github.com", False),
(random.choice(invalid_unquoted_chars), False),
("a" + string.digits, False),
(".appVersion", False),
("_private", False),
("$PWD", False),
("1zzz", False),
("192.168.1.1", False),
("0", True),
("1", True),
("2", True),
("3", True),
("4", True),
("5", True),
("6", True),
("7", True),
("8", True),
("9", True),
("", True),
("-", True),
("A-Z", True),
("hello world", True),
("\\backslash", True),
("http://github.com", True),
(random.choice(invalid_unquoted_chars), True),
],
)
def test_is_valid_unquoted_string(string, expected):
assert is_valid_unquoted_string(string) is expected
def test_string_needs_quotes(string, expected):
assert string_needs_quotes(string) is expected

0 comments on commit f950375

Please sign in to comment.