Implementation:Ggml org Llama cpp Jinja String Header
| Knowledge Sources | |
|---|---|
| Domains | Template_Engine |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Declares the `jinja::string_part` and `jinja::string` types that track input provenance through string operations.
Description
The `string_part` struct pairs a `std::string` value with an `is_input` boolean flag and provides case-checking helpers. The `jinja::string` struct wraps a `vector<string_part>` and provides constructors from `std::string`, `int`, and `double`. It declares methods for marking input, converting to plain string, computing length, hashing, checking case, appending, and in-place transformations (uppercase, lowercase, capitalize, titlecase, strip). Transformation rules preserve provenance: one-to-one operations keep the `is_input` flag, one-to-many propagate it, and many-to-one mark as input only if all source parts are input.
Usage
Include this header when working with the Jinja engine's string system. It defines the foundational string type for the input marking security mechanism, enabling safe handling of user input in chat templates by tracking data provenance at the string-part level.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/jinja/string.h
- Lines: 1-61
Signature
namespace jinja {
struct string_part {
bool is_input = false;
std::string val;
bool is_uppercase() const;
bool is_lowercase() const;
};
struct string {
std::vector<string_part> parts;
string() = default;
string(const std::string & v, bool user_input = false);
string(int v);
string(double v);
void mark_input();
std::string str() const;
size_t length() const;
void hash_update(hasher & hash) const noexcept;
bool all_parts_are_input() const;
bool is_uppercase() const;
bool is_lowercase() const;
void mark_input_based_on(const string & other);
string append(const string & other);
string uppercase();
string lowercase();
string capitalize();
string titlecase();
string strip(bool left, bool right, std::optional<const std::string_view> chars = std::nullopt);
};
} // namespace jinja
Import
#include "jinja/string.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| v | const std::string & | Yes | String value to construct a jinja::string from |
| user_input | bool | No | Whether this string originates from user input (default false) |
| other | const string & | Yes | Another jinja::string for append or mark_input_based_on operations |
| left | bool | Yes | Whether to strip characters from the left side |
| right | bool | Yes | Whether to strip characters 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 concatenated string from all parts |
| length return | size_t | Total character count across all parts |
| all_parts_are_input return | bool | True if every part has is_input set |
| transformation returns | string | New jinja::string with provenance flags preserved through the transformation |
Usage Examples
#include "jinja/string.h"
// Create a string tracking user input provenance
jinja::string_part part{true, "user message"}; // is_input = true
jinja::string s("template text"); // is_input = false by default
jinja::string user_s("user text", true); // is_input = true
// Check provenance
bool from_user = user_s.all_parts_are_input(); // true
// Transform while preserving provenance
auto upper = user_s.uppercase(); // still marked as input