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

Mamatha Busi mamabusi at in.ibm.com
Tue Mar 8 06:15:58 CST 2016


 @Philippe Hausler, @Tony Parker: Thank you for the response. 

As per Tony's suggestions I have translated CF's kCFNotFound into an 
NSNotFound of Foundation in 'NSTextCheckingResult'  (the place where the 
ranges are being constructed). Below is the code snippet with the changes:

 _regularExpression = regularExpression
        super.init()
        let notFound = NSRange(location: NSNotFound,length: 0)
        for i in 0..<count {
            ranges[i].location == kCFNotFound ? _ranges.append(notFound) : 
_ranges.append(ranges[i])
        }

@Philippe: Request your comments on the above code changes.

TestFoundation has been run and I see no failures with the above changes.

The tests 'test_complexRegularExpressions' in the  TestNSRegularExpression 
were excluded till date. I am now running the excluded tests as well and 
there are a few test-cases here which deal with the ranges having 
NSNotFound. Along with these should there be more tests written to test 
the same? 

Regards
Mamatha



Mamatha Busi


Java L3 Support

IBM Software Group
 
India Software Labs
 



 



 

e-mail:
mamabusi at in.ibm.com
 

Java Technology India Group
 







From:   Philippe Hausler <phausler at apple.com>
To:     Tony Parker <anthony.parker at apple.com>
Cc:     Mamatha Busi/India/IBM at IBMIN, swift-corelibs-dev at swift.org
Date:   03/04/2016 12:02 AM
Subject:        Re: [swift-corelibs-dev] 
NSRegularExpression.firstMatchInString() fails  on Linux
Sent by:        phausler at apple.com




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> 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
 

Java Technology India Group
 





_______________________________________________
swift-corelibs-dev mailing list
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
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/20160308/d559c57b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 360 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20160308/d559c57b/attachment.gif>


More information about the swift-corelibs-dev mailing list