wingo pushed a commit to branch wip-whippet in repository guile. commit c370d12d03f8e454888174462710655a3ed643fa Author: Andy Wingo <wi...@pobox.com> AuthorDate: Mon Jun 23 09:15:19 2025 +0200
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. --- libguile/strings.h | 60 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/libguile/strings.h b/libguile/strings.h index e7e0099d3..66399e191 100644 --- a/libguile/strings.h +++ b/libguile/strings.h @@ -175,29 +175,57 @@ SCM_API SCM scm_makfromstrs (int argc, char **argv); /* 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) \ - static SCM_UNUSED SCM_ALIGNED(8) const \ - struct \ - { \ - scm_t_bits word_0; \ - scm_t_bits word_1; \ - const char buffer[sizeof (contents)]; \ - } \ - c_name = \ + static SCM_ALIGNED(8) const struct scm_narrow_stringbuf c_name = \ { \ - scm_tc7_stringbuf, \ - sizeof (contents) - 1, \ + { scm_tc7_stringbuf, sizeof (contents) - 1 }, \ contents \ } #define SCM_IMMUTABLE_STRING(c_name, contents) \ SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \ - SCM_IMMUTABLE_DOUBLE_CELL (c_name, \ - scm_tc7_ro_string, \ - (scm_t_bits) &scm_i_paste (c_name, \ - _stringbuf), \ - (scm_t_bits) 0, \ - (scm_t_bits) (sizeof (contents) - 1)) + static SCM_ALIGNED(8) const struct scm_string scm_i_paste (c_name, _raw) = \ + { \ + scm_tc7_ro_string, \ + /* Discard "const" attr. */ \ + { (struct scm_stringbuf *) &scm_i_paste (c_name, _stringbuf).header }, \ + 0, \ + (sizeof (contents) - 1) \ + }; \ + static const SCM c_name = SCM_PACK (&scm_i_paste (c_name, _raw)) +