|
| 1 | +// Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/ |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-2-Clause |
| 4 | + |
| 5 | +#ifndef COMMON_NUMERIC_GROUPING_H |
| 6 | +#define COMMON_NUMERIC_GROUPING_H |
| 7 | + |
| 8 | +#include <assert.h> |
| 9 | +#include <stdbool.h> |
| 10 | +#include <stddef.h> |
| 11 | +#include <stdint.h> |
| 12 | + |
| 13 | +// Utility functions for inserting grouping characters into a number. |
| 14 | +// |
| 15 | +// The grouping string stored in LC_NUMERIC and LC_MONETARY has the |
| 16 | +// disadvantage that it encodes the grouping of a number from right to |
| 17 | +// left, whereas we would typically want to print numbers from left to |
| 18 | +// right. The last entry in the grouping string may also be repeated. |
| 19 | +// |
| 20 | +// This interface converts the grouping string to a simple state machine |
| 21 | +// that returns true or false depending on whether a grouping character |
| 22 | +// should be inserted. Example code: |
| 23 | +// |
| 24 | +// char digits[7] = "1234567"; |
| 25 | +// struct numeric_grouping ng; |
| 26 | +// size_t n = numeric_grouping_init(&ng, "\x03", sizeof(digits)); |
| 27 | +// char grouped_digits[sizeof(digits) + n]; |
| 28 | +// char *out = grouped_digits; |
| 29 | +// for (size_t i = 0; i < sizeof(digits); ++i) { |
| 30 | +// if (numeric_grouping_step(&ng)) |
| 31 | +// *out++ = ','; |
| 32 | +// *out++ = digits[i]; |
| 33 | +// } |
| 34 | +// |
| 35 | +// The grouped_digits array will now contain "1,234,567". |
| 36 | + |
| 37 | +struct numeric_grouping { |
| 38 | + const signed char *grouping; |
| 39 | + size_t steps; |
| 40 | + size_t repetitions; |
| 41 | +}; |
| 42 | + |
| 43 | +static inline size_t numeric_grouping_init(struct numeric_grouping *ng, |
| 44 | + const signed char *grouping, |
| 45 | + size_t ndigits) { |
| 46 | + if (ndigits <= 1 || grouping == NULL || *grouping == '\0') { |
| 47 | + // Number is too short, the grouping string is empty. Don't attempt |
| 48 | + // to do any grouping. |
| 49 | + ng->steps = SIZE_MAX; |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + size_t ngroupings = 0; |
| 54 | + for (;;) { |
| 55 | + assert(ndigits >= 1 && "Attempted to format short number"); |
| 56 | + assert(*grouping != '\0' && "Unexpected null byte"); |
| 57 | + if (*grouping < '\0' || ndigits <= (size_t)*grouping) { |
| 58 | + // Don't do any grouping anymore. Place all of the remaining |
| 59 | + // digits in a group. |
| 60 | + *ng = (struct numeric_grouping){ |
| 61 | + .grouping = grouping, |
| 62 | + .steps = ndigits, |
| 63 | + }; |
| 64 | + return ngroupings; |
| 65 | + } else if (grouping[1] == '\0') { |
| 66 | + // Continue to do the grouping the same way indefinitely. |
| 67 | + *ng = (struct numeric_grouping){ |
| 68 | + .grouping = grouping, |
| 69 | + .steps = (ndigits - 1) % *grouping + 1, |
| 70 | + .repetitions = (ndigits - 1) / *grouping, |
| 71 | + }; |
| 72 | + return ngroupings + ng->repetitions; |
| 73 | + } else { |
| 74 | + // Still more groups to go after this one. |
| 75 | + ndigits -= *grouping; |
| 76 | + ++grouping; |
| 77 | + ++ngroupings; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +static inline bool numeric_grouping_step(struct numeric_grouping *ng) { |
| 83 | + if (ng->steps != 0) { |
| 84 | + // Still more steps to go before we need to print a grouping character. |
| 85 | + --ng->steps; |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + // Reload the number of steps we need. |
| 90 | + if (ng->repetitions != 0) { |
| 91 | + // Repeat the same group another time. |
| 92 | + --ng->repetitions; |
| 93 | + } else { |
| 94 | + // No more repetitions of this group. Move on to the next group. |
| 95 | + --ng->grouping; |
| 96 | + } |
| 97 | + assert(*ng->grouping > 0 && "Attempted to reload zero steps"); |
| 98 | + ng->steps = *ng->grouping - 1; |
| 99 | + return true; |
| 100 | +} |
| 101 | + |
| 102 | +#endif |
0 commit comments