Merge branch 'charlesmchen/linkPreviewVsTitleNewlines'

pull/1/head
Matthew Chen 6 years ago
commit 673588a98c

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -85,13 +85,13 @@ class CompareSafetyNumbersActivity: UIActivity {
// MARK: Helpers // MARK: Helpers
func numericOnly(string: String?) -> String? { func numericOnly(string: String?) -> String? {
guard (string != nil) else { guard let string = string else {
return nil return nil
} }
var numericOnly: String? var numericOnly: String?
if let regex = try? NSRegularExpression(pattern: "\\D", options: .caseInsensitive) { if let regex = try? NSRegularExpression(pattern: "\\D", options: .caseInsensitive) {
numericOnly = regex.stringByReplacingMatches(in: string!, options: .withTransparentBounds, range: NSRange(location: 0, length: string!.count), withTemplate: "") numericOnly = regex.stringByReplacingMatches(in: string, options: .withTransparentBounds, range: NSRange(location: 0, length: string.utf16.count), withTemplate: "")
} }
return numericOnly return numericOnly

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -1107,12 +1107,12 @@ class HardenedRTCSessionDescription {
// Enforce Constant bit rate. // Enforce Constant bit rate.
let cbrRegex = try! NSRegularExpression(pattern: "(a=fmtp:111 ((?!cbr=).)*)\r?\n", options: .caseInsensitive) let cbrRegex = try! NSRegularExpression(pattern: "(a=fmtp:111 ((?!cbr=).)*)\r?\n", options: .caseInsensitive)
description = cbrRegex.stringByReplacingMatches(in: description, options: [], range: NSRange(location: 0, length: description.count), withTemplate: "$1;cbr=1\r\n") description = cbrRegex.stringByReplacingMatches(in: description, options: [], range: NSRange(location: 0, length: description.utf16.count), withTemplate: "$1;cbr=1\r\n")
// Strip plaintext audio-level details // Strip plaintext audio-level details
// https://tools.ietf.org/html/rfc6464 // https://tools.ietf.org/html/rfc6464
let audioLevelRegex = try! NSRegularExpression(pattern: ".+urn:ietf:params:rtp-hdrext:ssrc-audio-level.*\r?\n", options: .caseInsensitive) let audioLevelRegex = try! NSRegularExpression(pattern: ".+urn:ietf:params:rtp-hdrext:ssrc-audio-level.*\r?\n", options: .caseInsensitive)
description = audioLevelRegex.stringByReplacingMatches(in: description, options: [], range: NSRange(location: 0, length: description.count), withTemplate: "") description = audioLevelRegex.stringByReplacingMatches(in: description, options: [], range: NSRange(location: 0, length: description.utf16.count), withTemplate: "")
return RTCSessionDescription.init(type: rtcSessionDescription.type, sdp: description) return RTCSessionDescription.init(type: rtcSessionDescription.type, sdp: description)
} }
@ -1161,7 +1161,7 @@ class HardenedRTCSessionDescription {
do { do {
let regex = try NSRegularExpression(pattern: "[\\da-f]*:[\\da-f]*:[\\da-f:\\.]*", let regex = try NSRegularExpression(pattern: "[\\da-f]*:[\\da-f]*:[\\da-f:\\.]*",
options: .caseInsensitive) options: .caseInsensitive)
return regex.stringByReplacingMatches(in: sdp, options: [], range: NSRange(location: 0, length: sdp.count), withTemplate: "[ REDACTED_IPV6_ADDRESS ]") return regex.stringByReplacingMatches(in: sdp, options: [], range: NSRange(location: 0, length: sdp.utf16.count), withTemplate: "[ REDACTED_IPV6_ADDRESS ]")
} catch { } catch {
owsFailDebug("Could not redact IPv6 addresses.") owsFailDebug("Could not redact IPv6 addresses.")
return "[Could not redact IPv6 addresses.]" return "[Could not redact IPv6 addresses.]"

@ -1,5 +1,5 @@
// //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // Copyright (c) 2019 Open Whisper Systems. All rights reserved.
// //
import Foundation import Foundation
@ -94,7 +94,7 @@ import PromiseKit
var recipientIds = [String]() var recipientIds = [String]()
for recordName in recordNames { for recordName in recordNames {
let regex = recordNamePrefixRegex let regex = recordNamePrefixRegex
guard let match: NSTextCheckingResult = regex.firstMatch(in: recordName, options: [], range: NSRange(location: 0, length: recordName.count)) else { guard let match: NSTextCheckingResult = regex.firstMatch(in: recordName, options: [], range: NSRange(location: 0, length: recordName.utf16.count)) else {
Logger.warn("no match: \(recordName)") Logger.warn("no match: \(recordName)")
continue continue
} }

@ -751,7 +751,9 @@ public class OWSLinkPreview: MTLModel {
} }
var title: String? var title: String?
if let rawTitle = NSRegularExpression.parseFirstMatch(pattern: "<meta\\s+property\\s*=\\s*\"og:title\"\\s+content\\s*=\\s*\"(.*?)\"\\s*/?>", text: linkText) { if let rawTitle = NSRegularExpression.parseFirstMatch(pattern: "<meta\\s+property\\s*=\\s*\"og:title\"\\s+content\\s*=\\s*\"(.*?)\"\\s*/?>",
text: linkText,
options: .dotMatchesLineSeparators) {
if let decodedTitle = decodeHTMLEntities(inString: rawTitle) { if let decodedTitle = decodeHTMLEntities(inString: rawTitle) {
let normalizedTitle = OWSLinkPreview.normalizeTitle(title: decodedTitle) let normalizedTitle = OWSLinkPreview.normalizeTitle(title: decodedTitle)
if normalizedTitle.count > 0 { if normalizedTitle.count > 0 {

@ -14,12 +14,13 @@ public extension NSRegularExpression {
@objc @objc
public class func parseFirstMatch(pattern: String, public class func parseFirstMatch(pattern: String,
text: String) -> String? { text: String,
options: NSRegularExpression.Options = []) -> String? {
do { do {
let regex = try NSRegularExpression(pattern: pattern) let regex = try NSRegularExpression(pattern: pattern, options: options)
guard let match = regex.firstMatch(in: text, guard let match = regex.firstMatch(in: text,
options: [], options: [],
range: NSRange(location: 0, length: text.count)) else { range: NSRange(location: 0, length: text.utf16.count)) else {
return nil return nil
} }
let matchRange = match.range(at: 1) let matchRange = match.range(at: 1)

@ -434,4 +434,24 @@ class OWSLinkPreviewTest: SSKBaseTestSwift {
self.waitForExpectations(timeout: 5.0, handler: nil) self.waitForExpectations(timeout: 5.0, handler: nil)
} }
// When using regular expressions to parse link titles, we need to use
// String.utf16.count, not String.count in the range.
func testRegexRanges() {
let regex = try! NSRegularExpression(pattern: "bob", options: [])
var text = "bob"
XCTAssertNotNil(regex.firstMatch(in: text,
options: [],
range: NSRange(location: 0, length: text.count)))
XCTAssertNotNil(regex.firstMatch(in: text,
options: [],
range: NSRange(location: 0, length: text.utf16.count)))
text = "😂😘🙂 bob"
XCTAssertNil(regex.firstMatch(in: text,
options: [],
range: NSRange(location: 0, length: text.count)))
XCTAssertNotNil(regex.firstMatch(in: text,
options: [],
range: NSRange(location: 0, length: text.utf16.count)))
}
} }

Loading…
Cancel
Save