From 8f062ea9f99800354c371ac869420eafe80fec91 Mon Sep 17 00:00:00 2001 From: livecodepanos Date: Tue, 25 Aug 2020 16:03:06 +0300 Subject: [PATCH] [[ Bug 22868 ]] Ensure format() accepts negative length This patch ensures the format() fuction recognizes a negative format length. In this case, it appends spaces to the string, matching the LC 6.x behavior. --- docs/notes/bugfix-22868.md | 1 + engine/src/exec-strings.cpp | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 docs/notes/bugfix-22868.md diff --git a/docs/notes/bugfix-22868.md b/docs/notes/bugfix-22868.md new file mode 100644 index 00000000000..60aba0372dd --- /dev/null +++ b/docs/notes/bugfix-22868.md @@ -0,0 +1 @@ +# Ensure format() function recognizes a negative format length diff --git a/engine/src/exec-strings.cpp b/engine/src/exec-strings.cpp index 68e3a585ca7..eb7cfc48e56 100644 --- a/engine/src/exec-strings.cpp +++ b/engine/src/exec-strings.cpp @@ -1075,6 +1075,7 @@ void MCStringsEvalFormat(MCExecContext& ctxt, MCStringRef p_format, MCValueRef* bool t_zero_pad; t_zero_pad = false; + bool t_is_negative = false; *dptr++ = *t_native_format++; while (*t_native_format == '-' || *t_native_format == '#' || *t_native_format == '0' || *t_native_format == ' ' || *t_native_format == '+') @@ -1082,6 +1083,10 @@ void MCStringsEvalFormat(MCExecContext& ctxt, MCStringRef p_format, MCValueRef* // AL-2014-11-19: [[ Bug 14059 ]] Record position of last zero. if (*t_native_format == '0') prefix_zero = t_native_format; + + if (*t_native_format == '-') + t_is_negative = true; + *dptr++ = *t_native_format++; } if (isdigit((uint1)*t_native_format)) @@ -1227,9 +1232,19 @@ void MCStringsEvalFormat(MCExecContext& ctxt, MCStringRef p_format, MCValueRef* { // AL-2014-11-19: [[ Bug 14059 ]] Pad with zeroes if the appropriate specifier flag was used if (t_zero_pad) - t_success = MCStringAppendFormat(*t_result, "%0*s%@", width - t_range . length, "", *t_string); + { + if (!t_is_negative) + t_success = MCStringAppendFormat(*t_result, "%0*s%@", width - t_range . length, "", *t_string); + else + t_success = MCStringAppendFormat(*t_result, "%@", *t_string); + } else - t_success = MCStringAppendFormat(*t_result, "%*s%@", width - t_range . length, "", *t_string); + { + if (!t_is_negative) + t_success = MCStringAppendFormat(*t_result, "%*s%@", width - t_range . length, "", *t_string); + else + t_success = MCStringAppendFormat(*t_result, "%@%*s", *t_string, width - t_range . length, ""); + } } else t_success = MCStringAppendFormat(*t_result, "%@", *t_string);