about summary refs log tree commit diff
path: root/stdio-common/grouping_iterator.c
blob: aa4d8ad72cf7a466d4b19b4f9716f0392529e6aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Iterator for inserting thousands separators into numbers.
   Copyright (C) 2022-2024 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/>.  */

#include <grouping_iterator.h>

#include <assert.h>
#include <limits.h>
#include <locale/localeinfo.h>
#include <stdint.h>
#include <string.h>

/* 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)
{
  memset (it, 0, sizeof (*it));
  it->remaining_in_current_group = digits;
  it->remaining = digits;
  return false;
}

static bool
grouping_iterator_setup (struct grouping_iterator *it, unsigned int digits,
                         const char *grouping)
{
  /* We treat all negative values like CHAR_MAX.  */

  if (*grouping == CHAR_MAX || *grouping <= 0)
    /* No grouping should be done.  */
    return __grouping_iterator_init_none (it, digits);

  unsigned int remaining_to_group = digits;
  unsigned int non_repeating_groups = 0;
  unsigned int groups = 0;
  while (true)
    {
      non_repeating_groups += *grouping;
      if (remaining_to_group <= (unsigned int) *grouping)
        break;

      ++groups;
      remaining_to_group -= *grouping++;

      if (*grouping == CHAR_MAX
#if CHAR_MIN < 0
          || *grouping < 0
#endif
          )
          /* No more grouping should be done.  */
        break;
      else if (*grouping == 0)
        {
          /* Same grouping repeats.  */
          --grouping;
          non_repeating_groups -= *grouping; /* Over-counted.  */
          unsigned int repeats = (remaining_to_group - 1) / *grouping;
          groups += repeats;
          remaining_to_group -= repeats * *grouping;
          break;
        }
    }

  it->remaining_in_current_group = remaining_to_group;
  it->remaining = digits;
  it->groupings = grouping;
  it->non_repeating_groups = non_repeating_groups;
  it->separators = groups;
  return it->separators > 0;
}

/* Returns the appropriate grouping item in LOC depending on CATEGORY
   (which must be LC_MONETARY or LC_NUMERIC).  */
static const char *
get_grouping (int category, locale_t loc)
{
  return _nl_lookup (loc, category,
                     category == LC_MONETARY ? MON_GROUPING : GROUPING);
}


bool
__grouping_iterator_init (struct grouping_iterator *it,
                          int category, locale_t loc, unsigned int digits)
{
  if (digits <= 1)
    return __grouping_iterator_init_none (it, digits);
  else
    return grouping_iterator_setup (it, digits, get_grouping (category, loc));
}

bool
__grouping_iterator_next (struct grouping_iterator *it)
{
  assert (it->remaining > 0);
  --it->remaining;

  if (it->remaining_in_current_group > 0)
    {
      --it->remaining_in_current_group;
      return false;
    }

  /* If we are in the non-repeating part, switch group.  */
  if (it->remaining < it->non_repeating_groups)
    --it->groupings;

  it->remaining_in_current_group = *it->groupings - 1;
  return true;
}