@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
cpp 32 lines 1.24 KB
Raw
1 #include "HybridT3Highlights.hpp"
2
3 #include <cstdint>
4 #include <mutex>
5
6 #include "t3_highlights.h"
7
8 namespace margelo::nitro::t3highlights {
9
10 namespace {
11 // The lexer's memory is process-global and every JS runtime shares it.
12 std::mutex lexerMutex;
13 } // namespace
14
15 std::optional<std::shared_ptr<ArrayBuffer>> HybridT3Highlights::tokenize(SourceText source,
16 double language) {
17 if (!(language >= 0 && language <= UINT8_MAX)) return std::nullopt;
18 const auto languageId = static_cast<uint8_t>(language);
19 if (languageId != language) return std::nullopt;
20
21 std::lock_guard lock(lexerMutex);
22 const uint32_t* records = nullptr;
23 const int64_t count = t3_highlights_tokenize(reinterpret_cast<const uint8_t*>(source.utf8.data()),
24 source.utf8.size(), languageId, &records);
25 if (count < 0) return std::nullopt;
26 const auto byteCount = static_cast<size_t>(count) * sizeof(uint32_t);
27 if (records == nullptr || byteCount == 0) return ArrayBuffer::allocate(0);
28 // The records live in the lexer's memory until its next run.
29 return ArrayBuffer::copy(reinterpret_cast<const uint8_t*>(records), byteCount);
30 }
31
32 } // namespace margelo::nitro::t3highlights