// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #pragma once // Kotlin/JVM string semantics the file formats rely on, for UTF-8 text: lines // (lineSequence), trim (Char.isWhitespace), split (keeps empty parts), lengths and // indices in UTF-16 units (String.length, forEachIndexed), toIntOrNull (Unicode // decimal digits, as Character.digit) and toFloatOrNull (Java's Float.parseFloat // grammar). Malformed UTF-8 bytes are kept as they are and count as one unit each. #include #include namespace sizzle::io::kt { /** One code point at s[i], advancing i; malformed bytes come back one at a time. */ unsigned decodeUtf8(const std::string &s, size_t &i); /** Kotlin's Char.isWhitespace (Character.isWhitespace || isSpaceChar), BMP. */ bool isWhitespace(unsigned cp); /** Character.isDigit's decimal value of [cp] (0..9), or -1. */ int decimalDigit(unsigned cp); /** text.lineSequence(): split on CRLF, LF and CR. */ std::vector lines(const std::string &text); /** String.trim(). */ std::string trim(const std::string &s); /** String.split(c): every part, empty ones included. */ std::vector split(const std::string &s, char c); /** String.length (UTF-16 units). */ size_t utf16Length(const std::string &s); /** The code points of [s] with their UTF-16 index (as forEachIndexed sees them). */ template void forEachIndexed(const std::string &s, F f) { size_t i = 0, idx = 0; while (i < s.size()) { unsigned cp = decodeUtf8(s, i); f(idx, cp); idx += cp >= 0x10000 ? 2 : 1; } } bool startsWith(const std::string &s, const char *prefix); /** String.toIntOrNull(radix). */ bool toIntOrNull(const std::string &s, int &out, int radix = 10); /** String.toFloatOrNull(). */ bool toFloatOrNull(const std::string &s, float &out); /** Float.toInt(): NaN -> 0, saturating. */ int floatToInt(float v); /** Integer.toString(v, 16), upper-cased. */ std::string hexUpper(int v); /** padStart(n, '0'). */ std::string padStart(const std::string &s, size_t n, char c = '0'); /** replace(from, to) on every non-overlapping occurrence, left to right. */ std::string replaceAll(const std::string &s, const std::string &from, const std::string &to); } // namespace sizzle::io::kt