Skip to content

Commit 5831f8a

Browse files
ttaylorrgitster
authored andcommitted
config: introduce git_config_double()
Future commits will want to parse a double-precision floating point value from configuration, but we have no way to parse such a value prior to this patch. The core of the routine is implemented in git_parse_double(). Unlike git_parse_unsigned() and git_parse_signed(), however, the function implemented here only works on type "double", and not related types like "float", or "long double". This is because "float" and "long double" use different functions to convert from ASCII strings to floating point values (strtof() and strtold(), respectively). Likewise, there is no pointer type that can assign to any of these values (except for "void *"), so the only way to define this trio of functions would be with a macro expansion that is parameterized over the floating point type and conversion function. That is all doable, but likely to be overkill given our current needs, which is only to parse double-precision floats. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c059c87 commit 5831f8a

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

config.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,15 @@ ssize_t git_config_ssize_t(const char *name, const char *value,
12431243
return ret;
12441244
}
12451245

1246+
double git_config_double(const char *name, const char *value,
1247+
const struct key_value_info *kvi)
1248+
{
1249+
double ret;
1250+
if (!git_parse_double(value, &ret))
1251+
die_bad_number(name, value, kvi);
1252+
return ret;
1253+
}
1254+
12461255
static const struct fsync_component_name {
12471256
const char *name;
12481257
enum fsync_component component_bits;

config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ unsigned long git_config_ulong(const char *, const char *,
261261
ssize_t git_config_ssize_t(const char *, const char *,
262262
const struct key_value_info *);
263263

264+
/**
265+
* Identically to `git_config_double`, but for double-precision floating point
266+
* values.
267+
*/
268+
double git_config_double(const char *, const char *,
269+
const struct key_value_info *);
270+
264271
/**
265272
* Same as `git_config_bool`, except that integers are returned as-is, and
266273
* an `is_bool` flag is unset.

parse.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,35 @@ int git_parse_ssize_t(const char *value, ssize_t *ret)
125125
return 1;
126126
}
127127

128+
int git_parse_double(const char *value, double *ret)
129+
{
130+
char *end;
131+
double val;
132+
uintmax_t factor;
133+
134+
if (!value || !*value) {
135+
errno = EINVAL;
136+
return 0;
137+
}
138+
139+
errno = 0;
140+
val = strtod(value, &end);
141+
if (errno == ERANGE)
142+
return 0;
143+
if (end == value) {
144+
errno = EINVAL;
145+
return 0;
146+
}
147+
factor = get_unit_factor(end);
148+
if (!factor) {
149+
errno = EINVAL;
150+
return 0;
151+
}
152+
val *= factor;
153+
*ret = val;
154+
return 1;
155+
}
156+
128157
int git_parse_maybe_bool_text(const char *value)
129158
{
130159
if (!value)

parse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ int git_parse_ssize_t(const char *, ssize_t *);
66
int git_parse_ulong(const char *, unsigned long *);
77
int git_parse_int(const char *value, int *ret);
88
int git_parse_int64(const char *value, int64_t *ret);
9+
int git_parse_double(const char *value, double *ret);
910

1011
/**
1112
* Same as `git_config_bool`, except that it returns -1 on error rather

0 commit comments

Comments
 (0)