summaryrefslogtreecommitdiff
path: root/3rdparty/plibsys/src/pstring.c
blob: ed46ec5681623f1b20afd7f5196e328c9e820fb2 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * The MIT License
 *
 * Copyright (C) 2011-2016 Alexander Saprykin <saprykin.spb@gmail.com>
 * Copyright (C) 2009 Tom Van Baak (tvb) www.LeapSecond.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "pmem.h"
#include "pstring.h"

#include <string.h>
#include <ctype.h>

#define P_STR_MAX_EXPON		308

P_LIB_API pchar *
p_strdup (const pchar *str)
{
	pchar	*ret;
	psize	len;

	if (P_UNLIKELY (str == NULL))
		return NULL;

	len = strlen (str) + 1;

	if (P_UNLIKELY ((ret = p_malloc (len)) == NULL))
		return NULL;

	memcpy (ret, str, len);

	return ret;
}

P_LIB_API pchar *
p_strchomp (const pchar *str)
{
	pssize		pos_start, pos_end;
	psize		str_len;
	pchar		*ret;
	const pchar	*ptr;

	if (P_UNLIKELY (str == NULL))
		return NULL;

	ptr       = str;
	pos_start = 0;
	pos_end   = ((pssize) strlen (str)) - 1;

	while (pos_start < pos_end && isspace (* ((const puchar *) ptr++)))
		++pos_start;

	ptr = str + pos_end;

	while (pos_end > 0 && isspace (* ((const puchar *) ptr--)))
		--pos_end;

	if (pos_end < pos_start)
		return p_strdup ("\0");

	if (pos_end == pos_start && isspace (* ((const puchar *) (str + pos_end))))
		return p_strdup ("\0");

	str_len = (psize) (pos_end - pos_start + 2);

	if (P_UNLIKELY ((ret = p_malloc0 (str_len)) == NULL))
		return NULL;

	memcpy (ret, str + pos_start, str_len - 1);
	*(ret + str_len - 1) = '\0';

	return ret;
}

P_LIB_API pchar *
p_strtok (pchar *str, const pchar *delim, pchar **buf)
{
	if (P_UNLIKELY (delim == NULL))
		return str;

#ifdef P_OS_WIN
#  ifdef P_CC_MSVC
	if (P_UNLIKELY (buf == NULL))
		return str;
#    if _MSC_VER < 1400
	P_UNUSED (buf);
	return strtok (str, delim);
#    else
	return strtok_s (str, delim, buf);
#    endif
#  else
	P_UNUSED (buf);
	return strtok (str, delim);
#  endif
#else
	if (P_UNLIKELY (buf == NULL))
		return str;

	return strtok_r (str, delim, buf);
#endif
}

P_LIB_API double
p_strtod (const pchar *str)
{
	double	sign;
	double	value;
	double	scale;
	double	pow10;
	puint	expon;
	pint	frac;
	pchar	*orig_str, *strp;

	orig_str = p_strchomp (str);

	if (P_UNLIKELY (orig_str == NULL))
		return 0.0;

	strp = orig_str;
	sign = 1.0;

	if (*strp == '-') {
		sign = -1.0;
		strp += 1;
	} else if (*strp == '+')
		strp += 1;

	/* Get digits before decimal point or exponent, if any */
	for (value = 0.0; isdigit ((pint) *strp); strp += 1)
		value = value * 10.0 + (*strp - '0');

	/* Get digits after decimal point, if any */
	if (*strp == '.') {
		pow10 = 10.0;
		strp += 1;

		while (isdigit ((pint) *strp)) {
			value += (*strp - '0') / pow10;
			pow10 *= 10.0;
			strp += 1;
		}
	}

	/* Handle exponent, if any */
	frac  = 0;
	scale = 1.0;

	if ((*strp == 'e') || (*strp == 'E')) {
		/* Get sign of exponent, if any */
		strp += 1;

		if (*strp == '-') {
			frac = 1;
			strp += 1;

		} else if (*strp == '+')
			strp += 1;

		/* Get digits of exponent, if any */
		for (expon = 0; isdigit ((pint) *strp); strp += 1)
			expon = expon * 10 + (puint) ((*strp - '0'));

		if (P_UNLIKELY (expon > P_STR_MAX_EXPON))
			expon = P_STR_MAX_EXPON;

		/* Calculate scaling factor */
		while (expon >= 50) {
			scale *= 1e50;
			expon -= 50;
		}

		while (expon >= 8) {
			scale *= 1e8;
			expon -= 8;
		}

		while (expon > 0) {
			scale *= 10.0;
			expon -= 1;
		}
	}

	p_free (orig_str);

	/* Return signed and scaled floating point result */
	return sign * (frac ? (value / scale) : (value * scale));
}