Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01b8cc4

Browse files
committed
Remove trailing zeroes in path string output
1 parent 7539b61 commit 01b8cc4

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

src/_path.h

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,56 @@ char *__append_to_string(char *p, char **buffer, size_t *buffersize,
981981
return p;
982982
}
983983

984+
985+
char *__add_number(double val, const char *format, int precision,
986+
char **buffer, char *p, size_t *buffersize)
987+
{
988+
char *result;
989+
990+
#if PY_VERSION_HEX >= 0x02070000
991+
char *str;
992+
str = PyOS_double_to_string(val, format[0], precision, 0, NULL);
993+
#else
994+
char str[64];
995+
PyOS_ascii_formatd(str, 64, format, val);
996+
#endif
997+
998+
// Delete trailing zeros and decimal point
999+
char *q = str;
1000+
for (; *q != 0; ++q) {
1001+
// Find the end of the string
1002+
}
1003+
1004+
--q;
1005+
for (; q >= str && *q == '0'; --q) {
1006+
// Rewind through all the zeros
1007+
}
1008+
1009+
// If the end is a decimal qoint, delete that too
1010+
if (q >= str && *q == '.') {
1011+
--q;
1012+
}
1013+
1014+
// Truncate the string
1015+
++q;
1016+
*q = 0;
1017+
1018+
#if PY_VERSION_HEX >= 0x02070000
1019+
if ((result = __append_to_string(p, buffer, buffersize, str)) == NULL) {
1020+
PyMem_Free(str);
1021+
return NULL;
1022+
}
1023+
PyMem_Free(str);
1024+
#else
1025+
if ((result = __append_to_string(p, buffer, buffersize, str)) == NULL) {
1026+
return NULL;
1027+
}
1028+
#endif
1029+
1030+
return result;
1031+
}
1032+
1033+
9841034
template <class PathIterator>
9851035
int __convert_to_string(PathIterator &path,
9861036
int precision,
@@ -989,7 +1039,9 @@ int __convert_to_string(PathIterator &path,
9891039
char **buffer,
9901040
size_t *buffersize)
9911041
{
992-
#if PY_VERSION_HEX < 0x02070000
1042+
#if PY_VERSION_HEX >= 0x02070000
1043+
const char *format = "f";
1044+
#else
9931045
char format[64];
9941046
snprintf(format, 64, "%s.%df", "%", precision);
9951047
#endif
@@ -1031,31 +1083,10 @@ int __convert_to_string(PathIterator &path,
10311083
}
10321084

10331085
for (int i = 0; i < size; ++i) {
1034-
#if PY_VERSION_HEX >= 0x02070000
1035-
char *str;
1036-
str = PyOS_double_to_string(x[i], 'f', precision, 0, NULL);
1037-
if ((p = __append_to_string(p, buffer, buffersize, str)) == NULL) {
1038-
PyMem_Free(str);
1039-
return 1;
1040-
}
1041-
PyMem_Free(str);
1042-
if ((p = __append_to_string(p, buffer, buffersize, " ")) == NULL) return 1;
1043-
str = PyOS_double_to_string(y[i], 'f', precision, 0, NULL);
1044-
if ((p = __append_to_string(p, buffer, buffersize, str)) == NULL) {
1045-
PyMem_Free(str);
1046-
return 1;
1047-
}
1048-
PyMem_Free(str);
1049-
if ((p = __append_to_string(p, buffer, buffersize, " ")) == NULL) return 1;
1050-
#else
1051-
char str[64];
1052-
PyOS_ascii_formatd(str, 64, format, x[i]);
1053-
if ((p = __append_to_string(p, buffer, buffersize, str)) == NULL) return 1;
1086+
if ((p = __add_number(x[i], format, precision, buffer, p, buffersize)) == NULL) return 1;
10541087
if ((p = __append_to_string(p, buffer, buffersize, " ")) == NULL) return 1;
1055-
PyOS_ascii_formatd(str, 64, format, y[i]);
1056-
if ((p = __append_to_string(p, buffer, buffersize, str)) == NULL) return 1;
1088+
if ((p = __add_number(y[i], format, precision, buffer, p, buffersize)) == NULL) return 1;
10571089
if ((p = __append_to_string(p, buffer, buffersize, " ")) == NULL) return 1;
1058-
#endif
10591090
}
10601091

10611092
if (postfix) {

0 commit comments

Comments
 (0)