fix bugs with tinystr, it works now!
authorcassowarii <cassowary@cassowary.me>
Wed, 24 Jun 2026 02:53:34 +0000 (19:53 -0700)
committercassowarii <cassowary@cassowary.me>
Wed, 24 Jun 2026 02:53:34 +0000 (19:53 -0700)
src/data/string.c
src/parse/scanner.c

index 8041e5d..8d1c0a4 100644 (file)
@@ -76,12 +76,13 @@ hString sbString_new(const char *value, usize length) {
   } else {
     /* tinystr */
     hString new_handle = 0;
+    /* we encode the string backwards so it's easier to decode */
     for (int i = 0; i < length; i++) {
       new_handle <<= 8;
-      new_handle |= value[i];
+      new_handle |= value[length - i - 1];
     }
     /* set length in high bit */
-    new_handle |= ((length << (8 * 7)) | FLAG_TINY);
+    new_handle |= (((u64)length << (8 * 7)) | FLAG_TINY);
     return new_handle;
   }
 }
@@ -89,7 +90,7 @@ hString sbString_new(const char *value, usize length) {
 const char *sbString_get_value(hString handle, char *buffer_i_might_use, usize *length_out) {
   if (is_tinystr(handle)) {
     /* string is stored backwards because it's easiest to just get the low byte */
-    usize tinylength = ((handle >> (8 * 7)) & 0x8F);
+    usize tinylength = ((handle >> (8 * 7)) & 0x7F);
     if (length_out) *length_out = tinylength;
     if (buffer_i_might_use) {
       usize index = 0;
@@ -113,7 +114,7 @@ const char *sbString_get_value(hString handle, char *buffer_i_might_use, usize *
 usize sbString_get_length(hString handle) {
   if (handle & FLAG_TINY) {
     /* length is top byte except high bit */
-    return (handle >> (8 * 7)) & 0x8F;
+    return (handle >> (8 * 7)) & 0x7F;
   } else {
     /* length is just in the entry */
     strblk *blk = get_strblk(handle / STRINGS_PER_BLOCK);
@@ -125,7 +126,7 @@ usize sbString_get_length(hString handle) {
 /* --- */
 
 static flag is_tinystr(hString handle) {
-  return handle & FLAG_TINY;
+  return ((handle & FLAG_TINY) != 0);
 }
 
 static const char *get_ptr_of_entry(strentry *e) {
index 6b0c391..34a6724 100644 (file)
@@ -442,7 +442,7 @@ static sbLexToken compute_next_token(hScanner sc) {
         read_char_into_buffer(sc, '\0');
 
         finalize_char_buffer(sc);
-        hString hstr = sbString_new(sc->dynamic_buffer.data, sc->dynamic_buffer.size);
+        hString hstr = sbString_new(sc->dynamic_buffer.data, sc->dynamic_buffer.size - 1);
 
         new_token.hstr = hstr;
         new_token.size = sc->dynamic_buffer.size;