Tutorial 06: TText - Real-World Patterns
Overview
This tutorial demonstrates how TText is used in real-world applications, showing patterns from the AsmBB forum engine. You'll learn to build HTTP responses, integrate with StrLib, and apply practical text editing techniques.
Topics Covered
1. HTTP Response Building
Creating dynamic HTTP headers
Building HTML content incrementally
Managing text buffers efficiently
2. TText + StrLib Integration
Converting between TText and StrLib strings
When to use each text representation
Memory management best practices
3. Common Real-World Patterns
Pattern: Keep TText pointer in register (EDX)
Pattern: Update pointer after each modifying call
Pattern: Use position -1 for appending
Pattern: Compact only at the end
Prerequisites
Before starting this tutorial, you should have completed:
Tutorial 04: TText - Gap Buffer Basics
Tutorial 05: TText - Advanced Operations
Understanding of HTTP protocol basics
Demos
| Demo | Filename | Description |
| 19 | demo19_ttext_advanced.asm | Complete HTTP response builder |
Building and Running
cd 06-ttext-manipulation
./build.sh
This will compile and test all demos.
Real-World Patterns from AsmBB
Pattern 1: Keep Pointer in Register
AsmBB frequently keeps the TText pointer in EDX throughout operations:
stdcall TextCreate, sizeof.TText
mov edx, eax ; Keep in EDX
; All operations use EDX
stdcall TextAddString, edx, -1, cHeader
mov edx, eax ; Update after reallocation
stdcall TextAddString, edx, -1, cContent
mov edx, eax
Why: Avoids memory load/store and follows AsmBB conventions.
Pattern 2: Build HTTP Responses Incrementally
; Start with status line
stdcall TextAddString, edx, -1, cHTTP200
mov edx, eax
; Add headers
stdcall TextAddString, edx, -1, cContentType
mov edx, eax
; Add blank line (end of headers)
stdcall TextAddString, edx, -1, cCRLF
mov edx, eax
; Add body
stdcall TextAddString, edx, -1, cBody
mov edx, eax
; Compact only when ready to send
stdcall TextCompact, edx
stdcall FileWrite, [hSocket], edx, [...]
Pattern 3: Use GapBegin for Current Position
When appending at current position, use GapBegin directly:
stdcall TextAddString, edx, [edx+TText.GapBegin], cString
mov edx, eax
This is equivalent to position -1 but explicitly uses the gap position.
Pattern 4: Update After Every Modifying Call
CRITICAL: Always capture EDX after calls that modify content:
TextAddString→ movedx, eaxTextSetGapSize→ movedx, eaxTextDelChar→ (no pointer change)TextMoveGap→ (no pointer change)TextCompact→ (may change pointer!)
Function Reference Summary
All TText functions covered in Tutorials 04-06:
Creation (Tutorial 04)
TextCreate- Allocate new TText bufferTextDup- Duplicate TTextTextFree- Free TText buffer
Insertion (Tutorial 04)
TextAddString- Add string at positionTextAddBytes- Add raw bytes at positionTextAddChar- Add character at gapTextAddText- Add TText content
Deletion (Tutorial 04)
TextDelChar- Delete character at gapTextCompact- Collapse gap to endTextSetGapSize- Adjust gap size
Movement (Tutorial 04)
TextMoveGap- Move gap to position
Search (Tutorial 05)
TextPrepareSearch- Build KMP tableTextSearch- Search for substring
Coordinates (Tutorial 05)
TextIndexToPos- Character index to byte positionTextPosToIndex- Byte position to character indexTextPosToOffset- Position to memory offsetTextOffsetToPos- Memory offset to position
Utility (Tutorial 04)
TextGetChar- Get character at gapTextDebugInfo- Display structure info
Next Steps
After completing this tutorial series, you should be able to:
Build text editors using gap buffers
Create dynamic HTTP responses
Integrate TText with StrLib
Apply real-world patterns from AsmBB
For further study:
Read
~/Documents/fossil/asmbb/source/render2.asm- AsmBB's template engineStudy
~/Documents/fossil/FreshIDE/freshlib/data/buffergap.asm- TText implementation