2018-12-15 12:42:37 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) Yuxuan Shui <yshuiv7@gmail.com>
|
|
|
|
#pragma once
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2019-03-30 05:07:21 -04:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2018-12-15 12:42:37 -05:00
|
|
|
#define mstrncmp(s1, s2) strncmp((s1), (s2), strlen(s1))
|
|
|
|
|
|
|
|
char *mstrjoin(const char *src1, const char *src2);
|
2019-03-10 08:34:37 -04:00
|
|
|
char *mstrjoin3(const char *src1, const char *src2, const char *src3);
|
2018-12-15 12:42:37 -05:00
|
|
|
void mstrextend(char **psrc1, const char *src2);
|
|
|
|
|
Parse number locale-independently
Previously we were using glibc's strtod function to parse floating point
numbers. The problem is, strtod is locale dependent. Meaning 7,7 might
be parsed as two numbers (7 and 7) in one locale, and parsed as one
number (7 point 7) in another locale. This is undesirable.
We need to set the locale to a value we know to make number parsing
consistently. We could use setlocale(), but that is not thread-safe. We
can also use uselocale(), which is thread-safe, but doesn't cover strtod
(Yeah, some of the locale-aware functions only acknowledge the global
locale, not the thread local one).
So in frustration, I just wrote a simple floating point number parser
myself. This parser obviously doesn't cover all cases strtod covers, but
is good enough for our needs.
Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
2019-02-01 20:22:41 -05:00
|
|
|
/// Parse a floating point number of form (+|-)?[0-9]*(\.[0-9]*)
|
|
|
|
double strtod_simple(const char *, const char **);
|
|
|
|
|
2018-12-20 09:13:14 -05:00
|
|
|
static inline int uitostr(unsigned int n, char *buf) {
|
2019-03-10 08:34:37 -04:00
|
|
|
int ret = 0;
|
|
|
|
unsigned int tmp = n;
|
|
|
|
while (tmp > 0) {
|
|
|
|
tmp /= 10;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
int pos = ret;
|
|
|
|
while (pos--) {
|
2019-03-30 05:07:21 -04:00
|
|
|
buf[pos] = (char)(n % 10 + '0');
|
2019-03-10 08:34:37 -04:00
|
|
|
n /= 10;
|
|
|
|
}
|
|
|
|
return ret;
|
2018-12-20 09:13:14 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
static inline const char *skip_space_const(const char *src) {
|
|
|
|
if (!src)
|
|
|
|
return NULL;
|
|
|
|
while (*src && isspace(*src))
|
|
|
|
src++;
|
|
|
|
return src;
|
2018-12-15 12:42:37 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
static inline char *skip_space_mut(char *src) {
|
|
|
|
if (!src)
|
|
|
|
return NULL;
|
|
|
|
while (*src && isspace(*src))
|
|
|
|
src++;
|
|
|
|
return src;
|
2018-12-15 12:42:37 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
#define skip_space(x) \
|
|
|
|
_Generic((x), char * : skip_space_mut, const char * : skip_space_const)(x)
|