dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.19k stars 1.57k forks source link

Add hasMatch, firstMatch, and stringMatch to Pattern, String #7080

Closed munificent closed 9 years ago

munificent commented 11 years ago

Both String and RegExp implement Pattern, which is awesome. You can generically treat a String as if it were a RegExp... except that Pattern doesn't have all of the methods that RegExp provides. Pattern mysteriously leaves off hasMatch, firstMatch, and stringMatch, which means String also lacks them.

lrhn commented 11 years ago

Removed Type-Defect label. Added Type-Enhancement label.

alan-knight commented 11 years ago

Ran into this today. It would be nice to be able to, say, find the .dart files in a list of arguments.    args.where((each) => '.dart'.hasMatch(each)) The alternatives are both more awkward to write and less efficient.

lrhn commented 10 years ago

The problem is that adding more methods to Pattern also means adding them to String, and the name generally don't look as fitting on a String as on a more specialized pattern.

The solution is usually to use the "reverse method" on String:   args.where((each) => each.contains(".dart")); // or even .endsWith(...)

Methods on Pattern/RegExp often exists to support methods on String, so there is usually a corresponding method on string that takes a Pattern which should not be awkward (if it is, do speak up!)


Added NotPlanned label.