fx: implement a proper value-type mechanism
This commit is contained in:
+5
-77
@@ -1,4 +1,5 @@
|
||||
#include <ctype.h>
|
||||
#include <fx/cstr.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/string.h>
|
||||
@@ -1718,18 +1719,18 @@ FX_TYPE_CLASS_BEGIN(fx_string)
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
|
||||
FX_TYPE_CONSTRUCTOR("create", fx_string_create, 0, FX_VALUE_TYPE_NONE);
|
||||
FX_TYPE_CONSTRUCTOR("create", fx_string_create, 0, FX_TYPE_VOID);
|
||||
FX_TYPE_CONSTRUCTOR(
|
||||
"create_from_cstr",
|
||||
fx_string_create_from_cstr,
|
||||
1,
|
||||
FX_VALUE_TYPE_CSTR);
|
||||
FX_TYPE_CSTR);
|
||||
FX_TYPE_METHOD(
|
||||
FX_VALUE_TYPE_CSTR,
|
||||
FX_TYPE_CSTR,
|
||||
"get_cstr",
|
||||
fx_string_get_cstr,
|
||||
0,
|
||||
FX_VALUE_TYPE_POINTER);
|
||||
FX_TYPE_STRING);
|
||||
FX_TYPE_CLASS_END(fx_string)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_string)
|
||||
@@ -1763,76 +1764,3 @@ FX_TYPE_DEFINITION_BEGIN(fx_string_iterator)
|
||||
FX_TYPE_CLASS(fx_string_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_string_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_string_iterator)
|
||||
|
||||
/*** MISC FUNCTIONS ***********************************************************/
|
||||
|
||||
char *fx_strdup(const char *s)
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
char *p = malloc(len + 1);
|
||||
if (!p) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(p, s, len);
|
||||
p[len] = '\0';
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
size_t fx_strlen(const char *s, fx_strlen_flags flags)
|
||||
{
|
||||
if (!(flags & (FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD))) {
|
||||
return strlen(s);
|
||||
}
|
||||
|
||||
size_t out = 0;
|
||||
for (size_t i = 0; s[i]; i++) {
|
||||
if (s[i] == '\033' && (flags & FX_STRLEN_IGNORE_ESC)) {
|
||||
while (!isalpha(s[i]) && s[i]) {
|
||||
i++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (s[i] == '[' && (flags & FX_STRLEN_IGNORE_MOD)) {
|
||||
i++;
|
||||
if (s[i] == '[') {
|
||||
out++;
|
||||
continue;
|
||||
}
|
||||
|
||||
while (s[i] != ']' && s[i]) {
|
||||
i++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
out++;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
fx_wchar *fx_wstrdup(const fx_wchar *s)
|
||||
{
|
||||
size_t len = fx_wstrlen(s);
|
||||
fx_wchar *buf = calloc(len + 1, sizeof(fx_wchar));
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(buf, s, len * sizeof(fx_wchar));
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
size_t fx_wstrlen(const fx_wchar *s)
|
||||
{
|
||||
size_t len;
|
||||
for (len = 0; s[len] != 0; len++)
|
||||
;
|
||||
return len;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user