[dstring] Rename reserve to open

I've decided that I want reserve to mean only allocate backing memory,
not modify the size of the string, but I didn't want to rework much code
in the process. I might eventually get right of the open functions, but
I wouldn't be surprised if that's another decade or two in the future.
This commit is contained in:
Bill Currie 2024-09-14 15:51:41 +09:00
parent e933eb8fae
commit ebaa557d2a
4 changed files with 11 additions and 11 deletions

View file

@ -72,7 +72,7 @@ void dstring_adjust (dstring_t *dstr);
\param len the size of the hole to open. \param len the size of the hole to open.
\return pointer to the beginning of the opened hole. \return pointer to the beginning of the opened hole.
*/ */
char *dstring_reserve (dstring_t *dstr, size_t len); char *dstring_open (dstring_t *dstr, size_t len);
/** Copy len bytes from data into the dstring, replacing any existing data. /** Copy len bytes from data into the dstring, replacing any existing data.
*/ */
void dstring_copy (dstring_t *dstr, const char *data, size_t len); void dstring_copy (dstring_t *dstr, const char *data, size_t len);
@ -123,7 +123,7 @@ dstring_t *dstring_strdup (const char *str);
\return pointer to the current null terminator or beginning of the \return pointer to the current null terminator or beginning of the
opened hole if there was no terminator. opened hole if there was no terminator.
*/ */
char *dstring_reservestr (dstring_t *dstr, size_t len); char *dstring_openstr (dstring_t *dstr, size_t len);
/** Copy the null terminated string into the dstring. Replaces any existing /** Copy the null terminated string into the dstring. Replaces any existing
data. data.
The dstring does not have to be null terminated but will become so. The dstring does not have to be null terminated but will become so.

View file

@ -104,7 +104,7 @@ dstring_adjust (dstring_t *dstr)
} }
VISIBLE char * VISIBLE char *
dstring_reserve (dstring_t *dstr, size_t len) dstring_open (dstring_t *dstr, size_t len)
{ {
dstr->size += len; dstr->size += len;
dstring_adjust (dstr); dstring_adjust (dstr);
@ -225,7 +225,7 @@ dstring_strdup (const char *str)
} }
VISIBLE char * VISIBLE char *
dstring_reservestr (dstring_t *dstr, size_t len) dstring_openstr (dstring_t *dstr, size_t len)
{ {
int pos = dstr->size; int pos = dstr->size;
if (pos && !dstr->str[pos - 1]) if (pos && !dstr->str[pos - 1])

View file

@ -1111,7 +1111,7 @@ PL_GetArray (const char *string, hashctx_t **hashctx)
static void static void
write_tabs (dstring_t *dstr, int num) write_tabs (dstring_t *dstr, int num)
{ {
char *tabs = dstring_reservestr (dstr, num); char *tabs = dstring_openstr (dstr, num);
memset (tabs, '\t', num); memset (tabs, '\t', num);
tabs[num] = 0; tabs[num] = 0;
@ -1120,7 +1120,7 @@ write_tabs (dstring_t *dstr, int num)
static void static void
write_string_len (dstring_t *dstr, const char *str, int len) write_string_len (dstring_t *dstr, const char *str, int len)
{ {
char *dst = dstring_reservestr (dstr, len); char *dst = dstring_openstr (dstr, len);
memcpy (dst, str, len); memcpy (dst, str, len);
dst[len] = 0; dst[len] = 0;
} }
@ -1138,7 +1138,7 @@ static void
write_binary (dstring_t *dstr, byte *binary, int len) write_binary (dstring_t *dstr, byte *binary, int len)
{ {
int i; int i;
char *dst = dstring_reservestr (dstr, len * 2); char *dst = dstring_openstr (dstr, len * 2);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
*dst++ = to_hex (binary[i] >> 4); *dst++ = to_hex (binary[i] >> 4);
*dst++ = to_hex (binary[i]); *dst++ = to_hex (binary[i]);
@ -1159,12 +1159,12 @@ write_string (dstring_t *dstr, const char *str)
len++; len++;
} }
if (!quoted) { if (!quoted) {
dst = dstring_reservestr (dstr, len); dst = dstring_openstr (dstr, len);
strcpy (dst, str); strcpy (dst, str);
return; return;
} }
// assume worst case of all octal chars plus two quotes. // assume worst case of all octal chars plus two quotes.
dst = dstring_reservestr (dstr, len * 4 + 2); dst = dstring_openstr (dstr, len * 4 + 2);
*dst++= '\"'; *dst++= '\"';
while (*str) { while (*str) {
if (*str && isascii ((byte) *str) && isprint ((byte) *str) if (*str && isascii ((byte) *str) && isprint ((byte) *str)

View file

@ -127,7 +127,7 @@ locked_reader (void *data)
cnd_wait (&rb_read_cnd, &rb_mtx); cnd_wait (&rb_read_cnd, &rb_mtx);
} }
unsigned len = RB_DATA_AVAILABLE (ringbuffer_char); unsigned len = RB_DATA_AVAILABLE (ringbuffer_char);
RB_READ_DATA (ringbuffer_char, dstring_reserve (out_text, len), len); RB_READ_DATA (ringbuffer_char, dstring_open (out_text, len), len);
cnd_signal (&rb_write_cnd); cnd_signal (&rb_write_cnd);
mtx_unlock (&rb_mtx); mtx_unlock (&rb_mtx);
} while (out_text->str[out_text->size - 1]); } while (out_text->str[out_text->size - 1]);
@ -193,7 +193,7 @@ free_reader (void *data)
while (RB_DATA_AVAILABLE (ringbuffer_atomic) < 1) { while (RB_DATA_AVAILABLE (ringbuffer_atomic) < 1) {
} }
unsigned len = RB_DATA_AVAILABLE (ringbuffer_atomic); unsigned len = RB_DATA_AVAILABLE (ringbuffer_atomic);
RB_READ_DATA (ringbuffer_atomic, dstring_reserve (out_text, len), len); RB_READ_DATA (ringbuffer_atomic, dstring_open (out_text, len), len);
} while (out_text->str[out_text->size - 1]); } while (out_text->str[out_text->size - 1]);
return 0; return 0;