Add prefix and suffix string trimming functions

This is needed to fix extraneous whitespace issues in image posts
This commit is contained in:
William Casarin
2023-04-05 12:48:03 -07:00
parent b9fed6e4eb
commit b131c74ee3
2 changed files with 18 additions and 0 deletions

View File

@@ -346,3 +346,14 @@ struct TruncatedText: View {
return AttributedString(truncatedAttributedString) + "..." return AttributedString(truncatedAttributedString) + "..."
} }
} }
// trim suffix whitespace and newlines
func trim_suffix(_ str: String) -> String {
return str.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
}
// trim prefix whitespace and newlines
func trim_prefix(_ str: String) -> String {
return str.replacingOccurrences(of: "^\\s+", with: "", options: .regularExpression)
}

View File

@@ -40,6 +40,13 @@ class damusTests: XCTestCase {
XCTAssertEqual(bytes.count, 32) XCTAssertEqual(bytes.count, 32)
} }
func testTrimmingFunctions() {
let txt = " bobs "
XCTAssertEqual(trim_prefix(txt), "bobs ")
XCTAssertEqual(trim_suffix(txt), " bobs")
}
func testParseMentionWithMarkdown() { func testParseMentionWithMarkdown() {
let md = """ let md = """
Testing markdown in damus Testing markdown in damus