---
name: "tbuf"
section: "3pub"
platform: "linux"
package: "publib-dev"
version: "0.40-4"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/tbuf"
---

# tbuf(3pub)

manipulate text editor buffer

## NAME

tbuf\_create, tbuf\_destroy, tbuf\_copy, tbuf\_cat, tbuf\_length, tbuf\_chars - manipulate text editor buffer

## SYNOPSIS

```
#include <publib.h>

Tbuf *tbuf_create(const char *chars, size_t len);
void tbuf_destroy(Tbuf *tbuf);
Tbuf *tbuf_copy(Tbuf *tbuf, size_t offset, size_tlen);
Tbuf *tbuf_cat(Tbuf *tbuf, Tbuf * tbuf);
size_t tbuf_length(Tbuf *tbuf);
void tbuf_chars(char *chars, Tbuf *tbuf, size_t offset, size_t len);
```

## DESCRIPTION

These routines create and manipulate simple text editor buffers, which can also be thought of as arbitrarily large text strings. The buffers are one-dimensional (i.e., not automatically divided into lines), and are indexed with character offsets. They are 8-bit and binary clean, i.e., they may contain any 8-bit characters, including the zero byte ('\\0').

**tbuf\_create** creates a buffer from a C character array, and **tbuf\_destroy** destroys it. Once it's created, a buffer may not be modified. Instead, a new buffer needs to be created, using **tbuf\_cat** and **tbuf\_copy**. They create the new buffer so that it shares as much memory as possible with the old buffer, so the immutability does not necessarily waste memory much. By never changing a buffer, it is rather simple to implement undo and redo: you only need to keep a list of buffers and display the suitable one to the user. The caller should remember to call **tbuf\_destroy** for unnecessary buffers, of course.

**tbuf\_length** returns the number of characters in the buffer. **tbuf\_copy** copies part of a buffer into a C character array. The array is *not* zero-terminated; the caller must do it himself.

## RETURN VALUE

**tbuf\_create**, **tbuf\_copy**, and **tbuf\_cat** return a pointer to the new buffer, or NULL if the operation failed.

**tbuf\_length** returns the number of characters in the buffer.

**tbuf\_destroy** and **tbuf\_chars** return nothing and cannot fail.

## SEE ALSO

**publib**(3), **sbuf**(3)

## AUTHOR

Lars Wirzenius, liw@iki.fi.
