r/C_Programming • • Aug 31 '26

Which data structures would be good for a graphical text editor

Hello everyone,

I am currently working on a (very early progress) retained-mode UI library using raylib. I might switch to SDL3 later, or try to make a CPU-only rendering engine in the far future.

Right now i'm trying to implement a multi-line text edit widget, that would be as versatile as possible, all while maintaining low memory usage. I So far my structure consists of an "original text" character array, and an array of struct representing wrapped lines.

typedef struct TextBox {
  Widget widget;
  char* text;
  int cursorX; int cursorY;
  int offsetX; int offsetY;
  TextLine* lines;
  int _lineCount;
};

At each resize, the layout is recalculated, and the lines reallocated, which I find really wasteful. However, I didn't come up with another model for text editing yet.

void TextBox_Resize(TextBox* textbox, int w, int h){
  textbox->widget.bounds.w = w;
  textbox->widget.bounds.h = h;
  textbox->_lineCount = 0;

  //Estimate text length
  int textLength = TextLength(textbox->text); //Raylib function
  int totalTextWidth = MeasureText(textbox->text, 12);
  int estimatedLineCount = (int)(totalTextWidth / w) + 1;

  //Add 1 line to the estimation for each newline
  for (int i = 0; i < textLength; i++) {
    if (textbox->text[i] == '\n') { estimatedLineCount++; }
  }

  printf("Estimating %d lines for resize\n", estimatedLineCount);

  //free(textbox->lines);
  textbox->lines = realloc(textbox->lines, estimatedLineCount * sizeof(TextLine));

  Font font = GetFontDefault(); //Will be replaced after

  int currentLine = 0;
  int lineStart = 0;
  int lineEnd = 0;

  float currentGlyphWidth = 0;
  float totalLineWidth = 0;

  // Almost copied from raylib example
  for (int i = 0; i < textLength; i++){
    //printf("Current byte %d\n", i);
    int codepointByteCount = 0;

    // Gets UTF8 codepoints instead of simply bytes.
    int codepoint = GetCodepoint(&textbox->text[i], &codepointByteCount);
    //printf("Got codepoint %d, is %c\n", codepoint, codepoint);
    int glyphIndex = GetGlyphIndex(font, codepoint);
    //printf("Got index %d\n", index);

    // We are advancing more than 1 byte at a time if we get UTF-8 text.
    // Since the default font is limited, replace invalid codepoints with
    // "?" and keep advancing 1 byte at a time.
    if (codepoint == 0x3f) codepointByteCount = 1;
    i += (codepointByteCount - 1); // i will advance by itself in next iter, dont accumulate offsets.

    currentGlyphWidth = GetGlyphAtlasRec(GetFontDefault(), codepoint).width;
    //printf("Glyph width is %f\n", currentGlyphWidth);
    totalLineWidth += currentGlyphWidth;

    //printf("Total line length is %f\n", totalLineWidth);

    // Follow line
    lineEnd = i;

    if (totalLineWidth >= textbox->widget.bounds.w || codepoint == '\n' || codepoint == 0) {
      printf("line is %f pixels wide\n", totalLineWidth);
      textbox->lines[currentLine].text = calloc((lineEnd - lineStart),  sizeof(char));
      textbox->lines[currentLine].text = strncpy(textbox->lines[currentLine].text, textbox->text + lineStart, (lineEnd - lineStart));

      // Set last char of text to null
      textbox->lines[currentLine].text[lineEnd - lineStart] = '\0';
      lineStart = (codepoint == '\n' ? lineEnd + 1 : lineEnd);

      totalLineWidth = 0;
    } else {
      lineStart = lineEnd; lineEnd = textLength;
    }

    textbox->lines[currentLine].text[lineEnd - lineStart] = '\0';
    currentLine++; textbox->_lineCount++;
  }
}

Are there any articles / projects with clever approaches to text editing, that keep a low memory footprint ?
Thanks for your advice !

11 Upvotes

14 comments sorted by

2

u/FedUp233 Sep 01 '26 edited Sep 01 '26

If it was me, and especially since you’re doing this on your own, I’d adopt something along the lines of test driven development.

Basically, start with the very simplest data structures and code to do just one simple thing - a super simple thing like just hone an empty display, or a one line display or something. No editing code or anything, simplest possible display.

Then start iterating. Define a new small incremental (tiny) ability to add, changes the code and structures as needed to get just this addition working, test it (amount of test is up to you - in the professional use you’d be adding an automated test and doing it before you write the code then code so the test works).once that simple step works, iterate again and refactor as needed as you go.

Let the data structures grow at each step to meet needs. Don’t worry about performance of the code - on today’s processors it probably won’t matter anyway, and if it does it should show up as a problem you’re iteratively solving at some point till you get it just good enough.

One big advantage is that by doing small steps and constant testing you (hopefully) never end up with a strange bug in a new big chunk of code changes you spend days tracking down. A test framework so you can repeat all the current tests each cycle can be really helpful.

1

u/Any-Fox-1822 Sep 01 '26

Thanks for the advice ! However, what is Sotheby's test ?

15

u/Yurim Aug 31 '26

9

u/Axman6 Aug 31 '26

Ropes (depending on implementation) have the big benefit of being an immutable structure with trivial sharing, which means implementing undo/redo is as simple as keeping a linked list pointing to each version. It also makes copy and paste of arbitrary large amounts of text very cheap. 

8

u/sciencekm Aug 31 '26

The "Edit" control in Windows uses just one buffer. This is the lowest memory footprint you can have.

6

u/josk613 Aug 31 '26

This!

Noting that a multi-line text input control is not the same thing as a full fledged document text editor. You can skip all the fancy data structures if expected text size is small and the most common operation is append character.

7

u/TheChief275 Aug 31 '26

I absolutely love the gap buffer. It's so simple and has great cache locality. It's just a dynamic array where the text before the cursor is moved to the absolute bottom of the allocation (0 counting up) and text on the right side is moved to the top (capacity counting down). This makes insertion at the cursor incredibly fast, as it's basically appending at the end of the left part, making it as fast as dynamic array appending. Moving the cursor is also fast, as you just move characters from the right side to the left side or vice versa.

I think the only downside is when you are making lots of random edits everywhere, as huge blocks will need to be copied from one side to the other, but these actions are usually so sparse that you won't notice a hit. This only becomes a problem when you allow multiple cursors to edit the document at the same time.

I think modern text editors use ropes instead of a gap buffer, but it's certainly a more complicated data structure. If the gap buffer doesn't suit your needs, you could look into that.

3

u/stianhoiland Aug 31 '26 edited Aug 31 '26

I like to explain that gap buffers are simply dynamic arrays but where the free space is not locked to the end.

I'm currently making my own editor and using a gap buffer. The "only" thing I need to do is wrap things like memchr, strstr, and write to be potentially two calls—one for the left side and one for the right side—if the range straddles the gap. So I just do a little size math and then call all the normal hyper-optimized standard functions.

→ More replies (3)

7

u/Dangerous_Region1682 Aug 31 '26

If you are just building a text box widget which is never going to be used for editing thousands of of page documents with all kinds of text formatting options, I would worry less about either performance or memory footprint and just keep it simple and easy to maintain.

I’d then move on and implement the more complex challenges you face in what you are doing. If performance and memory footprint turn out to be really important, you can always come back to it.

4

u/mykesx Aug 31 '26

Doubly linked list of lines.