damus

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

commit c439100a864997d67b997784339d25dffd431f8b
parent fc3a86027f49a5a6976cc743e932a905212b2371
Author: Joshua Jiang <zhuoshengjiang@gmail.com>
Date:   Sat, 15 Apr 2023 02:40:56 -0700

Fix parse mention without space in content

Changelog-Fixed: Fix parse mention without space in content
Closes: #936
Fixes: #992

Diffstat:
Mdamus-c/damus.c | 2+-
Mdamus.xcodeproj/project.pbxproj | 12++++++++++++
AdamusTests/Models/DamusParseContentTests.swift | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/damus-c/damus.c b/damus-c/damus.c @@ -209,7 +209,7 @@ int damus_parse_content(struct blocks *blocks, const char *content) { c = peek_char(&cur, 0); pre_mention = cur.p; - if (cp == -1 || is_whitespace(cp)) { + if (cp == -1 || is_whitespace(cp) || c == '#') { if (c == '#' && (parse_mention_index(&cur, &block) || parse_hashtag(&cur, &block))) { if (!add_text_then_block(&cur, blocks, block, &start, pre_mention)) return 0; diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -292,6 +292,7 @@ F79C7FAD29D5E9620000F946 /* EditProfilePictureControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79C7FAC29D5E9620000F946 /* EditProfilePictureControl.swift */; }; F7F0BA25297892BD009531F3 /* SwipeToDismiss.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F0BA24297892BD009531F3 /* SwipeToDismiss.swift */; }; F7F0BA272978E54D009531F3 /* ParticipantsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F0BA262978E54D009531F3 /* ParticipantsView.swift */; }; + F944F56E29EA9CCC0067B3BF /* DamusParseContentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F944F56D29EA9CCC0067B3BF /* DamusParseContentTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -717,6 +718,7 @@ F79C7FAC29D5E9620000F946 /* EditProfilePictureControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditProfilePictureControl.swift; sourceTree = "<group>"; }; F7F0BA24297892BD009531F3 /* SwipeToDismiss.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwipeToDismiss.swift; sourceTree = "<group>"; }; F7F0BA262978E54D009531F3 /* ParticipantsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParticipantsView.swift; sourceTree = "<group>"; }; + F944F56D29EA9CCC0067B3BF /* DamusParseContentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamusParseContentTests.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -1259,6 +1261,7 @@ 4CE6DEF627F7A08200C66700 /* damusTests */ = { isa = PBXGroup; children = ( + F944F56C29EA9CB20067B3BF /* Models */, 50A50A8C29A09E1C00C01BE7 /* RequestTests.swift */, DD597CBC2963D85A00C64D32 /* MarkdownTests.swift */, 4C90BD1B283AC38E008EE7EF /* Bech32Tests.swift */, @@ -1381,6 +1384,14 @@ path = Modifiers; sourceTree = "<group>"; }; + F944F56C29EA9CB20067B3BF /* Models */ = { + isa = PBXGroup; + children = ( + F944F56D29EA9CCC0067B3BF /* DamusParseContentTests.swift */, + ); + path = Models; + sourceTree = "<group>"; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -1826,6 +1837,7 @@ 4C363A9E2828A822006E126D /* ReplyTests.swift in Sources */, 4CB883AA297612FF00DC99E7 /* ZapTests.swift in Sources */, 4CB8839A297322D200DC99E7 /* DMTests.swift in Sources */, + F944F56E29EA9CCC0067B3BF /* DamusParseContentTests.swift in Sources */, 4CB883AE2976FA9300DC99E7 /* FormatTests.swift in Sources */, 4C363AA02828A8DD006E126D /* LikeTests.swift in Sources */, 4C90BD1C283AC38E008EE7EF /* Bech32Tests.swift in Sources */, diff --git a/damusTests/Models/DamusParseContentTests.swift b/damusTests/Models/DamusParseContentTests.swift @@ -0,0 +1,64 @@ +// +// DamusParseContentTests.swift +// damusTests +// +// Created by Joshua Jiang on 4/15/23. +// + +import XCTest +@testable import damus + +class DamusParseContentTests: XCTestCase { + + private let decoder = JSONDecoder() + + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func test_damus_parse_content_can_parse_mention_without_white_space_at_front() throws { + var bs = blocks() + bs.num_blocks = 0; + + blocks_init(&bs) + + let content = "#[0]​, #[1]​,#[2]​,#[3]#[4]​,#[5]​,#[6]​,#[7]​, #[8]​, \n#[9]​, #[10]​, #[11]​, #[12]​" + + let tagsString = "[[\"p\",\"82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2\"],[\"p\",\"0339bb0d9d818ba126a39385a5edee5651993af7c21f18d4ceb0ba8c9de0d463\"],[\"p\",\"e7424ad457e512fdf4764a56bf6d428a06a13a1006af1fb8e0fe32f6d03265c7\"],[\"p\",\"520830c334a3f79f88cac934580d26f91a7832c6b21fb9625690ea2ed81b5626\"],[\"p\",\"971615b70ad9ec896f8d5ba0f2d01652f1dfe5f9ced81ac9469ca7facefad68b\"],[\"p\",\"2779f3d9f42c7dee17f0e6bcdcf89a8f9d592d19e3b1bbd27ef1cffd1a7f98d1\"],[\"p\",\"17538dc2a62769d09443f18c37cbe358fab5bbf981173542aa7c5ff171ed77c4\"],[\"p\",\"985a7c6b0e75508ad74c4110b2e52dfba6ce26063d80bca218564bd083a72b99\"],[\"p\",\"7fb2a29bd1a41d9a8ca43a19a7dcf3a8522f1bc09b4086253539190e9c29c51a\"],[\"p\",\"b88c7f007bbf3bc2fcaeff9e513f186bab33782c0baa6a6cc12add78b9110ba3\"],[\"p\",\"2f4fa408d85b962d1fe717daae148a4c98424ab2e10c7dd11927e101ed3257b2\"],[\"p\",\"bd1e19980e2c91e6dc657e92c25762ca882eb9272d2579e221f037f93788de91\"],[\"p\",\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\"]]" + + let tags = try decoder.decode([[String]].self, from: tagsString.data(using: .utf8)!) + + let bytes = content.utf8CString + + let _ = bytes.withUnsafeBufferPointer { p in + damus_parse_content(&bs, p.baseAddress) + } + + let isMentionBlockIndexList = [0,2,4,6,7,9,11,13,15,17,19,21,23] + let isMentionBlockSet = Set(isMentionBlockIndexList) + + + + var i = 0 + while (i < bs.num_blocks) { + let block = bs.blocks[i] + + guard let currentBlock = convert_block(block, tags: tags) else { + XCTFail("Cannot parse block") + return + } + + if currentBlock.is_mention { + XCTAssert(isMentionBlockSet.contains(i)) + } else { + XCTAssert(!isMentionBlockSet.contains(i)) + } + + i += 1 + } + } +}