1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-04 08:40:21 +02:00

Better type-safety for snarfed immutable strings

* libguile/strings.h: Add struct types for stringbufs and strings.
(SCM_IMMUTABLE_STRINGBUF, SCM_IMMUTABLE_STRING): Use the new data types.
This commit is contained in:
Andy Wingo 2025-06-23 09:15:19 +02:00
parent 4702becf89
commit c370d12d03

View file

@ -175,29 +175,57 @@ SCM_API SCM scm_makfromstrs (int argc, char **argv);
/* Snarfing support. See snarf.h. */ /* Snarfing support. See snarf.h. */
struct scm_stringbuf
{
scm_t_bits tag_and_flags;
size_t length;
};
struct scm_narrow_stringbuf
{
struct scm_stringbuf header;
char contents[];
};
struct scm_wide_stringbuf
{
struct scm_stringbuf header;
scm_t_wchar contents[];
};
struct scm_string
{
scm_t_bits tag_and_flags;
union
{
/* Usually a string is just a slice of a stringbuf. */
struct scm_stringbuf *stringbuf;
/* Sometimes though it aliases another string. */
struct scm_string *string;
};
size_t start;
size_t length;
};
#define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \ #define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \
static SCM_UNUSED SCM_ALIGNED(8) const \ static SCM_ALIGNED(8) const struct scm_narrow_stringbuf c_name = \
struct \
{ \ { \
scm_t_bits word_0; \ { scm_tc7_stringbuf, sizeof (contents) - 1 }, \
scm_t_bits word_1; \
const char buffer[sizeof (contents)]; \
} \
c_name = \
{ \
scm_tc7_stringbuf, \
sizeof (contents) - 1, \
contents \ contents \
} }
#define SCM_IMMUTABLE_STRING(c_name, contents) \ #define SCM_IMMUTABLE_STRING(c_name, contents) \
SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \ SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \
SCM_IMMUTABLE_DOUBLE_CELL (c_name, \ static SCM_ALIGNED(8) const struct scm_string scm_i_paste (c_name, _raw) = \
{ \
scm_tc7_ro_string, \ scm_tc7_ro_string, \
(scm_t_bits) &scm_i_paste (c_name, \ /* Discard "const" attr. */ \
_stringbuf), \ { (struct scm_stringbuf *) &scm_i_paste (c_name, _stringbuf).header }, \
(scm_t_bits) 0, \ 0, \
(scm_t_bits) (sizeof (contents) - 1)) (sizeof (contents) - 1) \
}; \
static const SCM c_name = SCM_PACK (&scm_i_paste (c_name, _raw))