aboutsummaryrefslogtreecommitdiff
path: root/src/str/ft_strtof.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
commit5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde (patch)
tree80911dc3c32e9f230750e7e1042d413dfb6efab2 /src/str/ft_strtof.c
parentee32953ea79616e72f5428cdf40c834714a891c9 (diff)
parentb96b82194ccad2cddbb46b77aa1962a57c47ff44 (diff)
downloadlibft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.gz
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.bz2
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.zip
Merge branch 'master' into ft_ssl
Diffstat (limited to 'src/str/ft_strtof.c')
-rw-r--r--src/str/ft_strtof.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/str/ft_strtof.c b/src/str/ft_strtof.c
new file mode 100644
index 0000000..f147394
--- /dev/null
+++ b/src/str/ft_strtof.c
@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strtof.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/09 12:09:13 by charles #+# #+# */
+/* Updated: 2020/05/09 12:23:03 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+/*
+** \brief Extract a float from a string
+** \param nptr String to extract from
+** \param endptr If not NULL pointer to last character
+** of the extraction is placed in *endptr
+** \return Extracted float value
+** \note This function doesn't try mimic the original perfectly
+** (no hexadecimal, exponent, NaN, inf handling)
+*/
+
+float ft_strtof(const char *nptr, char **endptr)
+{
+ float n;
+ bool is_neg;
+ const char *tmp;
+
+ while (ft_isspace(*nptr))
+ nptr++;
+ is_neg = *nptr == '-';
+ if (*nptr == '-' || *nptr == '+')
+ nptr++;
+ n = (float)ft_strtol(nptr, (char**)&nptr, 10);
+ if (*nptr == '.')
+ nptr++;
+ tmp = nptr;
+ n += (float)ft_strtol(nptr, (char**)&nptr, 10) / (10 * (nptr - tmp));
+ if (endptr != NULL)
+ *endptr = (char*)nptr;
+ return (is_neg ? -n : n);
+}