diff options
author | Florian Weimer <fweimer@redhat.com> | 2022-12-19 18:56:54 +0100 |
---|---|---|
committer | Florian Weimer <fweimer@redhat.com> | 2022-12-19 18:56:54 +0100 |
commit | ffde06c915d10c0717a0980508ccb28506c6ec63 (patch) | |
tree | c52145517af2e8dadf678e24d0ff79adfc7a014b /stdio-common/grouping_iterator.h | |
parent | edd1b2a0d9025b849a87f9b7eec616eb820e4fe2 (diff) | |
download | glibc-ffde06c915d10c0717a0980508ccb28506c6ec63.zip glibc-ffde06c915d10c0717a0980508ccb28506c6ec63.tar.gz glibc-ffde06c915d10c0717a0980508ccb28506c6ec63.tar.bz2 |
locale: Implement struct grouping_iterator
The iterator allows grouping while scanning forward through
the digits. This enables emitting digits as they are processed.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'stdio-common/grouping_iterator.h')
-rw-r--r-- | stdio-common/grouping_iterator.h | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/stdio-common/grouping_iterator.h b/stdio-common/grouping_iterator.h new file mode 100644 index 0000000..e9fc79f --- /dev/null +++ b/stdio-common/grouping_iterator.h @@ -0,0 +1,65 @@ +/* Iterator for grouping a number while scanning it forward. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#ifndef GROUPING_ITERATOR_H +#define GROUPING_ITERATOR_H + +#include <locale.h> +#include <stdbool.h> + +struct grouping_iterator +{ + /* Number of characters in the current group. If this reaches zero, + a thousands separator needs to be emittted. */ + unsigned int remaining_in_current_group; + + /* Number of characters remaining in the number. This is used to + detect the start of the non-repeating groups. */ + unsigned int remaining; + + /* Points to the current grouping descriptor. */ + const char *groupings; + + /* Total number of characters in the non-repeating groups. */ + unsigned int non_repeating_groups; + + /* Number of separators that will be inserted if the whole number is + processed. (Does not change during iteration.) */ + unsigned int separators; +}; + +struct __locale_data; + +/* Initializes *IT with the data from LOCDATA (which must be for + LC_MONETARY or LC_NUMERIC). DIGITS is the length of the number. + Returns true if grouping is active, false if not. */ +bool __grouping_iterator_init (struct grouping_iterator *it, + int category, locale_t loc, + unsigned int digits) attribute_hidden; + +/* Initializes *IT with no grouping information for a string of length + DIGITS, and return false to indicate no grouping. */ +bool __grouping_iterator_init_none (struct grouping_iterator *it, + unsigned int digits) + attribute_hidden; + +/* Advances to the next character and returns true if a thousands + separator should be inserted before emitting it. */ +bool __grouping_iterator_next (struct grouping_iterator *it); + +#endif /* GROUPING_ITERATOR_H */ |