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

Fix #354: Improve length check for string literal properties #355

Merged
merged 1 commit into from
Jan 6, 2025
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
4 changes: 2 additions & 2 deletions nml/actions/action0.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,13 @@ def apply_threshold(value):
if "string_literal" in prop_info and (
isinstance(value, expression.StringLiteral) or prop_info["string_literal"] != 4
):
# Parse non-string exprssions just like integers. User will have to take care of proper value.
# Parse non-string expressions just like integers. User will have to take care of proper value.
# This can be used to set a label (=string of length 4) to the value of a parameter.
if not isinstance(value, expression.StringLiteral):
raise generic.ScriptError(
"Value for property {:d} must be a string literal".format(prop_info["num"]), value.pos
)
if len(value.value) != prop_info["string_literal"]:
if grfstrings.get_string_size(value.value, False, True) != prop_info["string_literal"]:
raise generic.ScriptError(
"Value for property {:d} must be of length {:d}".format(
prop_info["num"], prop_info["string_literal"]
Expand Down
4 changes: 2 additions & 2 deletions nml/expression/string_literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
with NML; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""

from nml import generic
from nml import generic, grfstrings

from .base_expression import Expression, Type

Expand All @@ -37,7 +37,7 @@ def __str__(self):
return '"{}"'.format(self.value)

def write(self, file, size):
assert len(self.value) == size
assert grfstrings.get_string_size(self.value, False, True) == size
file.print_string(self.value, final_zero=False, force_ascii=True)

def reduce(self, id_dicts=None, unknown_id_fatal=True):
Expand Down
Loading