Random changes with varying usefulness
This commit is contained in:
119
src/helper.c
119
src/helper.c
@@ -1,7 +1,115 @@
|
||||
#include "helper.h"
|
||||
|
||||
#include "lexer.h"
|
||||
#include "noom.h"
|
||||
|
||||
double noom_strtod(const char* s, const char** endptr, int* error) {
|
||||
// Num???? Is that a fucking noom reference???????
|
||||
double num = 0.0;
|
||||
|
||||
int error_but_different = 1;
|
||||
|
||||
const noom_bool_t negative = *s == '-';
|
||||
if (*s == '-' || *s == '+')
|
||||
s++;
|
||||
|
||||
if (!noomL_isnumber(s[*s == '.'])) goto fuck;
|
||||
|
||||
if (*s == '0' && noomL_lower(s[1]) == 'x') {
|
||||
s += 2;
|
||||
// if the string starts with "0x" but does not contain digits it's invalid
|
||||
noom_bool_t are_we_cooked = 1;
|
||||
while (noomL_ishex(*s)) {
|
||||
int digit;
|
||||
if (noomL_isnumber(*s)) digit = *s - '0';
|
||||
else digit = noomL_lower(*s) - 'a' + 10;
|
||||
|
||||
num = num * 16.0 + digit;
|
||||
are_we_cooked = 0;
|
||||
s++;
|
||||
}
|
||||
|
||||
if (are_we_cooked) goto fuck;
|
||||
|
||||
if (noomL_lower(*s) == 'p') {
|
||||
// damn then
|
||||
s++;
|
||||
int exponent = 0;
|
||||
const noom_bool_t exponent_negative = *s == '-';
|
||||
if (*s == '-' || *s == '+')
|
||||
s++;
|
||||
|
||||
if (!noomL_isnumber(*s)) goto fuck;
|
||||
|
||||
while (noomL_isnumber(*s)) {
|
||||
exponent = exponent * 10 + (*s - '0');
|
||||
s++;
|
||||
}
|
||||
|
||||
num *= noom_pow(2.0, exponent_negative ? -exponent : exponent);
|
||||
}
|
||||
}
|
||||
else {
|
||||
noom_bool_t are_we_cooked = 1;
|
||||
while (noomL_isnumber(*s)) {
|
||||
num = num * 10.0 + (*s - '0');
|
||||
are_we_cooked = 0;
|
||||
s++;
|
||||
}
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
|
||||
double fraction_divisor = 10.0;
|
||||
while (noomL_isnumber(*s)) {
|
||||
are_we_cooked = 0;
|
||||
num += (double)(*s - '0') / fraction_divisor;
|
||||
fraction_divisor *= 10.0;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
if (are_we_cooked) goto fuck;
|
||||
|
||||
if (noomL_lower(*s) == 'e') {
|
||||
s++;
|
||||
int exponent = 0;
|
||||
const noom_bool_t exponent_negative = *s == '-';
|
||||
if (*s == '-' || *s == '+')
|
||||
s++;
|
||||
|
||||
if (!noomL_isnumber(*s)) goto fuck;
|
||||
|
||||
while (noomL_isnumber(*s)) {
|
||||
exponent = exponent * 10 + (*s - '0');
|
||||
s++;
|
||||
}
|
||||
|
||||
num *= noom_pow(10.0, exponent_negative ? -exponent : exponent);
|
||||
}
|
||||
}
|
||||
|
||||
error_but_different = 0;
|
||||
fuck:
|
||||
if (endptr) *endptr = s;
|
||||
if (error) *error = error_but_different;
|
||||
return negative ? -num : num;
|
||||
}
|
||||
|
||||
// made by some stranger on stack overflow
|
||||
double noom_pow(double x, int y) {
|
||||
if (y == 0)
|
||||
return 1;
|
||||
|
||||
const double temp = noom_pow(x, y / 2);
|
||||
|
||||
if (y % 2 == 0) {
|
||||
return temp * temp;
|
||||
}
|
||||
|
||||
if (y > 0)
|
||||
return x * temp * temp;
|
||||
|
||||
return temp * temp / x;
|
||||
}
|
||||
|
||||
int noom_startswith(const char* str, const char* compare) {
|
||||
#ifdef __has_builtin
|
||||
#if __has_builtin(__builtin_strncmp)
|
||||
@@ -90,3 +198,12 @@ void noom_free(void* ptr) {
|
||||
void* noom_realloc(void* ptr, noom_uint_t size) {
|
||||
return realloc(ptr, size);
|
||||
}
|
||||
|
||||
void* noom_realloc_free(void* ptr, noom_uint_t size) {
|
||||
void* newptr = noom_realloc(ptr, size);
|
||||
if (newptr == 0) {
|
||||
noom_free(ptr);
|
||||
return 0;
|
||||
}
|
||||
return newptr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user