1
0
forked from GitHub/gf-core

strings are stored as length+text and NULL byte is not a terminator

This commit is contained in:
krangelov
2021-07-30 13:45:22 +02:00
parent a8b3537184
commit 17629e4821
6 changed files with 44 additions and 6 deletions

View File

@@ -10,6 +10,8 @@ pgfinclude_HEADERS = \
libpgf_la_SOURCES = \
db.cxx \
db.h \
text.cxx \
text.h \
pgf.cxx \
reader.cxx \
reader.h \

View File

@@ -10,6 +10,7 @@
#include "pgf.h"
#include "db.h"
#include "text.h"
#include "namespace.h"
#include "expr.h"
@@ -31,7 +32,7 @@ private:
struct PGF_INTERNAL_DECL PgfFlag {
PgfLiteral value;
char name[];
PgfText name;
};
typedef struct {

View File

@@ -181,7 +181,7 @@ Namespace<V> namespace_insert(Namespace<V> map, ref<V> value) {
if (map == 0)
return Node<V>::new_node(value);
int cmp = strcmp(value->name,map->value->name);
int cmp = textcmp(value->name,map->value->name);
if (cmp < 0)
return Node<V>::balanceL(map->value,
namespace_insert(map->left, value),map->right);

View File

@@ -82,9 +82,9 @@ uint64_t PgfReader::read_uint()
return u;
}
moffset PgfReader::read_name(size_t size)
moffset PgfReader::read_name(size_t struct_size)
{
size_t len = read_len();
size_t len = read_len();
char* buf = (char*) alloca(len*6+1);
char* p = buf;
@@ -115,10 +115,14 @@ moffset PgfReader::read_name(size_t size)
p += len;
}
size_t size = p-buf;
*p++ = 0;
moffset offs = current_db->malloc(size+(p-buf));
strcpy((char*) (current_base+offs+size), buf);
moffset offs = current_db->malloc(struct_size+size+1);
PgfText* ptext = (PgfText*) (current_base+offs+struct_size-sizeof(PgfText));
ptext->size = size;
memcpy(ptext->text, buf, size+1);
return offs;
}

19
src/runtime/c/text.cxx Normal file
View File

@@ -0,0 +1,19 @@
#include "data.h"
PGF_INTERNAL
int textcmp(PgfText &t1, PgfText &t2)
{
for (size_t i = 0; ; i++) {
if (i >= t1.size)
return (i - t2.size);
if (i >= t2.size)
return 1;
if (t1.text[i] > t2.text[i])
return 1;
else if (t1.text[i] < t2.text[i])
return -1;
i++;
}
}

12
src/runtime/c/text.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef TEXT_H
#define TEXT_H
typedef struct {
size_t size;
char text[];
} PgfText;
PGF_INTERNAL_DECL
int textcmp(PgfText &t1, PgfText &t2);
#endif