Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Llama cpp Jinja Utils

From Leeroopedia
Knowledge Sources
Domains Template_Engine, Utilities
Last Updated 2026-02-15 00:00 GMT

Overview

Provides shared utility functions and types used across the Jinja engine, including string manipulation, error formatting, and a hash utility.

Description

This header implements `string_replace_all` for bulk string replacement, `peak_source` for displaying source code context around error positions (with newline-to-arrow conversion and caret positioning), and `fmt_error_with_source` for constructing error messages with source context. The `hasher` struct implements an FNV-1a hash function processing data in block-sized chunks (4 or 8 bytes depending on platform), providing a streaming `update()` method and a `digest()` method for finalization.

Usage

Include this header when working with any component of the Jinja engine. It is used by the lexer, parser, and runtime for error reporting with source context, and by the value system for hash table operations on Jinja values.

Code Reference

Source Location

Signature

namespace jinja {

static void string_replace_all(std::string & s, const std::string & search, const std::string & replace);

static std::string peak_source(const std::string & source, size_t pos, size_t max_peak_chars = 40);

static std::string fmt_error_with_source(const std::string & tag, const std::string & msg,
                                          const std::string & source, size_t pos);

struct hasher {
    static constexpr auto size_t_digits = sizeof(size_t) * 8;
    static constexpr size_t prime;
    static constexpr size_t seed;
    static constexpr auto block_size = sizeof(size_t);

    hasher() = default;
    hasher(const std::type_info & type_inf) noexcept;
    hasher& update(void const * bytes, size_t len) noexcept;
    hasher& update(const std::string & s) noexcept;
    size_t digest() noexcept;
};

} // namespace jinja

Import

#include "jinja/utils.h"

I/O Contract

Inputs

Name Type Required Description
s std::string & Yes String to perform replacements on (modified in place)
search const std::string & Yes Substring to search for in replacements
replace const std::string & Yes Replacement string
source const std::string & Yes Source code string for error context display
pos size_t Yes Position in source where the error occurred
max_peak_chars size_t No Maximum characters to show around error position (default 40)
tag const std::string & Yes Error tag (e.g., "lexer", "parser") for message prefix
msg const std::string & Yes Error message text
bytes void const * Yes Raw data bytes to feed into the hasher
len size_t Yes Number of bytes to process

Outputs

Name Type Description
peak_source return std::string Formatted source context with caret pointing to the error position
fmt_error_with_source return std::string Complete error message with tag, message, and source context
digest return size_t Final hash value after processing all input data

Usage Examples

#include "jinja/utils.h"

// String replacement
std::string text = "Hello {{name}}, welcome to {{place}}!";
jinja::string_replace_all(text, "{{name}}", "Alice");

// Error formatting with source context
std::string error_msg = jinja::fmt_error_with_source(
    "parser", "unexpected token", template_source, error_position);
// Output: "parser: unexpected token\n...source context...^\n"

// Hashing
jinja::hasher h;
h.update("key_data", 8);
size_t hash_value = h.digest();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment