Page MenuHomePhabricator

D7538.id17014.diff
No OneTemporary

D7538.id17014.diff

Index: src/__phutil_library_map__.php
===================================================================
--- src/__phutil_library_map__.php
+++ src/__phutil_library_map__.php
@@ -299,6 +299,7 @@
'PhutilXHPASTSyntaxHighlighterFuture' => 'markup/syntax/highlighter/xhpast/PhutilXHPASTSyntaxHighlighterFuture.php',
'PhutilXHPASTSyntaxHighlighterTestCase' => 'markup/syntax/highlighter/__tests__/PhutilXHPASTSyntaxHighlighterTestCase.php',
'PhutilcsprintfTestCase' => 'xsprintf/__tests__/PhutilcsprintfTestCase.php',
+ 'PhutilurisprintfTestCase' => 'xsprintf/__tests__/PhutilurisprintfTestCase.php',
'PhutilxsprintfTestCase' => 'xsprintf/__tests__/PhutilxsprintfTestCase.php',
'QueryFuture' => 'future/query/QueryFuture.php',
'TempFile' => 'filesystem/TempFile.php',
@@ -393,6 +394,7 @@
'queryfx' => 'xsprintf/queryfx.php',
'queryfx_all' => 'xsprintf/queryfx.php',
'queryfx_one' => 'xsprintf/queryfx.php',
+ 'urisprintf' => 'xsprintf/urisprintf.php',
'vcsprintf' => 'xsprintf/csprintf.php',
'vjsprintf' => 'xsprintf/jsprintf.php',
'vqsprintf' => 'xsprintf/qsprintf.php',
@@ -411,6 +413,7 @@
'xsprintf_ldap' => 'xsprintf/ldapsprintf.php',
'xsprintf_mercurial' => 'xsprintf/hgsprintf.php',
'xsprintf_query' => 'xsprintf/qsprintf.php',
+ 'xsprintf_uri' => 'xsprintf/urisprintf.php',
),
'xmap' =>
array(
@@ -632,6 +635,7 @@
'PhutilXHPASTSyntaxHighlighterFuture' => 'FutureProxy',
'PhutilXHPASTSyntaxHighlighterTestCase' => 'PhutilTestCase',
'PhutilcsprintfTestCase' => 'ArcanistTestCase',
+ 'PhutilurisprintfTestCase' => 'ArcanistTestCase',
'PhutilxsprintfTestCase' => 'ArcanistTestCase',
'QueryFuture' => 'Future',
'TestAbstractDirectedGraph' => 'AbstractDirectedGraph',
Index: src/xsprintf/__tests__/PhutilurisprintfTestCase.php
===================================================================
--- /dev/null
+++ src/xsprintf/__tests__/PhutilurisprintfTestCase.php
@@ -0,0 +1,21 @@
+<?php
+
+final class PhutilurisprintfTestCase extends ArcanistTestCase {
+
+ public function testurisprintf() {
+
+ $this->assertEqual(
+ 'x.com?a=huh%3F',
+ urisprintf('x.com?a=%s', 'huh?'));
+
+ $this->assertEqual(
+ '/a/origin%252Fmaster/z/',
+ urisprintf('/a/%p/z/', 'origin/master'));
+
+ $this->assertEqual(
+ 'y.com?%21&%23',
+ vurisprintf('y.com?%s&%s', array('!', '#')));
+
+ }
+
+}
Index: src/xsprintf/urisprintf.php
===================================================================
--- /dev/null
+++ src/xsprintf/urisprintf.php
@@ -0,0 +1,52 @@
+<?php
+
+/**
+ * Format a URI. This function behaves like sprintf(), except that all the
+ * normal conversions (like %s) will be properly escaped, and additional
+ * conversions are supported:
+ *
+ * %s (String)
+ * Escapes text for use in a URI.
+ *
+ * %p (Path Component)
+ * Escapes text for use in a URI path component.
+ *
+ * %R (Raw String)
+ * Inserts raw, unescaped text. DANGEROUS!
+ */
+function urisprintf($pattern /* , ... */) {
+ $args = func_get_args();
+ return xsprintf('xsprintf_uri', null, $args);
+}
+
+function vurisprintf($pattern, array $argv) {
+ array_unshift($argv, $pattern);
+ return call_user_func_array('urisprintf', $argv);
+}
+
+/**
+ * uri_sprintf() callback for URI encoding.
+ * @group markup
+ */
+function xsprintf_uri($userdata, &$pattern, &$pos, &$value, &$length) {
+
+ $type = $pattern[$pos];
+
+ switch ($type) {
+ case 's':
+ $value = phutil_escape_uri($value);
+ $type = 's';
+ break;
+
+ case 'p':
+ $value = phutil_escape_uri_path_component($value);
+ $type = 's';
+ break;
+
+ case 'R':
+ $type = 's';
+ break;
+ }
+
+ $pattern[$pos] = $type;
+}

File Metadata

Mime Type
text/plain
Expires
May 17 2024, 12:39 PM (4 w, 3 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6285300
Default Alt Text
D7538.id17014.diff (3 KB)

Event Timeline