mentions: fix bech32 mentions at end
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -70,14 +70,12 @@ func parse_post_bech32_mention(_ p: Parser) -> ReferencedId? {
|
||||
return nil
|
||||
}
|
||||
|
||||
var end = p.pos
|
||||
if consume_until(p, match: { c in !is_bech32_char(c) }) {
|
||||
end = p.pos
|
||||
} else {
|
||||
p.pos = start
|
||||
guard consume_until(p, match: { c in !is_bech32_char(c) }, end_ok: true) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let end = p.pos
|
||||
|
||||
let sliced = String(substring(p.str, start: start, end: end))
|
||||
guard let decoded = try? bech32_decode(sliced) else {
|
||||
p.pos = start
|
||||
|
||||
@@ -17,17 +17,22 @@ class Parser {
|
||||
}
|
||||
}
|
||||
|
||||
func consume_until(_ p: Parser, match: (Character) -> Bool) -> Bool {
|
||||
var i: Int = 0
|
||||
func consume_until(_ p: Parser, match: (Character) -> Bool, end_ok: Bool = false) -> Bool {
|
||||
let sub = substring(p.str, start: p.pos, end: p.str.count)
|
||||
let start = p.pos
|
||||
|
||||
for c in sub {
|
||||
if match(c) {
|
||||
p.pos += i
|
||||
return true
|
||||
}
|
||||
i += 1
|
||||
p.pos += 1
|
||||
}
|
||||
|
||||
if end_ok {
|
||||
return true
|
||||
}
|
||||
|
||||
p.pos = start
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user