Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15432017
D21561.id.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D21561.id.diff
View Options
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -849,6 +849,7 @@
'PhutilRawEnglishLocale' => 'internationalization/locales/PhutilRawEnglishLocale.php',
'PhutilReadableSerializer' => 'readableserializer/PhutilReadableSerializer.php',
'PhutilReadableSerializerTestCase' => 'readableserializer/__tests__/PhutilReadableSerializerTestCase.php',
+ 'PhutilRegexException' => 'exception/PhutilRegexException.php',
'PhutilRope' => 'utils/PhutilRope.php',
'PhutilRopeTestCase' => 'utils/__tests__/PhutilRopeTestCase.php',
'PhutilServiceProfiler' => 'serviceprofiler/PhutilServiceProfiler.php',
@@ -1009,6 +1010,9 @@
'phutil_partition' => 'utils/utils.php',
'phutil_passthru' => 'future/exec/execx.php',
'phutil_person' => 'internationalization/pht.php',
+ 'phutil_preg_match' => 'utils/utils.php',
+ 'phutil_preg_match_all' => 'utils/utils.php',
+ 'phutil_raise_preg_exception' => 'utils/utils.php',
'phutil_register_library' => 'init/lib/core.php',
'phutil_register_library_map' => 'init/lib/core.php',
'phutil_set_system_locale' => 'utils/utf8.php',
@@ -1925,6 +1929,7 @@
'PhutilRawEnglishLocale' => 'PhutilLocale',
'PhutilReadableSerializer' => 'Phobject',
'PhutilReadableSerializerTestCase' => 'PhutilTestCase',
+ 'PhutilRegexException' => 'Exception',
'PhutilRope' => 'Phobject',
'PhutilRopeTestCase' => 'PhutilTestCase',
'PhutilServiceProfiler' => 'Phobject',
diff --git a/src/exception/PhutilRegexException.php b/src/exception/PhutilRegexException.php
new file mode 100644
--- /dev/null
+++ b/src/exception/PhutilRegexException.php
@@ -0,0 +1,3 @@
+<?php
+
+final class PhutilRegexException extends Exception {}
diff --git a/src/utils/utils.php b/src/utils/utils.php
--- a/src/utils/utils.php
+++ b/src/utils/utils.php
@@ -2008,3 +2008,89 @@
return $partitions;
}
+
+function phutil_preg_match(
+ $pattern,
+ $subject,
+ $flags = 0,
+ $offset = 0) {
+
+ $matches = null;
+ $result = @preg_match($pattern, $subject, $matches, $flags, $offset);
+ if ($result === false || $result === null) {
+ phutil_raise_preg_exception(
+ 'preg_match',
+ array(
+ $pattern,
+ $subject,
+ $matches,
+ $flags,
+ $offset,
+ ));
+ }
+
+ return $matches;
+}
+
+function phutil_preg_match_all(
+ $pattern,
+ $subject,
+ $flags = 0,
+ $offset = 0) {
+
+ $matches = null;
+ $result = @preg_match_all($pattern, $subject, $matches, $flags, $offset);
+ if ($result === false || $result === null) {
+ phutil_raise_preg_exception(
+ 'preg_match_all',
+ array(
+ $pattern,
+ $subject,
+ $matches,
+ $flags,
+ $offset,
+ ));
+ }
+
+ return $matches;
+}
+
+function phutil_raise_preg_exception($function, array $argv) {
+ $trap = new PhutilErrorTrap();
+
+ // NOTE: This ugly construction to avoid issues with reference behavior when
+ // passing values through "call_user_func_array()".
+
+ switch ($function) {
+ case 'preg_match':
+ @preg_match($argv[0], $argv[1], $argv[2], $argv[3], $argv[4]);
+ break;
+ case 'preg_match_all':
+ @preg_match_all($argv[0], $argv[1], $argv[2], $argv[3], $argv[4]);
+ break;
+ }
+ $error_message = $trap->getErrorsAsString();
+
+ $trap->destroy();
+
+ $pattern = $argv[0];
+ $pattern_display = sprintf(
+ '"%s"',
+ addcslashes($pattern, '\\\"'));
+
+ $message = array();
+ $message[] = pht(
+ 'Call to %s(%s, ...) failed.',
+ $function,
+ $pattern_display);
+
+ if (strlen($error_message)) {
+ $message[] = pht(
+ 'Regular expression engine emitted message: %s',
+ $error_message);
+ }
+
+ $message = implode("\n\n", $message);
+
+ throw new PhutilRegexException($message);
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Mar 25, 3:51 PM (12 h, 39 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7227786
Default Alt Text
D21561.id.diff (3 KB)
Attached To
Mode
D21561: Provide some "preg_*" wrappers which raise exceptions on failure
Attached
Detach File
Event Timeline
Log In to Comment