diff options
| author | Dustin Mierau <dustin@mierau.me> | 2024-05-23 13:02:18 -0700 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2024-05-23 13:02:18 -0700 |
| commit | ded4e5978ed30175bb4ca9ce5f64f1f5a45f3ab4 (patch) | |
| tree | bd7073fd16b3bdec1ef8759660720f9393a1a544 /Hotline | |
| parent | 23b3f338f0818a53b2499692e999a0528e5d9753 (diff) | |
Convert line endings to classic Mac line endings as old Hotline servers expect.
Diffstat (limited to 'Hotline')
| -rw-r--r-- | Hotline/Hotline/HotlineClient.swift | 6 | ||||
| -rw-r--r-- | Hotline/Utility/FoundationExtensions.swift | 46 |
2 files changed, 29 insertions, 23 deletions
diff --git a/Hotline/Hotline/HotlineClient.swift b/Hotline/Hotline/HotlineClient.swift index b30ee11..b8b740b 100644 --- a/Hotline/Hotline/HotlineClient.swift +++ b/Hotline/Hotline/HotlineClient.swift @@ -516,8 +516,10 @@ class HotlineClient: NetSocketDelegate { return } + print("CONVERTED:", text.convertingLineEndings(to: .cr)) + var t = HotlineTransaction(type: .oldPostNews) - t.setFieldString(type: .data, val: text, encoding: .macOSRoman) + t.setFieldString(type: .data, val: text.convertingLineEndings(to: .cr), encoding: .macOSRoman) self.sendPacket(t) } @@ -568,7 +570,7 @@ class HotlineClient: NetSocketDelegate { t.setFieldString(type: .newsArticleTitle, val: title) t.setFieldString(type: .newsArticleDataFlavor, val: "text/plain") t.setFieldUInt32(type: .newsArticleFlags, val: 0) - t.setFieldString(type: .newsArticleData, val: text) + t.setFieldString(type: .newsArticleData, val: text.convertingLineEndings(to: .cr)) print("HotlineClient postings \(title) under \(parentID)") diff --git a/Hotline/Utility/FoundationExtensions.swift b/Hotline/Utility/FoundationExtensions.swift index 0c8966a..9cf2992 100644 --- a/Hotline/Utility/FoundationExtensions.swift +++ b/Hotline/Utility/FoundationExtensions.swift @@ -6,28 +6,32 @@ enum Endianness { case little } +enum LineEnding { + case lf // Unix-style (\n) + case crlf // Windows-style (\r\n) + case cr // Classic Mac-style (\r) +} + extension String { -// func convertToPlainText() -> String { -// var newString = self -// -// let map: [Character: Character] = [ -// "“": ", -// "”": ", -// "‘": "'", -// "’": "'", -// "\n": "\r" -// ] -// -// newString = newString.replacingOccurrences(of: "\u{0060}", with: "'") -// newString = newString.replacingOccurrences(of: "\u{00b4}", with: "'") -// newString = newString.replacingOccurrences(of: "\u{2018}", with: "'") -// newString = newString.replacingOccurrences(of: "\u{2019}", with: "'") -// newString = newString.replacingOccurrences(of: "\u{201c}", with: "'") -// newString = newString.replacingOccurrences(of: "\u{201d}", with: "'") -// newString = newString.replacingOccurrences(of: "\"", with: "\"") -// -// return newString -// } + + func convertingLineEndings(to targetEnding: LineEnding) -> String { + let lf = "\n" + let crlf = "\r\n" + let cr = "\r" + + // Normalize all line endings to LF (\n) + let normalizedString = self.replacingOccurrences(of: cr, with: lf).replacingOccurrences(of: crlf, with: lf) + + // Replace normalized LF (\n) line endings with the target line ending + switch targetEnding { + case .lf: + return normalizedString + case .crlf: + return normalizedString.replacingOccurrences(of: lf, with: crlf) + case .cr: + return normalizedString.replacingOccurrences(of: lf, with: cr) + } + } func replyToString() -> String { if self.range(of: "^Re:", options: [.regularExpression, .caseInsensitive]) != nil { |