Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/string.c
| Context not available. | |||||
| return strlen(str); | return strlen(str); | ||||
| } | } | ||||
| /** | |||||
| * Format numbers with thousands separators. | |||||
| * 1000 -> 1,000 | |||||
| * | |||||
| * \see http://c-faq.com/stdio/commaprint.html | |||||
| * \see http://stackoverflow.com/questions/1449805/how-to-format-a-number-from-1123456789-to-1-123-456-789-in-c | |||||
| * | |||||
| * \param num Number to format | |||||
| * \param dst Var to hold the resulting string | |||||
| * \param maxlen Maximum length for dst | |||||
| */ | |||||
| void BLI_str_number_separator(int num, char *dst, size_t maxlen) | |||||
| { | |||||
| char buffer[20]; | |||||
| char *p; | |||||
| int commas; | |||||
| int num_length; | |||||
| /* Hard coded sep, fix when we can format | |||||
campbellbarton: why is this `static`? and why an `int`? did you mean `const` ? | |||||
| * numbers with locales */ | |||||
| const char separator = ','; | |||||
| num_length = sprintf(buffer, "%d", num); | |||||
| commas = 2 - num_length % 3; | |||||
Not Done Inline Actionscan replace strcpy with memcpy using length from sprintf campbellbarton: can replace strcpy with memcpy using length from sprintf | |||||
| /* Don't change anything if num is smaller | |||||
| * than 1,000 or it doesn't fit maxlen */ | |||||
| if( num < 1000 || num <= 0 || | |||||
| maxlen < (num_length + commas + 1)) { | |||||
Not Done Inline Actionscan avoid a call to strlen() here, by getting the value from sprintf campbellbarton: can avoid a call to strlen() here, by getting the value from `sprintf` | |||||
| memcpy(dst, buffer, (size_t)num_length + 1); | |||||
| return; | |||||
| } | |||||
| /* copy into dst with separator */ | |||||
| for (p = buffer; *p != 0; p++) { | |||||
| *dst++ = *p; | |||||
| if (commas == 1) { | |||||
| *dst++ = separator; | |||||
| } | |||||
| commas = (commas + 1) % 3; | |||||
| } | |||||
| *--dst = 0; | |||||
| } | |||||
| Context not available. | |||||
why is this static? and why an int? did you mean const ?