Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/text/text.cpp

sdl_runtime_compiler git · main

SDL3 game for running and compiling code at runtime

sdl3 c++ compiler dlopen cmake · first commit 2026-04-19 · last commit 2026-07-03 (3 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-runtime-compiler

C++ 72.3% C 26.2%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler/archive/sdl_runtime_compiler.tar.gz
src/text/text.cpp 2.2 KB · 100 lines raw
#include "text.hpp"

namespace
Nimbin
{

namespace
Text
{


DynArray<std::string>
wrap_String(const std::string& text, size_t max_chars)
{
    std::string word;
    std::string current_line;

    DynArray<std::string> lines;

    auto flush_line = [&]() {
        while (!current_line.empty() && current_line.back() == ' ')
            current_line.pop_back();
        lines.push_back(current_line);
        current_line.clear();
    };

    auto flush_word = [&]() {
        if (word.empty()) return;
        if (current_line.size() + word.size() + (current_line.empty() ? 0 : 1)
                > max_chars) {
            flush_line();
        }
        if (!current_line.empty()) current_line += ' ';
        current_line += word;
        word.clear();
    };

    for (char c : text) {
        if (c == '\n') {
            flush_word();
            flush_line();
        }
        else if (c == ' ' || c == '\t') {
            flush_word();
        }
        else {
            word += c;
        }
    }
    flush_word();
    flush_line();

    return lines;
}


std::string
ascii_Sanitize(const std::string& in)
{
    std::string out;
    out.reserve(in.size());

    size_t i = 0;
    while (i < in.size()) {
        const unsigned char c = static_cast<unsigned char>(in[i]);

        if (c < 0x80) {
            out += static_cast<char>(c);
            ++i;
            continue;
        }

        // U+2000–U+20FF punctuation block: E2 80 xx
        if (c == 0xE2 && i + 2 < in.size() &&
            static_cast<unsigned char>(in[i + 1]) == 0x80) {
            const unsigned char c3 = static_cast<unsigned char>(in[i + 2]);
            switch (c3) {
                case 0x98: case 0x99: out += '\''; break;  // ‘ ’
                case 0x9C: case 0x9D: out += '"';  break;  // “ ”
                case 0x93: case 0x94: out += '-';  break;  // – —
                default:              out += '\''; break;
            }
            i += 3;
            continue;
        }

        // Any other multibyte sequence: one '?', skip continuation bytes.
        out += '?';
        ++i;
        while (i < in.size() &&
               (static_cast<unsigned char>(in[i]) & 0xC0) == 0x80)
            ++i;
    }
    return out;
}


} // namespace Text

} // namespace Nimbin