diff --git a/damus-c/cursor.h b/damus-c/cursor.h index 82e3f228..2d206516 100644 --- a/damus-c/cursor.h +++ b/damus-c/cursor.h @@ -10,6 +10,7 @@ #include #include +#include "bech32.h" typedef unsigned char u8; @@ -31,6 +32,10 @@ static inline int is_invalid_url_ending(char c) { return c == '!' || c == '?' || c == ')' || c == '.' || c == ',' || c == ';'; } +static inline int is_bech32_character(char c) { + return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || bech32_charset_rev[c] != -1; +} + static inline void make_cursor(struct cursor *c, const u8 *content, size_t len) { c->start = content; @@ -70,6 +75,23 @@ static inline int consume_until_whitespace(struct cursor *cur, int or_end) { return or_end; } +static inline int consume_until_non_bech32_character(struct cursor *cur, int or_end) { + char c; + int consumedAtLeastOne = 0; + + while (cur->p < cur->end) { + c = *cur->p; + + if (!is_bech32_character(c)) + return consumedAtLeastOne; + + cur->p++; + consumedAtLeastOne = 1; + } + + return or_end; +} + static inline int parse_char(struct cursor *cur, char c) { if (cur->p >= cur->end) return 0; diff --git a/damus-c/nostr_bech32.c b/damus-c/nostr_bech32.c index 8d993e9b..646ca8df 100644 --- a/damus-c/nostr_bech32.c +++ b/damus-c/nostr_bech32.c @@ -222,7 +222,7 @@ int parse_nostr_bech32(struct cursor *cur, struct nostr_bech32 *obj) { start = cur->p; - if (!consume_until_whitespace(cur, 1)) { + if (!consume_until_non_bech32_character(cur, 1)) { cur->p = start; return 0; }