Skip to content

Commit bd197af

Browse files
zackeesclaude
andauthored
feat(fl::url): add parse_lnk() helper for .lnk asset link files (#2286)
Introduces fl::parse_lnk(string_view) which parses the .lnk asset link format proposed in #2284: the first non-comment, non-blank line of the file is returned as a fl::url. Lines beginning with '#' are treated as comments; trailing metadata lines are reserved for future use (sha256=..., fallback=...) and ignored by the v1 parser for forward compatibility. Also adds examples/AudioUrl/data/track.mp3.lnk demonstrating the format alongside the existing AudioUrl sketch, so the .lnk convention is visible in-tree ahead of fbuild integration. This is slice 1 of the feature described in #2284. Remaining pieces (fbuild data/ scan, WASM runtime resolution, ESP32 LittleFS packing, fl::asset handle type, UIAudio asset constructor) are tracked in the issue and will land as follow-up PRs. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 5d3ca30 commit bd197af

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# .lnk asset link file — see FastLED issue #2284.
2+
# The first non-comment line is the URL this asset resolves to.
3+
# Lines starting with '#' and blank lines are ignored.
4+
# Additional lines (metadata like sha256=, fallback=) are reserved for
5+
# future use and are ignored by the v1 parser.
6+
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3

‎src/fl/stl/url.h‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,52 @@ class url {
245245

246246
using Url = url;
247247

248+
/// Parse the contents of a `.lnk` file into a `fl::url`.
249+
///
250+
/// Format: one URL per line. Leading/trailing whitespace on each line is
251+
/// ignored. Blank lines and lines whose first non-whitespace character is
252+
/// `#` are treated as comments and skipped. The first non-comment,
253+
/// non-blank line is returned as a `fl::url`.
254+
///
255+
/// Future-compat: additional lines (metadata like `sha256=...`,
256+
/// `fallback=...`) are ignored by this v1 parser so older binaries keep
257+
/// working when future `.lnk` files grow richer.
258+
inline url parse_lnk(fl::string_view content) FL_NOEXCEPT {
259+
fl::size pos = 0;
260+
while (pos < content.size()) {
261+
fl::size eol = content.find('\n', pos);
262+
fl::size lineEnd = (eol == fl::string_view::npos) ? content.size() : eol;
263+
fl::string_view line = content.substr(pos, lineEnd - pos);
264+
pos = (eol == fl::string_view::npos) ? content.size() : eol + 1;
265+
266+
// Strip a trailing '\r' from CRLF line endings.
267+
if (!line.empty() && line[line.size() - 1] == '\r') {
268+
line = line.substr(0, line.size() - 1);
269+
}
270+
271+
// Trim leading whitespace.
272+
fl::size s = 0;
273+
while (s < line.size() &&
274+
(line[s] == ' ' || line[s] == '\t')) {
275+
++s;
276+
}
277+
// Trim trailing whitespace.
278+
fl::size e = line.size();
279+
while (e > s &&
280+
(line[e - 1] == ' ' || line[e - 1] == '\t')) {
281+
--e;
282+
}
283+
line = line.substr(s, e - s);
284+
285+
if (line.empty()) {
286+
continue;
287+
}
288+
if (line[0] == '#') {
289+
continue;
290+
}
291+
return url(line);
292+
}
293+
return url();
294+
}
295+
248296
} // namespace fl

‎tests/fl/url_lnk.cpp‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
// Tests for fl::parse_lnk — the .lnk file format parser that turns
3+
// a `data/track.mp3.lnk` file into a fl::url.
4+
5+
#include "fl/stl/string_view.h"
6+
#include "fl/stl/url.h"
7+
#include "test.h"
8+
9+
using namespace fl;
10+
11+
FL_TEST_CASE("fl::parse_lnk") {
12+
FL_SUBCASE("plain URL") {
13+
url u = parse_lnk(string_view("https://example.com/track.mp3"));
14+
FL_CHECK(u.isValid());
15+
FL_CHECK_EQ(u.host(), string_view("example.com"));
16+
}
17+
18+
FL_SUBCASE("URL with trailing newline") {
19+
url u = parse_lnk(string_view("https://example.com/track.mp3\n"));
20+
FL_CHECK(u.isValid());
21+
FL_CHECK_EQ(u.host(), string_view("example.com"));
22+
}
23+
24+
FL_SUBCASE("CRLF line endings") {
25+
url u = parse_lnk(string_view("https://example.com/track.mp3\r\n"));
26+
FL_CHECK(u.isValid());
27+
FL_CHECK_EQ(u.host(), string_view("example.com"));
28+
}
29+
30+
FL_SUBCASE("leading blank lines are skipped") {
31+
url u = parse_lnk(string_view("\n\n\nhttps://example.com/a.mp3\n"));
32+
FL_CHECK(u.isValid());
33+
FL_CHECK_EQ(u.host(), string_view("example.com"));
34+
}
35+
36+
FL_SUBCASE("# comment lines are skipped") {
37+
url u = parse_lnk(string_view(
38+
"# this is a comment\n"
39+
"# another comment\n"
40+
"https://example.com/b.mp3\n"));
41+
FL_CHECK(u.isValid());
42+
FL_CHECK_EQ(u.host(), string_view("example.com"));
43+
}
44+
45+
FL_SUBCASE("leading whitespace before URL is trimmed") {
46+
url u = parse_lnk(string_view(" \thttps://example.com/c.mp3 \n"));
47+
FL_CHECK(u.isValid());
48+
FL_CHECK_EQ(u.host(), string_view("example.com"));
49+
}
50+
51+
FL_SUBCASE("trailing metadata lines are ignored") {
52+
url u = parse_lnk(string_view(
53+
"https://example.com/d.mp3\n"
54+
"sha256=abcdef\n"
55+
"fallback=https://mirror.example.com/d.mp3\n"));
56+
FL_CHECK(u.isValid());
57+
FL_CHECK_EQ(u.host(), string_view("example.com"));
58+
}
59+
60+
FL_SUBCASE("empty content returns invalid url") {
61+
url u = parse_lnk(string_view(""));
62+
FL_CHECK_FALSE(u.isValid());
63+
}
64+
65+
FL_SUBCASE("only comments returns invalid url") {
66+
url u = parse_lnk(string_view(
67+
"# comment one\n"
68+
" # indented comment\n"
69+
"\n"));
70+
FL_CHECK_FALSE(u.isValid());
71+
}
72+
73+
FL_SUBCASE("indented comment is still a comment") {
74+
url u = parse_lnk(string_view(
75+
" # leading space then hash\n"
76+
"https://example.com/e.mp3\n"));
77+
FL_CHECK(u.isValid());
78+
FL_CHECK_EQ(u.host(), string_view("example.com"));
79+
}
80+
}

0 commit comments

Comments
 (0)