Skip to content

samkrishna/RegexKitX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RegexKitX

Use the pattern-matching Force.

Inspiration

I've loved regular expressions since the beginning of my programming career back when I worked at Apple. I learned (and continue to learn) A LOT from Jeffrey Friedl's book, Mastering Regular Expression, 3rd. ed. I know they are controversial in some circles but when crafted well, there's really nothing like them.

Here's some follow-up reading about regexes:

Objective-C

This is a Regular Expression API inspired by the RegexKitLite 4.0 API that John Engelhart (@johnezang) shipped back in 2010. ("RKL4")

Basically, I'm modernizing the API as a cover for NSRegularExpression and NSTextCheckingResult.

I am doing this for several reasons:

  • How unwieldy NSRegularExpression naturally is
  • The false positives that the Clang Static Analyzer flagged in RKL4
  • The fact that RKL4 is NOT ARC-compliant
  • The fact that the low-level RKL4 C code accessing the ICU regex engine spits out OSSpinLock-based deprecation warnings on macOS 10.12 Sierra and above
  • How easy it is to forget to ALWAYS link to the ICU library on every project using RKL4 (Obviously CocoaPods solves this)
  • I wanted to provide cover API for named captures and backreferences, which NSRegularExpression supports, and RKL4 does not.

My concern is that no amount of work-arounds or modifications to all the low-level RKL4 magic code will save it from being unbuildable or free of static analyzer warnings in a future appleOS. So rather than wait for that to happen or repeatedly deal directly with the awkwardness of NSRegularExpression, I'm choosing to do RegexKitX ("RKX").

I've also added documentation that is option-clickable for all the RKX category methods.

NOTE: There's an almost-pure RKL4 API re-implementation here. This version is intended to be a drop-in replacement (with possible block-based modifications) for the original RKL4 codebase.

A few caveats:

  1. I've re-ordered and modernized some of the argument and block parameters for a number of APIs.
  2. I've renamed a few APIs as well to make them grammatically and contextually consistent and follow Apple naming conventions from NSString.
  3. The regex syntax is 100%-pure ICU syntax (the original RKL4 diverged slightly away from ICU).
  4. For some of the block methods, I'm exposing NSEnumerationOptions to provide an option for directional control of the enumeration. As usual, NSEnumerationConcurrent behavior is undefined.

Swift

I have deprecated the SwiftRegexKitX extension and tests. The new Apple Swift Regex additions pretty much negate any prior extensions. Apple has added a Regex struct and associated types along with a RegexBuilder DSL, which is much more sophisticated than anything I came up with for SwiftRegexKitX.