From 9d2ff2fe65ff75a7bf6af26597a4782e1348a0da Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 22 Jan 2023 09:57:00 -0800 Subject: [PATCH] Fix commas and emojis getting included in hashtags Changelog-Fixed: Fix commands and emojis getting included in hashtags --- damus-c/damus.c | 21 ++++++++++++++++++++- damusTests/damusTests.swift | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/damus-c/damus.c b/damus-c/damus.c index 6ec2215..d978810 100644 --- 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 index 329572b..9d044c6 100644 --- 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: [])