<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>I'm pretty sure the current implementation uses Unicode canonical equivalence to perform the comparison. This is equivalent to invoking `decomposedStringWithCanonicalMapping` on both strings and then comparing the resulting utf8 (or utf16) sequences, although it doesn't actually build new strings.<br></div>
<div>&nbsp;</div>
<div>This is not the same thing as comparing character sequences.<br></div>
<div>&nbsp;</div>
<div>For example, given the following two strings<br></div>
<div>&nbsp;</div>
<div>let s1 = "\u{1F1E6}\u{1F1F9}" // ðŸ‡¦ðŸ‡¹<br></div>
<div>let s2 = "\u{1F1E6}" // U+1F1E6 REGIONAL INDICATOR SYMBOL LETTER A<br></div>
<div>&nbsp;</div>
<div>The current behavior of `s1.hasPrefix(s2)` is to return `true`, but any kind of comparison on the character sequences is going to return `false` as both strings have one character each, and the characters are not the same. This is because String.characters is based on the notion of extended grapheme clusters, which is not the same thing as canonical equivalence.<br></div>
<div>&nbsp;</div>
<div>FWIW, the comparison operators on String (for non-ASCII strings) actually bridge into the ObjC runtime; if there is no ObjC runtime it appears to use ICU instead to do the comparison over UTF16 or UTF8 (see stdlib/public/stubs/UnicodeNormalization.cpp).<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
<div>&nbsp;</div>
<div>On Tue, Dec 22, 2015, at 01:28 PM, Alex Blewitt via swift-dev wrote:<br></div>
<blockquote type="cite"><div>I raised&nbsp;<a href="https://bugs.swift.org/browse/SR-337">https://bugs.swift.org/browse/SR-337</a>&nbsp;to implement hasPrefix and hasSuffix in Swift (instead of depending on the Objective-C implementation and only being available for OSX at present.<br></div>
<div>&nbsp;</div>
<div>I have an implementation which I’d like to create a pull request for but don’t know where it should go. The current implementation is at<br></div>
<div>&nbsp;</div>
<div><a href="https://github.com/apple/swift/blob/master/stdlib/public/core/StringLegacy.swift#L98">https://github.com/apple/swift/blob/master/stdlib/public/core/StringLegacy.swift#L98</a><br></div>
<div>&nbsp;</div>
<div>inside â€˜StringLegacy.swift’ but I’d presume it’s better to live elsewhere.<br></div>
<div>&nbsp;</div>
<div>In addition this uses the character view which I’m not sure follows the normalisation required for comparisons; or if there is some different normalisation routines that need to be written either.<br></div>
<div>&nbsp;</div>
<div>Finally it’s not clear if this the right way to solve the problem, or if the implementation should be in the open-source version of the Foundation NSString implementation.<br></div>
<div>&nbsp;</div>
<div>Happy to take comments by mail or on the bug.<br></div>
<div>&nbsp;</div>
<div>Alex<br></div>
<div><img style="height:1px !important;width:1px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;margin-top:0px !important;margin-bottom:0px !important;margin-right:0px !important;margin-left:0px !important;padding-top:0px !important;padding-bottom:0px !important;padding-right:0px !important;padding-left:0px !important;" border="0" height="1" width="1" alt="" src="https://www.fastmailusercontent.com/proxy/267159bab4a29a28382325f8dad2d309e3018e5547bfc3e6a34ef0592dee5a91/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d3148765176786c673171614a7d2236454230345272776e45607143505645513b647576455470553241317834715f653266783878746b61753567426156474a494a4846515164764a6c4d633345343f64723f49787a6272346a5f647b453d22364b4444623a7d636566783036323f6c417343377a417a54407942655439724a503052597d4b665779663b4435345b68415a4444355f664e6d2232495d6669354f4d22364756585959683075403d22324d2236466e4a7034317c4e68444771574e4e61726d464665475e665879734471433868375e4540764b6475544d223648396d6036614d23344d23344/open"><br></div>
<div><u>_______________________________________________</u><br></div>
<div>swift-dev mailing list<br></div>
<div><a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-dev">https://lists.swift.org/mailman/listinfo/swift-dev</a><br></div>
</blockquote><div>&nbsp;</div>
</body>
</html>