2019-03-07 16:32:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
extension String {
|
|
|
|
|
|
|
|
/// Calculates a numeric hash same as Riot Web
|
|
|
|
/// See original function here https://github.com/matrix-org/matrix-react-sdk/blob/321dd49db4fbe360fc2ff109ac117305c955b061/src/utils/FormattingUtils.js#L47
|
|
|
|
var vc_hashCode: Int32 {
|
|
|
|
var hash: Int32 = 0
|
|
|
|
|
|
|
|
for character in self {
|
|
|
|
let shiftedHash = hash << 5
|
|
|
|
hash = shiftedHash.subtractingReportingOverflow(hash).partialValue + Int32(character.vc_unicodeScalarCodePoint)
|
|
|
|
}
|
|
|
|
return abs(hash)
|
|
|
|
}
|
2019-07-25 14:44:06 +00:00
|
|
|
|
|
|
|
/// Locale-independent case-insensitive contains
|
|
|
|
/// Note: Prefer use `localizedCaseInsensitiveContains` when locale matters
|
|
|
|
///
|
|
|
|
/// - Parameter other: The other string.
|
|
|
|
/// - Returns: true if current string contains other string.
|
|
|
|
func vc_caseInsensitiveContains(_ other: String) -> Bool {
|
|
|
|
return self.range(of: other, options: .caseInsensitive) != nil
|
|
|
|
}
|
2020-12-01 10:41:31 +00:00
|
|
|
|
|
|
|
/// Returns a globally unique string
|
|
|
|
static var vc_unique: String {
|
|
|
|
return ProcessInfo.processInfo.globallyUniqueString
|
|
|
|
}
|
2021-01-13 18:49:45 +00:00
|
|
|
|
|
|
|
/// Returns a new string by removing all whitespaces from the receiver object
|
|
|
|
/// - Returns: New string without whitespaces from the receiver
|
|
|
|
func vc_removingAllWhitespaces() -> String {
|
|
|
|
return components(separatedBy: .whitespaces).joined()
|
|
|
|
}
|
2019-03-07 16:32:10 +00:00
|
|
|
}
|
2021-02-23 14:46:20 +00:00
|
|
|
|
|
|
|
extension Optional where Wrapped == String {
|
|
|
|
|
|
|
|
var isEmptyOrNil: Bool {
|
|
|
|
return self?.isEmpty ?? true
|
|
|
|
}
|
|
|
|
}
|