Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
utf8_safe.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include <string>
5#include <cstddef>
6
19namespace entropic::facade {
20
32inline std::string utf8_safe_substr(const std::string& s, std::size_t max_bytes) {
33 if (s.size() <= max_bytes) { return s; }
34 std::size_t cut = max_bytes;
35 while (cut > 0
36 && (static_cast<unsigned char>(s[cut]) & 0xC0) == 0x80) {
37 --cut;
38 }
39 return s.substr(0, cut);
40}
41
42} // namespace entropic::facade
std::string utf8_safe_substr(const std::string &s, std::size_t max_bytes)
Truncate a UTF-8 string at-or-before a byte cap on a codepoint boundary.
Definition utf8_safe.h:32