[swift-corelibs-dev] NSRegularExpression.firstMatchInString() fails on Linux

Philippe Hausler phausler at apple.com
Thu Mar 3 12:32:48 CST 2016


> On Mar 3, 2016, at 10:22 AM, Tony Parker via swift-corelibs-dev <swift-corelibs-dev at swift.org> wrote:
> 
> Hi Mamatha,
> 
>> On Mar 2, 2016, at 3:57 AM, Mamatha Busi via swift-corelibs-dev <swift-corelibs-dev at swift.org <mailto:swift-corelibs-dev at swift.org>> wrote:
>> 
>> Hello
>> 
>> The following test case, that currently fails on Linux,  is extracted from some of the excluded tests inside TestFoundation/TestNSRegularExpression. 
>> 
>> import Foundation
>> 
>> let searchStr = "123"
>> let testRegex = try NSRegularExpression.init(pattern: "a(b|c|d)(x|y|z)*|123", options: [])
>> let firstMatch = testRegex.firstMatchInString(searchStr, options: [], range: NSMakeRange(0,3))
>> 
>> if NSEqualRanges((firstMatch!.rangeAtIndex(1), NSMakeRange(NSNotFound,0)) {
>>     print("Test passed")
>> } else {
>>     print("First match first capture range = \(NSStringFromRange(firstMatch!.rangeAtIndex(1)))")
>>     print("Test failed")
>> }
>> 
>> 
>> 
>> In the above scenario, no capture group participates in the match. So, firstMatch.rangeAtIndex(1) should ideally return range {NSNotFound, 0} but the actual returned value is range {-1, 0}. Hence the failure.
>> 
>> Debugging on the CoreFoundation, it is observed that the value -1 is coming from 'kCFNotFound' which is defined as -1. On the Foundation side, 'NSNotFound' is defined with the value of ‘Int.max’ (which must be Int32.max and Int64.max on 32 and 64 bit platforms respectively).
>> 
>> There are a couple of queries that I have:
>> 
>> -> Is 'NSNotFound' in Foundation mapped  to 'kCFNotFound'  in CoreFoundation?
>> 
>> -> If so, then why do 'NSNotFound' and 'kCFNotFound' have different values on the Foundation and CoreFoundation respectively?
>> 
> 
> They don’t have the same value; we translate it manually in most places.
> 
>> Also, I tested by modifying the 'kCFNotFound' value in CoreFoundation/Base.subproj/CFBase.h  (See below code snippet):
>> 
>> #if TARGET_RT_64_BIT
>> static const CFIndex kCFNotFound = LONG_MAX;
>> #else
>> static const CFIndex kCFNotFound = INT_MAX;
>> #endif
>> 
>> 
>> With this change the above mentioned test-case passes but a lot of other TestFoundation tests seem to fail.
>> 
>> Please provide your comments on the appropriate change that is needed here. 
>> 
>> Thank you.
> 
> I think we probably just need to check the result from the CF function and translate it into an NSNotFound in this one particular case.

I think the problem in this case is that we fetch a buffer of CFRanges and pass that to the result creation.

ranges: UnsafeMutablePointer<CFRange>
...
let result = NSTextCheckingResult.regularExpressionCheckingResultWithRanges(NSRangePointer(ranges), count: count, regularExpression: matcher.regex)

> 
> Thanks,
> - Tony
> 
>> 
>> Regards
>> Mamatha
>> Mamatha Busi		
>> <Mail Attachment.gif>
>> Java L3 Support	
>> IBM Software Group	 
>> India Software Labs	 	
>>  	
>>  	
>> e-mail:	mamabusi at in.ibm.com <mailto:mamabusi at in.ibm.com>	 	
>> Java Technology India Group <http://bit.ly/a7KpQj>	 	
>> 
>> 
>> 
>> _______________________________________________
>> swift-corelibs-dev mailing list
>> swift-corelibs-dev at swift.org <mailto:swift-corelibs-dev at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev
> 
> _______________________________________________
> swift-corelibs-dev mailing list
> swift-corelibs-dev at swift.org <mailto:swift-corelibs-dev at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev <https://lists.swift.org/mailman/listinfo/swift-corelibs-dev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20160303/a97d3bcb/attachment.html>


More information about the swift-corelibs-dev mailing list