Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
godot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
community
godot
Commits
786fc63f
Unverified
Commit
786fc63f
authored
Jun 11, 2020
by
Rémi Verschelde
Committed by
GitHub
Jun 11, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #39261 from mrushyendra/string_padding
Enable zero padding with float specifier for format strings
parents
fcf14da8
b7d835d9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
9 deletions
+22
-9
ustring.cpp
core/ustring.cpp
+22
-9
No files found.
core/ustring.cpp
View file @
786fc63f
...
...
@@ -4143,27 +4143,40 @@ String String::sprintf(const Array &values, bool *error) const {
}
double
value
=
values
[
value_index
];
String
str
=
String
::
num
(
value
,
min_decimals
);
bool
is_negative
=
(
value
<
0
);
String
str
=
String
::
num
(
abs
(
value
),
min_decimals
);
// Pad decimals out.
str
=
str
.
pad_decimals
(
min_decimals
);
// Show sign
if
(
show_sign
&&
str
.
left
(
1
)
!=
"-"
)
{
str
=
str
.
insert
(
0
,
"+"
);
}
int
initial_len
=
str
.
length
();
// Padding
// Padding. Leave room for sign later if required.
int
pad_chars_count
=
(
is_negative
||
show_sign
)
?
min_chars
-
1
:
min_chars
;
String
pad_char
=
pad_with_zeroes
?
String
(
"0"
)
:
String
(
" "
);
if
(
left_justified
)
{
str
=
str
.
rpad
(
min_chars
);
if
(
pad_with_zeroes
)
{
return
"left justification cannot be used with zeros as the padding"
;
}
else
{
str
=
str
.
rpad
(
pad_chars_count
,
pad_char
);
}
}
else
{
str
=
str
.
lpad
(
min_chars
);
str
=
str
.
lpad
(
pad_chars_count
,
pad_char
);
}
// Add sign if needed.
if
(
show_sign
||
is_negative
)
{
String
sign_char
=
is_negative
?
"-"
:
"+"
;
if
(
left_justified
)
{
str
=
str
.
insert
(
0
,
sign_char
);
}
else
{
str
=
str
.
insert
(
pad_with_zeroes
?
0
:
str
.
length
()
-
initial_len
,
sign_char
);
}
}
formatted
+=
str
;
++
value_index
;
in_format
=
false
;
break
;
}
case
's'
:
{
// String
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment