mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-22 04:21:51 +00:00
[ruamoko] Extend the string api
Wrap the strtod, strtof, strtol, strtoul functions, supporting the end pointer as well (if not nil, the int offset of the end pointer relative to the string start is returned). Also, str_unmutable creates a return string from a mutable string (copying it).
This commit is contained in:
parent
a22c2224e0
commit
582423a019
3 changed files with 70 additions and 0 deletions
|
@ -39,6 +39,7 @@
|
|||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "qfalloca.h"
|
||||
|
||||
|
@ -119,6 +120,12 @@ bi_str_new (progs_t *pr, void *data)
|
|||
R_STRING (pr) = PR_NewMutableString (pr);
|
||||
}
|
||||
|
||||
static void
|
||||
bi_str_unmutable (progs_t *pr, void *data)
|
||||
{
|
||||
RETURN_STRING (pr, P_GSTRING (pr, 0));
|
||||
}
|
||||
|
||||
static void
|
||||
bi_str_free (progs_t *pr, void *data)
|
||||
{
|
||||
|
@ -315,6 +322,54 @@ bi_str_upper (progs_t *pr, void *data)
|
|||
RETURN_STRING (pr, upper);
|
||||
}
|
||||
|
||||
static void
|
||||
bi_strtod (progs_t *pr, void *data)
|
||||
{
|
||||
const char *str = P_GSTRING (pr, 0);
|
||||
pr_type_t *end = P_GPOINTER (pr, 1);
|
||||
char *end_ptr;
|
||||
R_DOUBLE (pr) = strtod (str, &end_ptr);
|
||||
if (end) {
|
||||
end->value = end_ptr - str;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bi_strtof (progs_t *pr, void *data)
|
||||
{
|
||||
const char *str = P_GSTRING (pr, 0);
|
||||
pr_type_t *end = P_GPOINTER (pr, 1);
|
||||
char *end_ptr;
|
||||
R_FLOAT (pr) = strtof (str, &end_ptr);
|
||||
if (end) {
|
||||
end->value = end_ptr - str;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bi_strtol (progs_t *pr, void *data)
|
||||
{
|
||||
const char *str = P_GSTRING (pr, 0);
|
||||
pr_type_t *end = P_GPOINTER (pr, 1);
|
||||
char *end_ptr;
|
||||
R_LONG (pr) = strtol (str, &end_ptr, P_INT (pr, 2));
|
||||
if (end) {
|
||||
end->value = end_ptr - str;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bi_strtoul (progs_t *pr, void *data)
|
||||
{
|
||||
const char *str = P_GSTRING (pr, 0);
|
||||
pr_type_t *end = P_GPOINTER (pr, 1);
|
||||
char *end_ptr;
|
||||
R_ULONG (pr) = strtoul (str, &end_ptr, P_INT (pr, 2));
|
||||
if (end) {
|
||||
end->value = end_ptr - str;
|
||||
}
|
||||
}
|
||||
|
||||
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
|
||||
#define p(type) PR_PARAM(type)
|
||||
#define P(a, s) { .size = (s), .alignment = BITOP_LOG2 (a), }
|
||||
|
@ -323,6 +378,7 @@ static builtin_t builtins[] = {
|
|||
bi(sprintf, -2, p(string)),
|
||||
bi(vsprintf, 2, p(string), P(1, 2)),
|
||||
bi(str_new, 0),
|
||||
bi(str_unmutable, 1, p(string)),
|
||||
bi(str_free, 1, p(string)),
|
||||
bi(str_hold, 1, p(string)),
|
||||
bi(str_valid, 1, p(string)),
|
||||
|
@ -337,6 +393,10 @@ static builtin_t builtins[] = {
|
|||
bi(str_quote, 1, p(string)),
|
||||
bi(str_lower, 1, p(string)),
|
||||
bi(str_upper, 1, p(string)),
|
||||
bi(strtod, 1, p(string)),
|
||||
bi(strtof, 1, p(string)),
|
||||
bi(strtol, 1, p(string)),
|
||||
bi(strtoul, 1, p(string)),
|
||||
{0}
|
||||
};
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
@extern string sprintf (string fmt, ...);
|
||||
@extern string vsprintf (string fmt, @va_list args);
|
||||
@extern string str_new (void);
|
||||
@extern string str_unmutable (string str);
|
||||
@extern void str_free (string str);
|
||||
@extern string str_hold (string str);
|
||||
@extern int str_valid (string str);
|
||||
|
@ -19,5 +20,9 @@ int str_str (string haystack, string needle);
|
|||
string str_quote (string str);
|
||||
string str_lower (string str);
|
||||
string str_upper (string str);
|
||||
double strtod (string str, int *end);
|
||||
float strtof (string str, int *end);
|
||||
long strtol (string str, int *end);
|
||||
unsigned long strtoul (string str, int *end);
|
||||
|
||||
#endif//__ruamoko_string_h
|
||||
|
|
|
@ -4,6 +4,7 @@ int (string s) strlen = #0;
|
|||
string (string fmt, ...) sprintf = #0;
|
||||
string vsprintf (string fmt, @va_list args) = #0;
|
||||
string (void) str_new = #0;
|
||||
string str_unmutable (string str) = #0;
|
||||
void (string str) str_free = #0;
|
||||
string str_hold (string str) = #0;
|
||||
int str_valid (string str) = #0;
|
||||
|
@ -18,3 +19,7 @@ int str_char (string str, int ind) = #0;
|
|||
string str_quote (string str) = #0;
|
||||
string str_lower (string str) = #0;
|
||||
string str_upper (string str) = #0;
|
||||
double strtod (string str, int *end) = #0;
|
||||
float strtof (string str, int *end) = #0;
|
||||
long strtol (string str, int *end) = #0;
|
||||
unsigned long strtoul (string str, int *end) = #0;
|
||||
|
|
Loading…
Reference in a new issue