Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit f565947

Browse files
Merge pull request #7419 from livecodepanos/bugfix-22868
[[ Bug 22868 ]] Ensure format() accepts negative length
2 parents b5cf07b + 8f062ea commit f565947

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

docs/notes/bugfix-22868.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Ensure format() function recognizes a negative format length

engine/src/exec-strings.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,13 +1075,18 @@ void MCStringsEvalFormat(MCExecContext& ctxt, MCStringRef p_format, MCValueRef*
10751075
bool t_zero_pad;
10761076
t_zero_pad = false;
10771077

1078+
bool t_is_negative = false;
10781079
*dptr++ = *t_native_format++;
10791080
while (*t_native_format == '-' || *t_native_format == '#' || *t_native_format == '0'
10801081
|| *t_native_format == ' ' || *t_native_format == '+')
10811082
{
10821083
// AL-2014-11-19: [[ Bug 14059 ]] Record position of last zero.
10831084
if (*t_native_format == '0')
10841085
prefix_zero = t_native_format;
1086+
1087+
if (*t_native_format == '-')
1088+
t_is_negative = true;
1089+
10851090
*dptr++ = *t_native_format++;
10861091
}
10871092
if (isdigit((uint1)*t_native_format))
@@ -1227,9 +1232,19 @@ void MCStringsEvalFormat(MCExecContext& ctxt, MCStringRef p_format, MCValueRef*
12271232
{
12281233
// AL-2014-11-19: [[ Bug 14059 ]] Pad with zeroes if the appropriate specifier flag was used
12291234
if (t_zero_pad)
1230-
t_success = MCStringAppendFormat(*t_result, "%0*s%@", width - t_range . length, "", *t_string);
1235+
{
1236+
if (!t_is_negative)
1237+
t_success = MCStringAppendFormat(*t_result, "%0*s%@", width - t_range . length, "", *t_string);
1238+
else
1239+
t_success = MCStringAppendFormat(*t_result, "%@", *t_string);
1240+
}
12311241
else
1232-
t_success = MCStringAppendFormat(*t_result, "%*s%@", width - t_range . length, "", *t_string);
1242+
{
1243+
if (!t_is_negative)
1244+
t_success = MCStringAppendFormat(*t_result, "%*s%@", width - t_range . length, "", *t_string);
1245+
else
1246+
t_success = MCStringAppendFormat(*t_result, "%@%*s", *t_string, width - t_range . length, "");
1247+
}
12331248
}
12341249
else
12351250
t_success = MCStringAppendFormat(*t_result, "%@", *t_string);

0 commit comments

Comments
 (0)