Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 1 | /* stringlib: locale related helpers implementation */ |
| 2 | |
| 3 | #ifndef STRINGLIB_LOCALEUTIL_H |
| 4 | #define STRINGLIB_LOCALEUTIL_H |
| 5 | |
| 6 | #include <locale.h> |
| 7 | |
| 8 | /** |
| 9 | * _Py_InsertThousandsGrouping: |
| 10 | * @buffer: A pointer to the start of a string. |
Eric Smith | 6d7e7a7 | 2008-06-24 01:06:47 +0000 | [diff] [blame] | 11 | * @n_buffer: The length of the string. |
| 12 | * @n_digits: The number of digits in the string, in which we want |
| 13 | * to put the grouping chars. |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 14 | * @buf_size: The maximum size of the buffer pointed to by buffer. |
| 15 | * @count: If non-NULL, points to a variable that will receive the |
| 16 | * number of characters we need to insert (and no formatting |
| 17 | * will actually occur). |
| 18 | * @append_zero_char: If non-zero, put a trailing zero at the end of |
| 19 | * of the resulting string, if and only if we modified the |
| 20 | * string. |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 21 | * @grouping: see definition in localeconv(). |
| 22 | * @thousands_sep: see definition in localeconv(). |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 23 | * |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 24 | * Inserts thousand grouping characters (as defined by grouping and |
| 25 | * thousands_sep) into the string between buffer and buffer+n_digits. |
| 26 | * If count is non-NULL, don't do any formatting, just count the |
| 27 | * number of characters to insert. This is used by the caller to |
Eric Smith | 6d7e7a7 | 2008-06-24 01:06:47 +0000 | [diff] [blame] | 28 | * appropriately resize the buffer, if needed. If count is non-NULL, |
| 29 | * buffer can be NULL (it is not dereferenced at all in that case). |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 30 | * |
| 31 | * Return value: 0 on error, else 1. Note that no error can occur if |
| 32 | * count is non-NULL. |
| 33 | * |
| 34 | * This name won't be used, the includer of this file should define |
| 35 | * it to be the actual function name, based on unicode or string. |
| 36 | **/ |
| 37 | int |
| 38 | _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer, |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 39 | Py_ssize_t n_buffer, |
| 40 | Py_ssize_t n_digits, |
| 41 | Py_ssize_t buf_size, |
| 42 | Py_ssize_t *count, |
| 43 | int append_zero_char, |
| 44 | const char *grouping, |
| 45 | const char *thousands_sep) |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 46 | { |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 47 | Py_ssize_t thousands_sep_len = strlen(thousands_sep); |
| 48 | STRINGLIB_CHAR *pend = NULL; /* current end of buffer */ |
| 49 | STRINGLIB_CHAR *pmax = NULL; /* max of buffer */ |
| 50 | char current_grouping; |
| 51 | Py_ssize_t remaining = n_digits; /* Number of chars remaining to |
| 52 | be looked at */ |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 53 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 54 | /* Initialize the character count, if we're just counting. */ |
| 55 | if (count) |
| 56 | *count = 0; |
| 57 | else { |
| 58 | /* We're not just counting, we're modifying buffer */ |
| 59 | pend = buffer + n_buffer; |
| 60 | pmax = buffer + buf_size; |
| 61 | } |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 62 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 63 | /* Starting at the end and working right-to-left, keep track of |
| 64 | what grouping needs to be added and insert that. */ |
| 65 | current_grouping = *grouping++; |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 66 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 67 | /* If the first character is 0, perform no grouping at all. */ |
| 68 | if (current_grouping == 0) |
| 69 | return 1; |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 70 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 71 | while (remaining > current_grouping) { |
| 72 | /* Always leave buffer and pend valid at the end of this |
| 73 | loop, since we might leave with a return statement. */ |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 74 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 75 | remaining -= current_grouping; |
| 76 | if (count) { |
| 77 | /* We're only counting, not touching the memory. */ |
| 78 | *count += thousands_sep_len; |
| 79 | } |
| 80 | else { |
| 81 | /* Do the formatting. */ |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 82 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 83 | STRINGLIB_CHAR *plast = buffer + remaining; |
Eric Smith | 6d7e7a7 | 2008-06-24 01:06:47 +0000 | [diff] [blame] | 84 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 85 | /* Is there room to insert thousands_sep_len chars? */ |
| 86 | if (pmax - pend < thousands_sep_len) |
| 87 | /* No room. */ |
| 88 | return 0; |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 89 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 90 | /* Move the rest of the string down. */ |
| 91 | memmove(plast + thousands_sep_len, |
| 92 | plast, |
| 93 | (pend - plast) * sizeof(STRINGLIB_CHAR)); |
| 94 | /* Copy the thousands_sep chars into the buffer. */ |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 95 | #if STRINGLIB_IS_UNICODE |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 96 | /* Convert from the char's of the thousands_sep from |
| 97 | the locale into unicode. */ |
| 98 | { |
| 99 | Py_ssize_t i; |
| 100 | for (i = 0; i < thousands_sep_len; ++i) |
| 101 | plast[i] = thousands_sep[i]; |
| 102 | } |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 103 | #else |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 104 | /* No conversion, just memcpy the thousands_sep. */ |
| 105 | memcpy(plast, thousands_sep, thousands_sep_len); |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 106 | #endif |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 107 | } |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 108 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 109 | /* Adjust end pointer. */ |
| 110 | pend += thousands_sep_len; |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 111 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 112 | /* Move to the next grouping character, unless we're |
| 113 | repeating (which is designated by a grouping of 0). */ |
| 114 | if (*grouping != 0) { |
| 115 | current_grouping = *grouping++; |
| 116 | if (current_grouping == CHAR_MAX) |
| 117 | /* We're done. */ |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | if (append_zero_char) { |
| 122 | /* Append a zero character to mark the end of the string, |
| 123 | if there's room. */ |
| 124 | if (pend - (buffer + remaining) < 1) |
| 125 | /* No room, error. */ |
| 126 | return 0; |
| 127 | *pend = 0; |
| 128 | } |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * _Py_InsertThousandsGroupingLocale: |
| 134 | * @buffer: A pointer to the start of a string. |
| 135 | * @n_buffer: The length of the string. |
| 136 | * @n_digits: The number of digits in the string, in which we want |
| 137 | * to put the grouping chars. |
| 138 | * @buf_size: The maximum size of the buffer pointed to by buffer. |
| 139 | * @count: If non-NULL, points to a variable that will receive the |
| 140 | * number of characters we need to insert (and no formatting |
| 141 | * will actually occur). |
| 142 | * @append_zero_char: If non-zero, put a trailing zero at the end of |
| 143 | * of the resulting string, if and only if we modified the |
| 144 | * string. |
| 145 | * |
| 146 | * Reads thee current locale and calls _Py_InsertThousandsGrouping(). |
| 147 | **/ |
| 148 | int |
| 149 | _Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR *buffer, |
| 150 | Py_ssize_t n_buffer, |
| 151 | Py_ssize_t n_digits, |
| 152 | Py_ssize_t buf_size, |
| 153 | Py_ssize_t *count, |
| 154 | int append_zero_char) |
| 155 | { |
| 156 | struct lconv *locale_data = localeconv(); |
| 157 | const char *grouping = locale_data->grouping; |
| 158 | const char *thousands_sep = locale_data->thousands_sep; |
| 159 | |
| 160 | return _Py_InsertThousandsGrouping(buffer, n_buffer, n_digits, |
| 161 | buf_size, count, |
| 162 | append_zero_char, grouping, |
| 163 | thousands_sep); |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 164 | } |
| 165 | #endif /* STRINGLIB_LOCALEUTIL_H */ |