Implementation:Ggml org Llama cpp Jinja String
| Knowledge Sources | |
|---|---|
| Domains | Template_Engine |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Implements the `jinja::string` type with input-provenance-aware string operations for the Jinja engine.
Description
Strings are represented as vectors of `string_part`, each with an `is_input` flag tracking whether the content originated from user input. Operations like `uppercase()`, `lowercase()`, `capitalize()`, `titlecase()`, and `strip()` transform parts in place while preserving the `is_input` flag. The `append()` method concatenates by adding parts from both strings, and `mark_input_based_on()` propagates input status using the many-to-one rule (mark as input only if all source parts are input). The `str()` method flattens parts to a plain `std::string`.
Usage
This module is used internally by the Jinja engine to perform all string operations while maintaining input provenance tracking. It underpins the input marking security feature that prevents special token injection by distinguishing user-provided content from template-generated content.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/jinja/string.cpp
- Lines: 1-213
Signature
namespace jinja {
bool string_part::is_uppercase() const;
bool string_part::is_lowercase() const;
void string::mark_input();
std::string string::str() const;
size_t string::length() const;
void string::hash_update(hasher & hash) const noexcept;
bool string::all_parts_are_input() const;
bool string::is_uppercase() const;
bool string::is_lowercase() const;
void string::mark_input_based_on(const string & other);
string string::append(const string & other);
string string::uppercase();
string string::lowercase();
string string::capitalize();
string string::titlecase();
string string::strip(bool left, bool right, std::optional<const std::string_view> chars);
} // namespace jinja
Import
#include "jinja/string.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| other | const string & | Yes | Another jinja::string to append or use for input marking propagation |
| hash | hasher & | Yes | Hash object to update with this string's content |
| left | bool | Yes | Whether to strip whitespace from the left side |
| right | bool | Yes | Whether to strip whitespace from the right side |
| chars | std::optional<const std::string_view> | No | Specific characters to strip; defaults to whitespace |
Outputs
| Name | Type | Description |
|---|---|---|
| str return | std::string | Plain string with all parts concatenated |
| length return | size_t | Total length of all parts combined |
| all_parts_are_input return | bool | True if every part in the string has is_input set to true |
| is_uppercase return | bool | True if all characters in all parts are uppercase |
| is_lowercase return | bool | True if all characters in all parts are lowercase |
| transformation returns | string | Modified jinja::string with is_input flags preserved |
Usage Examples
#include "jinja/string.h"
// Create a string from user input
jinja::string user_str("Hello World", true); // is_input = true
// Transform while preserving provenance
auto upper = user_str.uppercase();
// upper.all_parts_are_input() == true (provenance preserved)
// Concatenate strings
jinja::string template_str("Prefix: ");
auto combined = template_str.append(user_str);
// combined has mixed provenance parts
// Convert to plain std::string
std::string plain = combined.str();
// "Prefix: Hello World"