- Three source trees: core/ (shared C++ engine, moved from ios/core), app/ (Android UI), ios/app/ (iOS UI) - Android plays through core/ via JNI (EngineHost, jni_engine.cpp): the Oboe callback renders natively; the Kotlin model is mirrored as project text and applied as a diff, so playback continues through edits - The Kotlin engine moves to the unit tests as ReferenceEngine (golden reference) - Shared MasterRecorder in core/src/host, used by both apps; host_test - versions.properties: one versionName/versionCode for both platforms, continuing Android's numbering (iOS 0.6.0/11 -> 0.37.0/71) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
216 lines
7.8 KiB
C++
216 lines
7.8 KiB
C++
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "io/KotlinText.hpp"
|
|
|
|
#include <climits>
|
|
#include <clocale>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace sizzle::io::kt {
|
|
|
|
unsigned decodeUtf8(const std::string &s, size_t &i) {
|
|
const unsigned char c0 = (unsigned char)s[i];
|
|
if (c0 < 0x80) { ++i; return c0; }
|
|
int n; unsigned cp, min;
|
|
if ((c0 & 0xE0) == 0xC0) { n = 1; cp = c0 & 0x1F; min = 0x80; }
|
|
else if ((c0 & 0xF0) == 0xE0) { n = 2; cp = c0 & 0x0F; min = 0x800; }
|
|
else if ((c0 & 0xF8) == 0xF0) { n = 3; cp = c0 & 0x07; min = 0x10000; }
|
|
else { ++i; return 0xFFFD; }
|
|
if (i + (size_t)n >= s.size()) { ++i; return 0xFFFD; }
|
|
for (int k = 1; k <= n; ++k) {
|
|
const unsigned char c = (unsigned char)s[i + (size_t)k];
|
|
if ((c & 0xC0) != 0x80) { ++i; return 0xFFFD; }
|
|
cp = (cp << 6) | (c & 0x3F);
|
|
}
|
|
if (cp < min || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) { ++i; return 0xFFFD; }
|
|
i += (size_t)n + 1;
|
|
return cp;
|
|
}
|
|
|
|
bool isWhitespace(unsigned cp) {
|
|
return (cp >= 0x09 && cp <= 0x0D) || (cp >= 0x1C && cp <= 0x20) || cp == 0xA0 || cp == 0x1680 ||
|
|
(cp >= 0x2000 && cp <= 0x200A) || cp == 0x2028 || cp == 0x2029 || cp == 0x202F || cp == 0x205F ||
|
|
cp == 0x3000;
|
|
}
|
|
|
|
// Zeros of the BMP's Nd (decimal digit) runs, Unicode 13 (JDK 17). Supplementary
|
|
// digits arrive in Kotlin as surrogate pairs, which are not digits.
|
|
static const unsigned kDigitZeros[] = {
|
|
0x0030, 0x0660, 0x06F0, 0x07C0, 0x0966, 0x09E6, 0x0A66, 0x0AE6, 0x0B66, 0x0BE6, 0x0C66, 0x0CE6,
|
|
0x0D66, 0x0DE6, 0x0E50, 0x0ED0, 0x0F20, 0x1040, 0x1090, 0x17E0, 0x1810, 0x1946, 0x19D0, 0x1A80,
|
|
0x1A90, 0x1B50, 0x1BB0, 0x1C40, 0x1C50, 0xA620, 0xA8D0, 0xA900, 0xA9D0, 0xA9F0, 0xAA50, 0xABF0, 0xFF10};
|
|
|
|
int decimalDigit(unsigned cp) {
|
|
for (unsigned z : kDigitZeros)
|
|
if (cp >= z && cp < z + 10) return (int)(cp - z);
|
|
return -1;
|
|
}
|
|
|
|
/** Character.digit(cp, radix). */
|
|
static int digitOf(unsigned cp, int radix) {
|
|
int d = decimalDigit(cp);
|
|
if (d < 0) {
|
|
if (cp >= 'a' && cp <= 'z') d = (int)(cp - 'a') + 10;
|
|
else if (cp >= 'A' && cp <= 'Z') d = (int)(cp - 'A') + 10;
|
|
else if (cp >= 0xFF41 && cp <= 0xFF5A) d = (int)(cp - 0xFF41) + 10;
|
|
else if (cp >= 0xFF21 && cp <= 0xFF3A) d = (int)(cp - 0xFF21) + 10;
|
|
}
|
|
return d >= 0 && d < radix ? d : -1;
|
|
}
|
|
|
|
std::vector<std::string> lines(const std::string &text) {
|
|
std::vector<std::string> out;
|
|
size_t start = 0;
|
|
for (size_t i = 0; i < text.size(); ++i) {
|
|
if (text[i] == '\n' || text[i] == '\r') {
|
|
out.emplace_back(text, start, i - start);
|
|
if (text[i] == '\r' && i + 1 < text.size() && text[i + 1] == '\n') ++i;
|
|
start = i + 1;
|
|
}
|
|
}
|
|
out.emplace_back(text, start, text.size() - start);
|
|
return out;
|
|
}
|
|
|
|
std::string trim(const std::string &s) {
|
|
size_t first = s.size(), last = 0; // [first, last) of non-whitespace
|
|
size_t i = 0;
|
|
while (i < s.size()) {
|
|
size_t at = i;
|
|
unsigned cp = decodeUtf8(s, i);
|
|
if (!isWhitespace(cp)) { if (first == s.size()) first = at; last = i; }
|
|
}
|
|
return first == s.size() ? std::string() : s.substr(first, last - first);
|
|
}
|
|
|
|
std::vector<std::string> split(const std::string &s, char c) {
|
|
std::vector<std::string> out;
|
|
size_t start = 0;
|
|
for (size_t i = 0; i < s.size(); ++i)
|
|
if (s[i] == c) { out.emplace_back(s, start, i - start); start = i + 1; }
|
|
out.emplace_back(s, start, s.size() - start);
|
|
return out;
|
|
}
|
|
|
|
size_t utf16Length(const std::string &s) {
|
|
size_t n = 0;
|
|
forEachIndexed(s, [&](size_t idx, unsigned cp) { n = idx + (cp >= 0x10000 ? 2 : 1); });
|
|
return n;
|
|
}
|
|
|
|
bool startsWith(const std::string &s, const char *prefix) {
|
|
size_t n = std::strlen(prefix);
|
|
return s.size() >= n && s.compare(0, n, prefix) == 0;
|
|
}
|
|
|
|
bool toIntOrNull(const std::string &s, int &out, int radix) {
|
|
if (s.empty()) return false;
|
|
size_t i = 0;
|
|
bool negative = false;
|
|
if ((unsigned char)s[0] < '0') { // a possible sign (Kotlin: firstChar < '0')
|
|
if (s.size() == 1) return false;
|
|
if (s[0] == '-') negative = true;
|
|
else if (s[0] != '+') return false;
|
|
i = 1;
|
|
}
|
|
int64_t v = 0;
|
|
const int64_t limit = negative ? -(int64_t)INT_MIN : (int64_t)INT_MAX;
|
|
while (i < s.size()) {
|
|
int d = digitOf(decodeUtf8(s, i), radix);
|
|
if (d < 0) return false;
|
|
v = v * radix + d;
|
|
if (v > limit) return false;
|
|
}
|
|
out = (int)(negative ? -v : v);
|
|
return true;
|
|
}
|
|
|
|
// Java's Double.valueOf grammar (what Kotlin screens toFloatOrNull with):
|
|
// [\x00-\x20]*[+-]?(NaN|Infinity|((Number)[fFdD]?))[\x00-\x20]*
|
|
bool toFloatOrNull(const std::string &str, float &out) {
|
|
size_t b = 0, e = str.size();
|
|
while (b < e && (unsigned char)str[b] <= 0x20) ++b;
|
|
while (e > b && (unsigned char)str[e - 1] <= 0x20) --e;
|
|
if (b == e) return false;
|
|
std::string s = str.substr(b, e - b);
|
|
size_t i = 0;
|
|
bool neg = false;
|
|
if (s[i] == '+' || s[i] == '-') { neg = s[i] == '-'; ++i; }
|
|
std::string rest = s.substr(i);
|
|
if (rest == "NaN") { out = NAN; return true; }
|
|
if (rest == "Infinity") { out = neg ? -INFINITY : INFINITY; return true; }
|
|
auto isDec = [](char c) { return c >= '0' && c <= '9'; };
|
|
auto isHex = [](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); };
|
|
size_t j = i;
|
|
auto run = [&](auto pred) { size_t n = 0; while (j < s.size() && pred(s[j])) { ++j; ++n; } return n; };
|
|
if (j + 1 < s.size() && s[j] == '0' && (s[j + 1] == 'x' || s[j + 1] == 'X')) {
|
|
j += 2;
|
|
size_t a = run(isHex), f = 0;
|
|
bool dot = j < s.size() && s[j] == '.';
|
|
if (dot) { ++j; f = run(isHex); }
|
|
if (a == 0 && f == 0) return false;
|
|
if (j >= s.size() || (s[j] != 'p' && s[j] != 'P')) return false;
|
|
++j;
|
|
if (j < s.size() && (s[j] == '+' || s[j] == '-')) ++j;
|
|
if (run(isDec) == 0) return false;
|
|
} else {
|
|
size_t a = run(isDec), f = 0;
|
|
if (j < s.size() && s[j] == '.') { ++j; f = run(isDec); }
|
|
if (a == 0 && f == 0) return false;
|
|
if (j < s.size() && (s[j] == 'e' || s[j] == 'E')) {
|
|
++j;
|
|
if (j < s.size() && (s[j] == '+' || s[j] == '-')) ++j;
|
|
if (run(isDec) == 0) return false;
|
|
}
|
|
}
|
|
size_t numEnd = j;
|
|
if (j < s.size() && (s[j] == 'f' || s[j] == 'F' || s[j] == 'd' || s[j] == 'D')) ++j;
|
|
if (j != s.size()) return false;
|
|
std::string num = s.substr(0, numEnd);
|
|
// strtof reads the C locale's decimal point; keep it right if a host changed it.
|
|
const char *dp = std::localeconv()->decimal_point;
|
|
if (dp && std::strcmp(dp, ".") != 0) {
|
|
size_t p = num.find('.');
|
|
if (p != std::string::npos) num.replace(p, 1, dp);
|
|
}
|
|
out = std::strtof(num.c_str(), nullptr);
|
|
return true;
|
|
}
|
|
|
|
int floatToInt(float v) {
|
|
if (std::isnan(v)) return 0;
|
|
if (v >= 2147483648.f) return INT_MAX;
|
|
if (v <= -2147483648.f) return INT_MIN;
|
|
return (int)v;
|
|
}
|
|
|
|
std::string hexUpper(int v) {
|
|
uint32_t m = v < 0 ? (uint32_t)0 - (uint32_t)v : (uint32_t)v;
|
|
std::string r;
|
|
do { r.insert(r.begin(), "0123456789ABCDEF"[m & 15]); m >>= 4; } while (m);
|
|
return v < 0 ? "-" + r : r;
|
|
}
|
|
|
|
std::string padStart(const std::string &s, size_t n, char c) {
|
|
size_t len = utf16Length(s);
|
|
return len >= n ? s : std::string(n - len, c) + s;
|
|
}
|
|
|
|
std::string replaceAll(const std::string &s, const std::string &from, const std::string &to) {
|
|
if (from.empty()) return s;
|
|
std::string out;
|
|
size_t pos = 0;
|
|
for (size_t f; (f = s.find(from, pos)) != std::string::npos; pos = f + from.size()) {
|
|
out.append(s, pos, f - pos);
|
|
out += to;
|
|
}
|
|
out.append(s, pos, std::string::npos);
|
|
return out;
|
|
}
|
|
|
|
} // namespace sizzle::io::kt
|