logoalt Hacker News

Making a StringBuffer in C, and questioning my sanity

33 pointsby coneonthefloorlast Tuesday at 10:48 AM30 commentsview on HN

Comments

burnt-resistortoday at 3:55 AM

It can be refactored into creating a buffer primitive of void* buf, size_t capacity, size_t refcount. Then, the string can implement using CoW logic on a buffer and size_t length. Read-only references to substrings become cheap and copying is done whenever there's a modification or realloc can't grow the underlying buffer.

WalterBrighttoday at 12:25 AM

    new_capacity *= 2;
A better value is to increase size by 1.5:

https://stackoverflow.com/questions/1100311/what-is-the-idea...

show 2 replies
o11cyesterday at 9:22 PM

Hm, this implementation seems allergic to passing types by value, which eliminates half of the allocations. It also makes the mistake of being mutable-first, and provides some fundamentally-inefficient operations.

The main mistake that this makes in common with most string implementations make is to only provide a single type, rather than a series of mostly-compatible types that can be used generically in common contexts, but which differ in ways that sometimes matter. Ownership, lifetime, representation, etc.

show 4 replies
gblarggtoday at 12:26 AM

It's odd how it has error reporting in some areas (alloc, split can return NULL if allocation fails), but not others (append, prepend have a void return type but might require allocation internally).

ranger_dangeryesterday at 8:46 PM

You might be interested in https://github.com/antirez/sds

show 1 reply
improgrammer007yesterday at 9:17 PM

I would rather focus on solving the main problem than reinvent the wheel. Just use C++ if perf is critical which gives you all these things for free. In this day and age the reasons for using C as your main language should be almost zero.