mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib] Add missing 10.10 NSString APIs.
containsString and localizedCaseInsensitiveContainsString were introduced in 10.10, release-noted, but never documented (<rdar://22236574>), so we missed them. Fixes <rdar://18776075> String.containsString doesn't work in Swift Swift SVN r31152
This commit is contained in:
@@ -1601,5 +1601,46 @@ extension String {
|
|||||||
) -> String? {
|
) -> String? {
|
||||||
return _ns.stringByApplyingTransform(transform, reverse: reverse)
|
return _ns.stringByApplyingTransform(transform, reverse: reverse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//===--- From the 10.10 release notes; not in public documentation ------===//
|
||||||
|
// No need to make these unavailable on earlier OSes, since they can
|
||||||
|
// forward trivially to rangeOfString.
|
||||||
|
|
||||||
|
/// Returns `true` iff `other` is non-empty and contained within
|
||||||
|
/// `self` by case-sensitive, non-literal search.
|
||||||
|
///
|
||||||
|
/// Equivalent to `self.rangeOfString(other) != nil`
|
||||||
|
@warn_unused_result
|
||||||
|
public func containsString(other: String) -> Bool {
|
||||||
|
let r = self.rangeOfString(other) != nil
|
||||||
|
if #available(OSX 10.10, iOS 8.0, *) {
|
||||||
|
_sanityCheck(r == _ns.containsString(other))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` iff `other` is non-empty and contained within
|
||||||
|
/// `self` by case-insensitive, non-literal search, taking into
|
||||||
|
/// account the current locale.
|
||||||
|
///
|
||||||
|
/// Locale-independent case-insensitive operation, and other needs,
|
||||||
|
/// can be achieved by calling
|
||||||
|
/// `rangeOfString(_:options:_,range:_locale:_)`.
|
||||||
|
///
|
||||||
|
/// Equivalent to
|
||||||
|
///
|
||||||
|
/// self.rangeOfString(
|
||||||
|
/// other, options: .CaseInsensitiveSearch,
|
||||||
|
/// locale: NSLocale.currentLocale()) != nil
|
||||||
|
@warn_unused_result
|
||||||
|
public func localizedCaseInsensitiveContainsString(other: String) -> Bool {
|
||||||
|
let r = self.rangeOfString(
|
||||||
|
other, options: .CaseInsensitiveSearch, locale: NSLocale.currentLocale()
|
||||||
|
) != nil
|
||||||
|
if #available(OSX 10.10, iOS 8.0, *) {
|
||||||
|
_sanityCheck(r == _ns.localizedCaseInsensitiveContainsString(other))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1176,6 +1176,62 @@ NSStringAPIs.test("rangeOfString(_:options:range:locale:)") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NSStringAPIs.test("containsString(_:)") {
|
||||||
|
withOverriddenNSLocaleCurrentLocale("en") { () -> () in
|
||||||
|
expectFalse("".containsString(""))
|
||||||
|
expectFalse("".containsString("a"))
|
||||||
|
expectFalse("a".containsString(""))
|
||||||
|
expectFalse("a".containsString("b"))
|
||||||
|
expectTrue("a".containsString("a"))
|
||||||
|
expectFalse("a".containsString("A"))
|
||||||
|
expectFalse("A".containsString("a"))
|
||||||
|
expectFalse("a".containsString("a\u{0301}"))
|
||||||
|
expectTrue("a\u{0301}".containsString("a\u{0301}"))
|
||||||
|
expectFalse("a\u{0301}".containsString("a"))
|
||||||
|
expectTrue("a\u{0301}".containsString("\u{0301}"))
|
||||||
|
expectFalse("a".containsString("\u{0301}"))
|
||||||
|
|
||||||
|
expectFalse("i".containsString("I"))
|
||||||
|
expectFalse("I".containsString("i"))
|
||||||
|
expectFalse("\u{0130}".containsString("i"))
|
||||||
|
expectFalse("i".containsString("\u{0130}"))
|
||||||
|
|
||||||
|
return ()
|
||||||
|
}
|
||||||
|
|
||||||
|
withOverriddenNSLocaleCurrentLocale("tr") {
|
||||||
|
expectFalse("\u{0130}".containsString("ı"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
NSStringAPIs.test("localizedCaseInsensitiveContainsString(_:)") {
|
||||||
|
withOverriddenNSLocaleCurrentLocale("en") { () -> () in
|
||||||
|
expectFalse("".localizedCaseInsensitiveContainsString(""))
|
||||||
|
expectFalse("".localizedCaseInsensitiveContainsString("a"))
|
||||||
|
expectFalse("a".localizedCaseInsensitiveContainsString(""))
|
||||||
|
expectFalse("a".localizedCaseInsensitiveContainsString("b"))
|
||||||
|
expectTrue("a".localizedCaseInsensitiveContainsString("a"))
|
||||||
|
expectTrue("a".localizedCaseInsensitiveContainsString("A"))
|
||||||
|
expectTrue("A".localizedCaseInsensitiveContainsString("a"))
|
||||||
|
expectFalse("a".localizedCaseInsensitiveContainsString("a\u{0301}"))
|
||||||
|
expectTrue("a\u{0301}".localizedCaseInsensitiveContainsString("a\u{0301}"))
|
||||||
|
expectFalse("a\u{0301}".localizedCaseInsensitiveContainsString("a"))
|
||||||
|
expectTrue("a\u{0301}".localizedCaseInsensitiveContainsString("\u{0301}"))
|
||||||
|
expectFalse("a".localizedCaseInsensitiveContainsString("\u{0301}"))
|
||||||
|
|
||||||
|
expectTrue("i".localizedCaseInsensitiveContainsString("I"))
|
||||||
|
expectTrue("I".localizedCaseInsensitiveContainsString("i"))
|
||||||
|
expectFalse("\u{0130}".localizedCaseInsensitiveContainsString("i"))
|
||||||
|
expectFalse("i".localizedCaseInsensitiveContainsString("\u{0130}"))
|
||||||
|
|
||||||
|
return ()
|
||||||
|
}
|
||||||
|
|
||||||
|
withOverriddenNSLocaleCurrentLocale("tr") {
|
||||||
|
expectFalse("\u{0130}".localizedCaseInsensitiveContainsString("ı"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NSStringAPIs.test("localizedStandardContainsString(_:)") {
|
NSStringAPIs.test("localizedStandardContainsString(_:)") {
|
||||||
if #available(OSX 10.11, iOS 9.0, *) {
|
if #available(OSX 10.11, iOS 9.0, *) {
|
||||||
withOverriddenNSLocaleCurrentLocale("en") { () -> () in
|
withOverriddenNSLocaleCurrentLocale("en") { () -> () in
|
||||||
|
|||||||
Reference in New Issue
Block a user