Skip to content

Commit 79ad974

Browse files
committed
wip
1 parent cf5a0b2 commit 79ad974

File tree

7 files changed

+433
-143
lines changed

7 files changed

+433
-143
lines changed

src/apps/hideo-browser/main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <karm-main/main.h>
2+
#include <web-html/tokenizer.h>
3+
4+
Res<> entryPoint(Ctx &) {
5+
auto tokenizer = Web::Html::Tokenizer{};
6+
7+
auto html = R"(
8+
<!DOCTYPE html>
9+
<html>
10+
<head>
11+
<title>Test</title>
12+
</head>
13+
<body>
14+
<h1>Hello, world!</h1>
15+
</body>
16+
</html>
17+
)";
18+
}

src/apps/hideo-browser/manifest.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
3+
"id": "hideo-browser",
4+
"type": "exe",
5+
"description": "Browse the web",
6+
"requires": [
7+
"karm-ui",
8+
"karm-main"
9+
]
10+
}

src/libs/karm-base/ctype.h

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#pragma once
2+
3+
#include "rune.h"
4+
5+
namespace Karm {
6+
7+
/* --- Check Identity ------------------------------------------------------- */
8+
9+
constexpr bool isAscii(Rune rune) {
10+
return rune <= 0x7F;
11+
}
12+
13+
constexpr bool isAsciiDigit(Rune rune) {
14+
return rune >= '0' and rune <= '9';
15+
}
16+
17+
constexpr bool isAsciiLower(Rune rune) {
18+
return rune >= 'a' and rune <= 'z';
19+
}
20+
21+
constexpr bool isAsciiUpper(Rune rune) {
22+
return rune >= 'A' and rune <= 'Z';
23+
}
24+
25+
constexpr bool isAsciiAlpha(Rune rune) {
26+
return isAsciiLower(rune) or isAsciiUpper(rune);
27+
}
28+
29+
constexpr bool isAsciiAlphaNum(Rune rune) {
30+
return isAsciiAlpha(rune) or isAsciiDigit(rune);
31+
}
32+
33+
constexpr bool isBinDigit(Rune rune) {
34+
return rune == '0' or rune == '1';
35+
}
36+
37+
constexpr bool isOctDigit(Rune rune) {
38+
return rune >= '0' and rune <= '7';
39+
}
40+
41+
constexpr bool isDecDigit(Rune rune) {
42+
return rune >= '0' and rune <= '9';
43+
}
44+
45+
constexpr bool isHexDigit(Rune rune) {
46+
return isDecDigit(rune) or (rune >= 'a' and rune <= 'f') or (rune >= 'A' and rune <= 'F');
47+
}
48+
49+
constexpr bool isAsciiBlank(Rune rune) {
50+
return rune == ' ' or rune == '\t';
51+
}
52+
53+
constexpr bool isAsciiSpace(Rune rune) {
54+
return rune == ' ' or (rune >= '\t' and rune <= '\r');
55+
}
56+
57+
constexpr bool isAsciiPunct(Rune rune) {
58+
return (rune >= '!' and rune <= '/') or (rune >= ':' and rune <= '@') or
59+
(rune >= '[' and rune <= '`') or (rune >= '{' and rune <= '~');
60+
}
61+
62+
constexpr bool isAsciiGraph(Rune rune) {
63+
return rune >= '!' and rune <= '~';
64+
}
65+
66+
constexpr bool isAsciiPrint(Rune rune) {
67+
return rune >= ' ' and rune <= '~';
68+
}
69+
70+
constexpr bool isAsciiC0Control(Rune rune) {
71+
return rune <= 0x20;
72+
}
73+
74+
constexpr bool isAsciiControl(Rune rune) {
75+
return isAsciiC0Control(rune) or rune == 0x7F;
76+
}
77+
78+
constexpr bool isUnicode(Rune rune) {
79+
return rune <= 0x10FFFF;
80+
}
81+
82+
constexpr bool isUnicodeControl(Rune rune) {
83+
return isAsciiC0Control(rune) or (rune >= 0x7E and rune <= 0x9F);
84+
}
85+
86+
constexpr bool isUnicodeSurrogate(Rune rune) {
87+
return rune >= 0xD800 and rune <= 0xDFFF;
88+
}
89+
90+
constexpr bool isUnicodeScalar(Rune rune) {
91+
return isUnicode(rune) and not isUnicodeSurrogate(rune);
92+
}
93+
94+
constexpr bool isUnicodeNonCharacter(Rune rune) {
95+
return isUnicode(rune) and
96+
((rune >= 0xFDD0 and rune <= 0xFDEF) or
97+
((rune & 0xFFFE) == 0xFFFE) or
98+
((rune & 0xFFFF) == 0xFFFF));
99+
}
100+
101+
/* --- Rune Conversion ------------------------------------------------------ */
102+
103+
constexpr Rune toAsciiLower(Rune rune) {
104+
if (isAsciiUpper(rune))
105+
return rune + 0x20;
106+
107+
return rune;
108+
}
109+
110+
constexpr Rune toAsciiUpper(Rune rune) {
111+
if (isAsciiLower(rune))
112+
return rune - 0x20;
113+
114+
return rune;
115+
}
116+
117+
/* --- Rune Parsing --------------------------------------------------------- */
118+
119+
constexpr Rune parseAsciiDecDigit(Rune rune) {
120+
if (isDecDigit(rune))
121+
return rune - '0';
122+
123+
panic("invalid decimal digit");
124+
}
125+
126+
constexpr Rune parseAsciiHexDigit(Rune rune) {
127+
rune = toAsciiLower(rune);
128+
129+
if (isDecDigit(rune))
130+
return rune - '0';
131+
132+
if (rune >= 'a' and rune <= 'f')
133+
return rune - 'a' + 10;
134+
135+
panic("invalid hexadecimal digit");
136+
}
137+
138+
} // namespace Karm

src/libs/karm-base/std.h

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <new>
88
#include <utility>
99

10-
#include <ctype.h>
1110
#include <limits.h>
1211
#include <math.h>
1312
#include <stddef.h>

src/libs/karm-base/string.h

+48
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ordr.h"
44
#include "rune.h"
55
#include "std.h"
6+
#include "vec.h"
67

78
namespace Karm {
89

@@ -126,6 +127,53 @@ struct _String {
126127
usize len() const { return _len; }
127128
};
128129

130+
template <StaticEncoding E>
131+
struct _StringBuilder {
132+
Buf<typename E::Unit> _buf{};
133+
134+
_StringBuilder(usize cap = 16)
135+
: _buf(cap) {}
136+
137+
void append(Rune rune) {
138+
typename E::One one;
139+
if (not E::encodeUnit(rune, one)) {
140+
return;
141+
}
142+
143+
for (auto unit : iter(one)) {
144+
_buf.insert(_buf.len(), std::move(unit));
145+
}
146+
}
147+
148+
void append(_Str<E> str) {
149+
usize written = 0;
150+
for (auto rune : iterRunes(str)) {
151+
written += try$(append(rune));
152+
}
153+
}
154+
155+
void append(Slice<typename E::Unit> unit) {
156+
_buf.insert(COPY, _buf.len(), unit.buf(), unit.len());
157+
}
158+
159+
_Str<E> str() {
160+
return _buf;
161+
}
162+
163+
void clear() {
164+
_buf.truncate(0);
165+
}
166+
167+
_String<E> take() {
168+
usize len = _buf.size();
169+
_buf.insert(len, 0);
170+
171+
return {MOVE, _buf.take(), len};
172+
}
173+
};
174+
175+
using StringBuilder = _StringBuilder<Utf8>;
176+
129177
template <
130178
Sliceable S,
131179
typename E = typename S::Encoding,

src/libs/karm-io/impls.h

+22-28
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ struct Empty : public Reader {
2929
Res<usize> read(MutBytes) override { return Ok(0uz); }
3030
};
3131

32-
template <Readable Readable> struct Limit : public Reader {
32+
template <Readable Readable>
33+
struct Limit : public Reader {
3334
Readable _reader;
3435
usize _limit;
3536
usize _read;
@@ -151,10 +152,11 @@ struct BufferWriter : public Writer, public Flusher {
151152

152153
struct BitReader {
153154
Reader &_reader;
154-
u8 _bits;
155-
u8 _len;
155+
u8 _bits{};
156+
u8 _len{};
156157

157-
BitReader(Reader &reader) : _reader(reader), _bits(0), _len(0) {}
158+
BitReader(Reader &reader)
159+
: _reader(reader) {}
158160

159161
Res<u8> readBit() {
160162
if (_len == 0) {
@@ -169,7 +171,8 @@ struct BitReader {
169171
return Ok(bit);
170172
}
171173

172-
template <typename T> Res<T> readBits(usize len) {
174+
template <typename T>
175+
Res<T> readBits(usize len) {
173176
T bits = 0;
174177
for (usize i = 0; i < len; i++) {
175178
bits |= try$(readBit()) << i;
@@ -178,51 +181,42 @@ struct BitReader {
178181
}
179182
};
180183

181-
template <StaticEncoding E> struct _StringWriter : public TextWriter {
182-
Buf<typename E::Unit> _buf{};
184+
template <StaticEncoding E>
185+
struct _StringWriter :
186+
public TextWriter,
187+
public _StringBuilder<E> {
183188

184-
_StringWriter(usize cap = 16) : _buf(cap) {}
189+
_StringWriter(usize cap = 16) : _StringBuilder<E>(cap) {}
185190

186191
Res<usize> write(Bytes) override {
187192
panic("can't write raw bytes to a string");
188193
}
189194

190195
Res<usize> writeStr(Str str) override {
191196
usize written = 0;
192-
for (auto rune : iterRunes(str)) {
197+
for (auto rune : iterRunes(str))
193198
written += try$(writeRune(rune));
194-
}
199+
195200
return Ok(written);
196201
}
197202

198203
Res<usize> writeRune(Rune rune) override {
199204
typename E::One one;
200-
if (not E::encodeUnit(rune, one)) {
205+
if (not E::encodeUnit(rune, one))
201206
return Error::invalidInput("invalid rune");
202-
}
203207

204-
for (auto unit : iter(one)) {
205-
_buf.insert(_buf.len(), std::move(unit));
206-
}
208+
usize written = 0;
209+
210+
for (auto unit : iter(one))
211+
written += try$(writeUnit(unit));
207212

208-
return Ok(1uz);
213+
return Ok(written);
209214
}
210215

211216
Res<usize> writeUnit(Slice<typename E::Unit> unit) {
212-
_buf.insert(COPY, _buf.len(), unit.buf(), unit.len());
217+
append(unit);
213218
return Ok(unit.len());
214219
}
215-
216-
Str str() { return _buf; }
217-
218-
String take() {
219-
usize len = _buf.size();
220-
_buf.insert(len, 0);
221-
222-
return String{MOVE, _buf.take(), len};
223-
}
224-
225-
void clear() { _buf.truncate(0); }
226220
};
227221

228222
using StringWriter = _StringWriter<Utf8>;

0 commit comments

Comments
 (0)