[Foundation] Improve/fix NSRegularExpression and NSDataDetector bindings. Fixes #5881. (#5882)

* NSDataDetector

    * Add constructor found in header. No idea where the commented-out
      constructor came from, it's not in the header, so I removed it.
    * Add overloads that take NSTextCheckingType in addition to
      NSTextCheckingTypes. Apple's API take NSTextCheckingTypes, but all the
      documentation and samples say that you're supposed to pass one or more
      or'ed NSTextCheckingType values, so support that as well without casting
      between enums.

* NSRegularExpression

    * GetMatches had the wrong return type, so add a GetMatches2 that does it
      right. Also add a test to make sure it's really right.
    * Bind 'regularExpressionWithPattern:options:error:' with a static method.
      There's a corresponding constructor, but constructors returning out
      NSError parameters isn't the nicest API (when the NSError is important),
      so add the static method as well.
    * Add a missing [NullAllowed] on FindFirstMatch's return value.

* NSRegularExpressionOptions

    * Add missing enum value.

Fixes https://github.com/xamarin/xamarin-macios/issues/5881.
This commit is contained in:
Rolf Bjarne Kvinge 2019-04-10 15:12:24 +02:00 коммит произвёл GitHub
Родитель f2edbe12dc
Коммит 06426bd7a0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 75 добавлений и 5 удалений

Просмотреть файл

@ -1170,6 +1170,7 @@ namespace Foundation {
IgnoreMetacharacters = 1 << 2,
DotMatchesLineSeparators = 1 << 3,
AnchorsMatchLines = 1 << 4,
UseUnixLineSeparators = 1 << 5,
UseUnicodeWordBoundaries = 1 << 6
}

Просмотреть файл

@ -1475,13 +1475,20 @@ namespace Foundation
[BaseType (typeof (NSRegularExpression))]
interface NSDataDetector : NSCopying, NSCoding {
// Invalid parent ctor: -[NSDataDetector initWithPattern:options:error:]: Not valid for NSDataDetector
// [Export ("initWithPattern:options:error:")]
// IntPtr Constructor (NSString pattern, NSRegularExpressionOptions options, out NSError error);
[DesignatedInitializer]
[Export ("initWithTypes:error:")]
IntPtr Constructor (NSTextCheckingTypes options, out NSError error);
[Wrap ("this ((NSTextCheckingTypes) options, out error)")]
IntPtr Constructor (NSTextCheckingType options, out NSError error);
[Export ("dataDetectorWithTypes:error:"), Static]
NSDataDetector Create (NSTextCheckingTypes checkingTypes, out NSError error);
[Static]
[Wrap ("Create ((NSTextCheckingTypes) checkingTypes, out error)")]
NSDataDetector Create (NSTextCheckingType checkingTypes, out NSError error);
[Export ("checkingTypes")]
NSTextCheckingTypes CheckingTypes { get; }
}
@ -4566,6 +4573,10 @@ namespace Foundation
[Export ("initWithPattern:options:error:")]
IntPtr Constructor (NSString pattern, NSRegularExpressionOptions options, out NSError error);
[Static]
[Export ("regularExpressionWithPattern:options:error:")]
NSRegularExpression Create (NSString pattern, NSRegularExpressionOptions options, out NSError error);
[Export ("pattern")]
NSString Pattern { get; }
@ -4579,21 +4590,37 @@ namespace Foundation
[Static]
NSString GetEscapedPattern (NSString str);
/* From the NSMatching category */
[Export ("enumerateMatchesInString:options:range:usingBlock:")]
void EnumerateMatches (NSString str, NSMatchingOptions options, NSRange range, NSMatchEnumerator enumerator);
#if !XAMCORE_4_0
[Obsolete ("Use 'GetMatches2' instead, this method has the wrong return type.")]
[Export ("matchesInString:options:range:")]
NSString [] GetMatches (NSString str, NSMatchingOptions options, NSRange range);
#endif
[Export ("matchesInString:options:range:")]
#if XAMCORE_4_0
NSTextCheckingResult [] GetMatches (NSString str, NSMatchingOptions options, NSRange range);
#else
[Sealed]
NSTextCheckingResult [] GetMatches2 (NSString str, NSMatchingOptions options, NSRange range);
#endif
[Export ("numberOfMatchesInString:options:range:")]
nuint GetNumberOfMatches (NSString str, NSMatchingOptions options, NSRange range);
[Export ("firstMatchInString:options:range:")]
[return: NullAllowed]
NSTextCheckingResult FindFirstMatch (string str, NSMatchingOptions options, NSRange range);
[Export ("rangeOfFirstMatchInString:options:range:")]
NSRange GetRangeOfFirstMatch (string str, NSMatchingOptions options, NSRange range);
/* From the NSReplacement category */
[Export ("stringByReplacingMatchesInString:options:range:withTemplate:")]
string ReplaceMatches (string sourceString, NSMatchingOptions options, NSRange range, string template);

Просмотреть файл

@ -0,0 +1,44 @@
//
// Unit tests for NSRegularExpression
//
// Authors:
// Rolf Bjarne Kvinge <rolf@xamarin.com>
//
// Copyright 2019 Microsoft Corp. All rights reserved.
//
using System;
using System.IO;
#if XAMCORE_2_0
using Foundation;
using ObjCRuntime;
#else
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;
#endif
using NUnit.Framework;
namespace MonoTouchFixtures.Foundation {
[TestFixture]
[Preserve (AllMembers = true)]
public class RegularExpressionTest {
[Test]
public void GetMatches ()
{
var text = "some text https://microsoft.com text text";
var range = new NSRange (0, text.Length);
var detector = NSDataDetector.Create (NSTextCheckingType.Link, out NSError error);
#if XAMCORE_4_0
var matches = detector.GetMatches (new NSString (text), 0, range);
#else
var matches = detector.GetMatches2 (new NSString (text), 0, range);
#endif
Assert.AreEqual (10, matches [0].Range.Location, "Range.Location");
Assert.AreEqual (21, matches [0].Range.Length, "Range.Length");
Assert.AreEqual ("https://microsoft.com", matches [0].Url.AbsoluteString, "Url");
}
}
}

Просмотреть файл

@ -373,7 +373,6 @@
!missing-selector! +NSPredicate::predicateWithFormat:arguments: not bound
!missing-selector! +NSPropertyListSerialization::dataFromPropertyList:format:errorDescription: not bound
!missing-selector! +NSPropertyListSerialization::propertyListFromData:mutabilityOption:format:errorDescription: not bound
!missing-selector! +NSRegularExpression::regularExpressionWithPattern:options:error: not bound
!missing-selector! +NSSet::setWithObject: not bound
!missing-selector! +NSSet::setWithObjects: not bound
!missing-selector! +NSSet::setWithObjects:count: not bound
@ -497,7 +496,6 @@
!missing-selector! NSData::initWithContentsOfURL:options:error: not bound
!missing-selector! NSData::initWithData: not bound
!missing-selector! NSData::isEqualToData: not bound
!missing-selector! NSDataDetector::initWithTypes:error: not bound
!missing-selector! NSDate::description not bound
!missing-selector! NSDate::initWithTimeInterval:sinceDate: not bound
!missing-selector! NSDate::initWithTimeIntervalSince1970: not bound