Last active
April 30, 2017 04:33
-
-
Save objectx/80ec930cb48f90c7619c407f4b1be1e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <assert.h> | |
static char digits [] = "0123456789" ; | |
static char separator = ',' ; | |
static void reverse (char *start, char *last) { | |
while (start < last) { | |
char tmp = *start ; | |
*start = *last ; | |
*last = tmp ; | |
++start ; | |
--last ; | |
} | |
} | |
static char * mickeyconv (char *dst, int value, unsigned int group_size) { | |
unsigned int rem = 0 ; | |
unsigned int v ; | |
if (value < 0) { | |
v = (unsigned int)-value ; | |
} | |
else { | |
v = (unsigned int)value ; | |
} | |
int length = 0 ; | |
do { | |
rem = v % 10; | |
v /= 10 ; | |
if (length % (group_size + 1) == 0) { | |
// Note: Emitted 0th separator is nuked after reversing | |
dst [length++] = separator ; | |
} | |
dst [length++] = digits [rem] ; | |
} while (0 < v) ; | |
if (value < 0) { | |
dst [length++] = '-' ; | |
} | |
// At this point, `dst` contains a delimited digits w/ prepended superfluous separator in reverse order | |
reverse (dst, dst + length - 1) ; | |
dst [length - 1] = 0 ; // Overwrite superfluous separator. | |
return dst ; | |
} | |
static int test (const char *expected, const char *actual) { | |
if (strcmp (actual, expected) == 0) { | |
return 1 ; | |
} | |
fprintf (stderr, "Expected: \"%s\"\n", expected) ; | |
fprintf (stderr, " But got: \"%s\"\n", actual) ; | |
assert (0) ; | |
} | |
int main () { | |
char buf [256] ; | |
test ( "0", mickeyconv (buf, 0, 3)) ; | |
test ( "10", mickeyconv (buf, 10, 3)) ; | |
test ( "100", mickeyconv (buf, 100, 3)) ; | |
test ( "1,000", mickeyconv (buf, 1000, 3)) ; | |
test ( "10,000", mickeyconv (buf, 10000, 3)) ; | |
test ( "1,234,567,890", mickeyconv (buf, 1234567890, 3)) ; | |
test ( "-10", mickeyconv (buf, -10, 3)) ; | |
test ( "-100", mickeyconv (buf, -100, 3)) ; | |
test ( "-1,000", mickeyconv (buf, -1000, 3)) ; | |
test ( "-10,000", mickeyconv (buf, -10000, 3)) ; | |
test ("-1,234,567,890", mickeyconv (buf, -1234567890, 3)) ; | |
test ( "0", mickeyconv (buf, 0, 4)) ; | |
test ( "10", mickeyconv (buf, 10, 4)) ; | |
test ( "100", mickeyconv (buf, 100, 4)) ; | |
test ( "1000", mickeyconv (buf, 1000, 4)) ; | |
test ( "1,0000", mickeyconv (buf, 10000, 4)) ; | |
test ( "12,3456,7890", mickeyconv (buf, 1234567890, 4)) ; | |
test ( "-10", mickeyconv (buf, -10, 4)) ; | |
test ( "-100", mickeyconv (buf, -100, 4)) ; | |
test ( "-1000", mickeyconv (buf, -1000, 4)) ; | |
test ( "-1,0000", mickeyconv (buf, -10000, 4)) ; | |
test ("-12,3456,7890", mickeyconv (buf, -1234567890, 4)) ; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment