damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 9d2ff2fe65ff75a7bf6af26597a4782e1348a0da
parent 13ea42a2e26e2e6b2015e251f4122d6ee539c432
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 22 Jan 2023 09:57:00 -0800

Fix commas and emojis getting included in hashtags

Changelog-Fixed: Fix commands and emojis getting included in hashtags

Diffstat:
Mdamus-c/damus.c | 21++++++++++++++++++++-
MdamusTests/damusTests.swift | 20++++++++++++++++++++
2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/damus-c/damus.c b/damus-c/damus.c @@ -22,6 +22,10 @@ static inline int is_whitespace(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'; } +static inline int is_boundary(char c) { + return !isalnum(c); +} + static void make_cursor(struct cursor *c, const u8 *content, size_t len) { c->start = content; @@ -29,6 +33,21 @@ static void make_cursor(struct cursor *c, const u8 *content, size_t len) c->p = content; } +static int consume_until_boundary(struct cursor *cur) { + char c; + + while (cur->p < cur->end) { + c = *cur->p; + + if (is_boundary(c)) + return 1; + + cur->p++; + } + + return 1; +} + static int consume_until_whitespace(struct cursor *cur, int or_end) { char c; bool consumedAtLeastOne = false; @@ -147,7 +166,7 @@ static int parse_hashtag(struct cursor *cur, struct block *block) { return 0; } - consume_until_whitespace(cur, 1); + consume_until_boundary(cur); block->type = BLOCK_HASHTAG; block->block.str.start = (const char*)(start + 1); diff --git a/damusTests/damusTests.swift b/damusTests/damusTests.swift @@ -145,6 +145,26 @@ class damusTests: XCTestCase { XCTAssertEqual(parsed[2].is_text, " derp") } + func testHashtagWithComma() { + let parsed = parse_mentions(content: "some hashtag #bitcoin, cool", tags: []) + + XCTAssertNotNil(parsed) + XCTAssertEqual(parsed.count, 3) + XCTAssertEqual(parsed[0].is_text, "some hashtag ") + XCTAssertEqual(parsed[1].is_hashtag, "bitcoin") + XCTAssertEqual(parsed[2].is_text, ", cool") + } + + func testHashtagWithEmoji() { + let parsed = parse_mentions(content: "some hashtag #bitcoin☕️ cool", tags: []) + + XCTAssertNotNil(parsed) + XCTAssertEqual(parsed.count, 3) + XCTAssertEqual(parsed[0].is_text, "some hashtag ") + XCTAssertEqual(parsed[1].is_hashtag, "bitcoin") + XCTAssertEqual(parsed[2].is_text, "☕️ cool") + } + func testParseHashtagEnd() { let parsed = parse_mentions(content: "some hashtag #bitcoin", tags: [])