r/ProgrammingLanguages • • May 23 '26

Discussion How to implement String?

Currently, String in my language is just value and length because it's a temporary solution, And as the language has developed, I am now able to rewrite a lot just for it, so I want to make a decent String in my language. So my question is, which String concept annoys you the least?

47 Upvotes

76 comments sorted by

View all comments

Show parent comment

1

u/s0litar1us May 23 '26

You can also just

typedef struct {  
    char* data;  
    size_t length;  
    size_t capacity;  
} String;  
void foo(String* string) {  
    char* data = string->data;  
    data[...] = ...;  
    string->... = ...;  
}

But yeah that is a neat trick if you absolutely cannot spare that extra dereference.

1

u/EggplantExtra4946 May 26 '26

I would hope that the optimizer can perform this optimization automatically. I wonder if the Common Subexpression Elimination optimization would suffice for removing the extra dereference.