Page MenuHomePhabricator

D12310.id29575.diff
No OneTemporary

D12310.id29575.diff

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
@@ -255,6 +255,7 @@
'PhutilPhabricatorAuthAdapter' => 'auth/PhutilPhabricatorAuthAdapter.php',
'PhutilPhobjectTestCase' => 'object/__tests__/PhutilPhobjectTestCase.php',
'PhutilPhtTestCase' => 'internationalization/__tests__/PhutilPhtTestCase.php',
+ 'PhutilPregsprintfTestCase' => 'xsprintf/__tests__/PhutilPregsprintfTestCase.php',
'PhutilProcessGroupDaemon' => 'daemon/torture/PhutilProcessGroupDaemon.php',
'PhutilProtocolChannel' => 'channel/PhutilProtocolChannel.php',
'PhutilProxyException' => 'error/PhutilProxyException.php',
@@ -459,6 +460,7 @@
'phutil_utf8v_combined' => 'utils/utf8.php',
'phutil_var_export' => 'utils/utils.php',
'ppull' => 'utils/utils.php',
+ 'pregsprintf' => 'xsprintf/pregsprintf.php',
'qsprintf' => 'xsprintf/qsprintf.php',
'queryfx' => 'xsprintf/queryfx.php',
'queryfx_all' => 'xsprintf/queryfx.php',
@@ -479,12 +481,13 @@
'xsprintf_ldap' => 'xsprintf/ldapsprintf.php',
'xsprintf_mercurial' => 'xsprintf/hgsprintf.php',
'xsprintf_query' => 'xsprintf/qsprintf.php',
+ 'xsprintf_regex' => 'xsprintf/pregsprintf.php',
'xsprintf_uri' => 'xsprintf/urisprintf.php',
),
'xmap' => array(
'AASTNodeList' => array(
- 'Iterator',
'Countable',
+ 'Iterator',
),
'AbstractDirectedGraphTestCase' => 'PhutilTestCase',
'AphrontAccessDeniedQueryException' => 'AphrontRecoverableQueryException',
@@ -673,6 +676,7 @@
'PhutilPhabricatorAuthAdapter' => 'PhutilOAuthAuthAdapter',
'PhutilPhobjectTestCase' => 'PhutilTestCase',
'PhutilPhtTestCase' => 'PhutilTestCase',
+ 'PhutilPregsprintfTestCase' => 'PhutilTestCase',
'PhutilProcessGroupDaemon' => 'PhutilTortureTestDaemon',
'PhutilProtocolChannel' => 'PhutilChannelChannel',
'PhutilProxyException' => 'Exception',
diff --git a/src/xsprintf/__tests__/PhutilPregsprintfTestCase.php b/src/xsprintf/__tests__/PhutilPregsprintfTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/xsprintf/__tests__/PhutilPregsprintfTestCase.php
@@ -0,0 +1,19 @@
+<?php
+
+final class PhutilPregsprintfTestCase extends PhutilTestCase {
+
+ public function testHgsprintf() {
+ $this->assertEqual(
+ '@foobar@',
+ pregsprintf('%s', 'foobar'));
+
+ $this->assertEqual(
+ '@\.\*\[a\-z\]@',
+ pregsprintf('%s', '.*[a-z]'));
+
+ $this->assertEqual(
+ '@.*\@[a-z]@',
+ pregsprintf('%R', '.*@[a-z]'));
+ }
+
+}
diff --git a/src/xsprintf/pregsprintf.php b/src/xsprintf/pregsprintf.php
new file mode 100644
--- /dev/null
+++ b/src/xsprintf/pregsprintf.php
@@ -0,0 +1,29 @@
+<?php
+
+function pregsprintf($pattern /* , ... */) {
+ $args = func_get_args();
+ $delim = '@';
+ $userdata = array('delimiter' => $delim);
+
+ return $delim.xsprintf('xsprintf_regex', $userdata, $args).$delim;
+}
+
+/**
+ * @{function:xsprintf} callback for regular expressions.
+ */
+function xsprintf_regex($userdata, &$pattern, &$pos, &$value, &$length) {
+ $delim = idx($userdata, 'delimiter');
+ $type = $pattern[$pos];
+
+ switch ($type) {
+ case 's':
+ $value = preg_quote($value, $delim);
+ break;
+ case 'R':
+ $value = str_replace($delim, '\\'.$delim, $value);
+ $type = 's';
+ break;
+ }
+
+ $pattern[$pos] = $type;
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Mar 25, 4:01 PM (1 w, 3 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7705824
Default Alt Text
D12310.id29575.diff (3 KB)

Event Timeline