Skip to content
Snippets Groups Projects
Commit c7c06373 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Move separate_thousands() to str_util so it can be used elsewhere, later

Seems a useful function to have on hand. Especially now that it's "fixed"
(will convert "1234" to "1,234").
parent 68e49672
No related branches found
No related tags found
No related merge requests found
Pipeline #5425 passed
...@@ -33,27 +33,6 @@ ...@@ -33,27 +33,6 @@
#define SOCKLIB_DESC NULL #define SOCKLIB_DESC NULL
#endif #endif
static char* separate_thousands(const char* src, char *dest, size_t maxlen, char sep)
{
if(strlen(src) * 1.3 > maxlen)
return (char*)src;
const char* tail = src;
while(*tail && IS_DIGIT(*tail))
tail++;
if(tail == src)
return (char*)src;
size_t digits = tail - src;
char* d = dest;
for(size_t i = 0; i < digits; d++, i++) {
*d = src[i];
if(i + 3 < digits && (digits - (i + 1)) % 3 == 0)
*(++d) = sep;
}
*d = 0;
strcpy(d, tail);
return dest;
}
struct atcode_format { struct atcode_format {
int disp_len; int disp_len;
enum { enum {
......
...@@ -712,3 +712,23 @@ char* utf8_to_cp437_inplace(char* str) ...@@ -712,3 +712,23 @@ char* utf8_to_cp437_inplace(char* str)
,/* decode error char: */CP437_INVERTED_EXCLAMATION_MARK); ,/* decode error char: */CP437_INVERTED_EXCLAMATION_MARK);
} }
char* separate_thousands(const char* src, char *dest, size_t maxlen, char sep)
{
if(strlen(src) * 1.3 > maxlen)
return (char*)src;
const char* tail = src;
while(*tail && IS_DIGIT(*tail))
tail++;
if(tail == src)
return (char*)src;
size_t digits = tail - src;
char* d = dest;
for(size_t i = 0; i < digits; d++, i++) {
*d = src[i];
if(i + 3 < digits && (digits - (i + 1)) % 3 == 0)
*(++d) = sep;
}
*d = 0;
strcpy(d, tail);
return dest;
}
...@@ -63,6 +63,7 @@ DLLEXPORT uint32_t str_to_bits(uint32_t currval, const char *str); ...@@ -63,6 +63,7 @@ DLLEXPORT uint32_t str_to_bits(uint32_t currval, const char *str);
DLLEXPORT bool str_has_ctrl(const char*); DLLEXPORT bool str_has_ctrl(const char*);
DLLEXPORT bool str_is_ascii(const char*); DLLEXPORT bool str_is_ascii(const char*);
DLLEXPORT char * utf8_to_cp437_inplace(char* str); DLLEXPORT char * utf8_to_cp437_inplace(char* str);
DLLEXPORT char * separate_thousands(const char* src, char *dest, size_t maxlen, char sep);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment