mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-27 03:38:55 -06:00
strings are stored as length+text and NULL byte is not a terminator
This commit is contained in:
@@ -10,6 +10,8 @@ pgfinclude_HEADERS = \
|
|||||||
libpgf_la_SOURCES = \
|
libpgf_la_SOURCES = \
|
||||||
db.cxx \
|
db.cxx \
|
||||||
db.h \
|
db.h \
|
||||||
|
text.cxx \
|
||||||
|
text.h \
|
||||||
pgf.cxx \
|
pgf.cxx \
|
||||||
reader.cxx \
|
reader.cxx \
|
||||||
reader.h \
|
reader.h \
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include "pgf.h"
|
#include "pgf.h"
|
||||||
#include "db.h"
|
#include "db.h"
|
||||||
|
#include "text.h"
|
||||||
#include "namespace.h"
|
#include "namespace.h"
|
||||||
#include "expr.h"
|
#include "expr.h"
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ private:
|
|||||||
|
|
||||||
struct PGF_INTERNAL_DECL PgfFlag {
|
struct PGF_INTERNAL_DECL PgfFlag {
|
||||||
PgfLiteral value;
|
PgfLiteral value;
|
||||||
char name[];
|
PgfText name;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ Namespace<V> namespace_insert(Namespace<V> map, ref<V> value) {
|
|||||||
if (map == 0)
|
if (map == 0)
|
||||||
return Node<V>::new_node(value);
|
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)
|
if (cmp < 0)
|
||||||
return Node<V>::balanceL(map->value,
|
return Node<V>::balanceL(map->value,
|
||||||
namespace_insert(map->left, value),map->right);
|
namespace_insert(map->left, value),map->right);
|
||||||
|
|||||||
@@ -82,9 +82,9 @@ uint64_t PgfReader::read_uint()
|
|||||||
return u;
|
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* buf = (char*) alloca(len*6+1);
|
||||||
char* p = buf;
|
char* p = buf;
|
||||||
@@ -115,10 +115,14 @@ moffset PgfReader::read_name(size_t size)
|
|||||||
|
|
||||||
p += len;
|
p += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t size = p-buf;
|
||||||
*p++ = 0;
|
*p++ = 0;
|
||||||
|
|
||||||
moffset offs = current_db->malloc(size+(p-buf));
|
moffset offs = current_db->malloc(struct_size+size+1);
|
||||||
strcpy((char*) (current_base+offs+size), buf);
|
PgfText* ptext = (PgfText*) (current_base+offs+struct_size-sizeof(PgfText));
|
||||||
|
ptext->size = size;
|
||||||
|
memcpy(ptext->text, buf, size+1);
|
||||||
|
|
||||||
return offs;
|
return offs;
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/runtime/c/text.cxx
Normal file
19
src/runtime/c/text.cxx
Normal 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
12
src/runtime/c/text.h
Normal 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
|
||||||
Reference in New Issue
Block a user