MCPcopy Create free account
hub / github.com/numpy/numpy / ensure_decimal_point

Function ensure_decimal_point

numpy/core/src/common/numpyos.c:116–168  ·  view source on GitHub ↗

* Ensure that buffer has a decimal point in it. The decimal point * will not be in the current locale, it will always be '.' */

Source from the content-addressed store, hash-verified

114 * will not be in the current locale, it will always be '.'
115 */
116static void
117ensure_decimal_point(char* buffer, size_t buf_size)
118{
119 int insert_count = 0;
120 char* chars_to_insert;
121
122 /* search for the first non-digit character */
123 char *p = buffer;
124 if (*p == '-' || *p == '+')
125 /*
126 * Skip leading sign, if present. I think this could only
127 * ever be '-', but it can't hurt to check for both.
128 */
129 ++p;
130 while (*p && isdigit(Py_CHARMASK(*p))) {
131 ++p;
132 }
133 if (*p == '.') {
134 if (isdigit(Py_CHARMASK(*(p+1)))) {
135 /*
136 * Nothing to do, we already have a decimal
137 * point and a digit after it.
138 */
139 }
140 else {
141 /*
142 * We have a decimal point, but no following
143 * digit. Insert a zero after the decimal.
144 */
145 ++p;
146 chars_to_insert = "0";
147 insert_count = 1;
148 }
149 }
150 else {
151 chars_to_insert = ".0";
152 insert_count = 2;
153 }
154 if (insert_count) {
155 size_t buf_len = strlen(buffer);
156 if (buf_len + insert_count + 1 >= buf_size) {
157 /*
158 * If there is not enough room in the buffer
159 * for the additional text, just skip it. It's
160 * not worth generating an error over.
161 */
162 }
163 else {
164 memmove(p + insert_count, p, buffer + strlen(buffer) - p + 1);
165 memcpy(p, chars_to_insert, insert_count);
166 }
167 }
168}
169
170/* see FORMATBUFLEN in unicodeobject.c */
171#define FLOAT_FORMATBUFLEN 120

Callers 1

fix_ascii_formatFunction · 0.85

Calls 1

isdigitFunction · 0.85

Tested by

no test coverage detected