nebble/util/fixed_strings

Source   Edit  

Statically allocated, heap-free strings for Pebble.

This module provides a FixedString[N] type that stores text in a fixed-size array, and a fmt macro that performs zero-allocation string formatting at runtime.

Types

FixedString[N] = object
  len*: int
  data*: array[N, char]
A stack-allocated string with maximum capacity N. Includes a null-terminator, so actual capacity is N-1. Source   Edit  

Procs

proc add[N](s: var FixedString[N]; c: char) {.inline.}
Add a single character to the string. Source   Edit  
proc add[N](s: var FixedString[N]; str: cstring) {.inline.}
Add a C-string to the fixed string. Source   Edit  
proc addInt[N](s: var FixedString[N]; val: int32) {.inline.}
Add an integer to the string without heap allocation. Source   Edit  
proc clear[N](s: var FixedString[N]) {.inline.}
Reset the string length to 0. Source   Edit  
proc fromCstring[N](s: cstring): FixedString[N]

Creates FixedString from cstring, calculating len once at construction.

This constructor scans the input once to determine length, then copies the data. Use when you have a cstring and want O(1) len access afterwards.

Example: var myStr = fromCstring32 # myStr.len is now correctly set (O(1) access)

Complexity: O(min(strlen(s), N)) - one-time scan at construction

Source   Edit  
proc recalcLen[N](s: var FixedString[N]; maxScan: int = N) {.inline.}

Recalculate len by scanning for null terminator (bounded).

Use this after calling raw FFI functions directly that write to the buffer. For high-level wrappers (formatTime, storage.read, etc.), len is updated automatically.

Complexity: O(min(maxScan, N)) - bounded scan

Example: # Raw FFI call (not recommended): discard ffi.strftime(myStr.cstr, maxSize, "%H:%M", time) myStr.recalcLen() # Update len after raw call

Bounded scan (faster for known-max-length strings):

myStr.recalcLen(maxScan = 32) # Scan at most 32 chars

Source   Edit  

Macros

macro formatInto(s: var FixedString; body: varargs[untyped]): untyped

Macro to format multiple values into a FixedString. Expands to a series of s.add and s.addInt calls.

Example: var str: FixedString[32] str.formatInto("Score: ", score, "/", total)

Source   Edit  

Templates

template cstr[N](s: FixedString[N]): cstring

Get cstring pointer to FixedString data.

IMPORTANT: Pebble stores this pointer (doesn't copy), so the FixedString must remain alive as long as the text is displayed. Use global/static FixedString variables for text that persists.

Template ensures this operates at the call site without creating a copy.

Example: var timeStr: FixedString16 # Global variable timeStr.f("12:00") timeLayer.text = timeStr.cstr # Safe - points to global data

Manual FFI Usage

When calling raw FFI functions that write to the buffer, use high-level wrappers or manually update len afterwards:

Using high-level wrapper (recommended):

timeStr.formatTime(tickTime) # len updated automatically (O(1))

Using raw FFI (must update len manually):

discard ffi.strftime(timeStr.cstr, ...) timeStr.recalcLen() # Recalculate len (O(n) bounded scan)

See also: recalcLen for manual length recalculation.

Source   Edit  
template f[N](s: var FixedString[N]; body: varargs[untyped])
Alias for formatInto with auto-clear. Source   Edit