Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13968756
D9788.id23495.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
24 KB
Referenced Files
None
Subscribers
None
D9788.id23495.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
@@ -139,6 +139,7 @@
'PhutilConsoleTable' => 'console/PhutilConsoleTable.php',
'PhutilConsoleWrapTestCase' => 'console/__tests__/PhutilConsoleWrapTestCase.php',
'PhutilContextFreeGrammar' => 'grammar/PhutilContextFreeGrammar.php',
+ 'PhutilCsprintfTestCase' => 'xsprintf/__tests__/PhutilCsprintfTestCase.php',
'PhutilDaemon' => 'daemon/PhutilDaemon.php',
'PhutilDaemonOverseer' => 'daemon/PhutilDaemonOverseer.php',
'PhutilDefaultSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilDefaultSyntaxHighlighter.php',
@@ -176,6 +177,7 @@
'PhutilGitURITestCase' => 'parser/__tests__/PhutilGitURITestCase.php',
'PhutilHangForeverDaemon' => 'daemon/torture/PhutilHangForeverDaemon.php',
'PhutilHelpArgumentWorkflow' => 'parser/argument/workflow/PhutilHelpArgumentWorkflow.php',
+ 'PhutilHgsprintfTestCase' => 'xsprintf/__tests__/PhutilHgsprintfTestCase.php',
'PhutilInfrastructureTestCase' => '__tests__/PhutilInfrastructureTestCase.php',
'PhutilInteractiveEditor' => 'console/PhutilInteractiveEditor.php',
'PhutilInvisibleSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilInvisibleSyntaxHighlighter.php',
@@ -315,14 +317,12 @@
'PhutilURITestCase' => 'parser/__tests__/PhutilURITestCase.php',
'PhutilUTF8StringTruncator' => 'utils/PhutilUTF8StringTruncator.php',
'PhutilUTF8TestCase' => 'utils/__tests__/PhutilUTF8TestCase.php',
+ 'PhutilUrisprintfTestCase' => 'xsprintf/__tests__/PhutilUrisprintfTestCase.php',
'PhutilUtilsTestCase' => 'utils/__tests__/PhutilUtilsTestCase.php',
'PhutilWordPressFuture' => 'future/wordpress/PhutilWordPressFuture.php',
'PhutilXHPASTSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilXHPASTSyntaxHighlighter.php',
'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',
'TestAbstractDirectedGraph' => 'utils/__tests__/TestAbstractDirectedGraph.php',
@@ -332,6 +332,7 @@
'XHPASTToken' => 'parser/xhpast/api/XHPASTToken.php',
'XHPASTTree' => 'parser/xhpast/api/XHPASTTree.php',
'XHPASTTreeTestCase' => 'parser/xhpast/api/__tests__/XHPASTTreeTestCase.php',
+ 'XsprintfUnknownConversionException' => 'xsprintf/exception/XsprintfUnknownConversionException.php',
),
'function' =>
array(
@@ -560,6 +561,7 @@
'PhutilConsoleStdinNotInteractiveException' => 'Exception',
'PhutilConsoleTable' => 'Phobject',
'PhutilConsoleWrapTestCase' => 'PhutilTestCase',
+ 'PhutilCsprintfTestCase' => 'ArcanistTestCase',
'PhutilDefaultSyntaxHighlighterEngine' => 'PhutilSyntaxHighlighterEngine',
'PhutilDefaultSyntaxHighlighterEnginePygmentsFuture' => 'FutureProxy',
'PhutilDefaultSyntaxHighlighterEngineTestCase' => 'PhutilTestCase',
@@ -581,6 +583,7 @@
'PhutilGitURITestCase' => 'PhutilTestCase',
'PhutilHangForeverDaemon' => 'PhutilTortureTestDaemon',
'PhutilHelpArgumentWorkflow' => 'PhutilArgumentWorkflow',
+ 'PhutilHgsprintfTestCase' => 'ArcanistTestCase',
'PhutilInfrastructureTestCase' => 'PhutilTestCase',
'PhutilJSONParserException' => 'Exception',
'PhutilJSONParserTestCase' => 'PhutilTestCase',
@@ -684,13 +687,11 @@
'PhutilURITestCase' => 'PhutilTestCase',
'PhutilUTF8StringTruncator' => 'Phobject',
'PhutilUTF8TestCase' => 'PhutilTestCase',
+ 'PhutilUrisprintfTestCase' => 'ArcanistTestCase',
'PhutilUtilsTestCase' => 'PhutilTestCase',
'PhutilWordPressFuture' => 'FutureProxy',
'PhutilXHPASTSyntaxHighlighterFuture' => 'FutureProxy',
'PhutilXHPASTSyntaxHighlighterTestCase' => 'PhutilTestCase',
- 'PhutilcsprintfTestCase' => 'ArcanistTestCase',
- 'PhutilurisprintfTestCase' => 'ArcanistTestCase',
- 'PhutilxsprintfTestCase' => 'ArcanistTestCase',
'QueryFuture' => 'Future',
'TestAbstractDirectedGraph' => 'AbstractDirectedGraph',
'XHPASTNode' => 'AASTNode',
@@ -699,5 +700,6 @@
'XHPASTToken' => 'AASTToken',
'XHPASTTree' => 'AASTTree',
'XHPASTTreeTestCase' => 'PhutilTestCase',
+ 'XsprintfUnknownConversionException' => 'InvalidArgumentException',
),
));
diff --git a/src/xsprintf/PhutilCommandString.php b/src/xsprintf/PhutilCommandString.php
--- a/src/xsprintf/PhutilCommandString.php
+++ b/src/xsprintf/PhutilCommandString.php
@@ -17,11 +17,11 @@
}
public function getUnmaskedString() {
- return $this->renderString($unmasked = true);
+ return $this->renderString(true);
}
public function getMaskedString() {
- return $this->renderString($unmasked = false);
+ return $this->renderString(false);
}
private function renderString($unmasked) {
diff --git a/src/xsprintf/__tests__/PhutilcsprintfTestCase.php b/src/xsprintf/__tests__/PhutilCsprintfTestCase.php
rename from src/xsprintf/__tests__/PhutilcsprintfTestCase.php
rename to src/xsprintf/__tests__/PhutilCsprintfTestCase.php
--- a/src/xsprintf/__tests__/PhutilcsprintfTestCase.php
+++ b/src/xsprintf/__tests__/PhutilCsprintfTestCase.php
@@ -1,19 +1,18 @@
<?php
-final class PhutilcsprintfTestCase extends PhutilTestCase {
+final class PhutilCsprintfTestCase extends PhutilTestCase {
public function testCommandReadableEscapes() {
// For arguments comprised of only characters which are safe in any context,
// %R this should avoid adding quotes.
- $this->assertTrue(('ab' === (string)csprintf('%R', 'ab')));
+ $this->assertEqual('ab', (string)csprintf('%R', 'ab'));
// For arguments which have any characters which are not safe in some
// context, %R should apply standard escaping.
- $this->assertFalse(('a b' === (string)csprintf('%R', 'a b')));
+ $this->assertFalse('a b' === (string)csprintf('%R', 'a b'));
}
public function testPasswords() {
-
// Normal "%s" doesn't do anything special.
$command = csprintf('echo %s', 'hunter2trustno1');
$this->assertTrue(strpos($command, 'hunter2trustno1') !== false);
@@ -25,7 +24,7 @@
} catch (Exception $ex) {
$caught = $ex;
}
- $this->assertTrue($caught instanceof Exception);
+ $this->assertTrue($caught instanceof InvalidArgumentException);
// "%P" masks the provided value.
@@ -36,23 +35,25 @@
// Executing the command works as expected.
list($out) = execx('%C', $command);
$this->assertTrue(strpos($out, 'hunter2trustno1') !== false);
+ }
+ public function testEscapingIsRobust() {
+ if (phutil_is_windows()) {
+ $this->assertSkipped("This test doesn't work on Windows.");
+ }
// Escaping should be robust even when used to escape commands which take
// other commands.
- if (!phutil_is_windows()) {
- list($out) = execx(
+ list($out) = execx(
+ 'sh -c %s',
+ csprintf(
'sh -c %s',
csprintf(
'sh -c %s',
csprintf(
- 'sh -c %s',
- csprintf(
- 'echo %P',
- new PhutilOpaqueEnvelope('!@#$%^&*()')))));
- $this->assertTrue(strpos($out, '!@#$%^&*()') !== false);
- }
-
+ 'echo %P',
+ new PhutilOpaqueEnvelope('!@#$%^&*()')))));
+ $this->assertTrue(strpos($out, '!@#$%^&*()') !== false);
}
}
diff --git a/src/xsprintf/__tests__/PhutilxsprintfTestCase.php b/src/xsprintf/__tests__/PhutilHgsprintfTestCase.php
rename from src/xsprintf/__tests__/PhutilxsprintfTestCase.php
rename to src/xsprintf/__tests__/PhutilHgsprintfTestCase.php
--- a/src/xsprintf/__tests__/PhutilxsprintfTestCase.php
+++ b/src/xsprintf/__tests__/PhutilHgsprintfTestCase.php
@@ -1,8 +1,8 @@
<?php
-final class PhutilxsprintfTestCase extends PhutilTestCase {
+final class PhutilHgsprintfTestCase extends PhutilTestCase {
- public function testhgsprintf() {
+ public function testHgsprintf() {
$this->assertEqual(
"'version-1'",
hgsprintf('%s', 'version-1'));
diff --git a/src/xsprintf/__tests__/PhutilurisprintfTestCase.php b/src/xsprintf/__tests__/PhutilUrisprintfTestCase.php
rename from src/xsprintf/__tests__/PhutilurisprintfTestCase.php
rename to src/xsprintf/__tests__/PhutilUrisprintfTestCase.php
--- a/src/xsprintf/__tests__/PhutilurisprintfTestCase.php
+++ b/src/xsprintf/__tests__/PhutilUrisprintfTestCase.php
@@ -1,9 +1,8 @@
<?php
-final class PhutilurisprintfTestCase extends PhutilTestCase {
-
- public function testurisprintf() {
+final class PhutilUrisprintfTestCase extends PhutilTestCase {
+ public function testUrisprintf() {
$this->assertEqual(
'x.com?a=huh%3F',
urisprintf('x.com?a=%s', 'huh?'));
@@ -15,7 +14,6 @@
$this->assertEqual(
'y.com?%21&%23',
vurisprintf('y.com?%s&%s', array('!', '#')));
-
}
}
diff --git a/src/xsprintf/csprintf.php b/src/xsprintf/csprintf.php
--- a/src/xsprintf/csprintf.php
+++ b/src/xsprintf/csprintf.php
@@ -1,8 +1,8 @@
<?php
/**
- * Format a shell command string. This function behaves like sprintf(), except
- * that all the normal conversions (like %s) will be properly escaped, and
+ * Format a shell command string. This function behaves like `sprintf`, except
+ * that all the normal conversions (like "%s") will be properly escaped, and
* additional conversions are supported:
*
* %Ls
@@ -13,7 +13,7 @@
*
* %P
* Password (or other sensitive parameter) to escape. Pass a
- * PhutilOpaqueEnvelope.
+ * @{class:PhutilOpaqueEnvelope}.
*
* %C (Raw Command)
* Passes the argument through without escaping. Dangerous!
@@ -24,8 +24,8 @@
* in any context. The intent is to produce prettier human-readable
* commands.
*
- * Generally, you should invoke shell commands via execx() rather than by
- * calling csprintf() directly.
+ * Generally, you should invoke shell commands via @{function:execx} rather
+ * than by calling @{function:csprintf} directly.
*
* @param string sprintf()-style format string.
* @param ... Zero or more arguments.
@@ -38,7 +38,7 @@
}
/**
- * Version of csprintf() that takes a vector of arguments.
+ * Version of @{function:csprintf} that takes a vector of arguments.
*
* @param string sprintf()-style format string.
* @param list List of zero or more arguments to csprintf().
@@ -50,9 +50,8 @@
return call_user_func_array('csprintf', $argv);
}
-
/**
- * xsprintf() callback for csprintf().
+ * @{function:xsprintf} callback for @{function:csprintf}.
* @group exec
*/
function xsprintf_command($userdata, &$pattern, &$pos, &$value, &$length) {
@@ -78,7 +77,8 @@
// Check that the value is a non-empty array.
if (!is_array($value)) {
- throw new Exception("Expected an array for %L{$next} conversion.");
+ throw new InvalidArgumentException(
+ "Expected an array for %L{$next} conversion.");
}
switch ($next) {
@@ -99,7 +99,7 @@
break;
default:
- throw new Exception("Unknown conversion %L{$next}.");
+ throw new XsprintfUnknownConversionException("%L{$next}");
}
break;
@@ -115,7 +115,7 @@
break;
case 'P':
if (!($value instanceof PhutilOpaqueEnvelope)) {
- throw new Exception(
+ throw new InvalidArgumentException(
'Expected PhutilOpaqueEnvelope for %P conversion.');
}
if ($is_unmasked) {
diff --git a/src/xsprintf/exception/XsprintfUnknownConversionException.php b/src/xsprintf/exception/XsprintfUnknownConversionException.php
new file mode 100644
--- /dev/null
+++ b/src/xsprintf/exception/XsprintfUnknownConversionException.php
@@ -0,0 +1,10 @@
+<?php
+
+final class XsprintfUnknownConversionException
+ extends InvalidArgumentException {
+
+ public function __construct($conversion) {
+ parent::__construct("Unknown conversion {$conversion}.");
+ }
+
+}
diff --git a/src/xsprintf/hgsprintf.php b/src/xsprintf/hgsprintf.php
--- a/src/xsprintf/hgsprintf.php
+++ b/src/xsprintf/hgsprintf.php
@@ -16,14 +16,12 @@
return xsprintf('xsprintf_mercurial', null, $args);
}
-
/**
- * xsprintf() callback for Mercurial encoding.
+ * @{function:xsprintf} callback for Mercurial encoding.
*
* @group mercurial
*/
function xsprintf_mercurial($userdata, &$pattern, &$pos, &$value, &$length) {
-
$type = $pattern[$pos];
switch ($type) {
@@ -35,5 +33,5 @@
break;
}
- $pattern[$pos] = $type;
+ $pattern[$pos] = $type;
}
diff --git a/src/xsprintf/jsprintf.php b/src/xsprintf/jsprintf.php
--- a/src/xsprintf/jsprintf.php
+++ b/src/xsprintf/jsprintf.php
@@ -2,8 +2,8 @@
/**
* Format a Javascript string, using JSON to export complex values. This
- * function behaves like sprintf(), except that all the normal conversions
- * (like %s) will be properly escaped, and additional conversions are
+ * function behaves like `sprintf`, except that all the normal conversions
+ * (like "%s") will be properly escaped, and additional conversions are
* supported:
*
* %O (Object)
@@ -30,7 +30,6 @@
return xsprintf('xsprintf_javascript', null, $args);
}
-
/**
* @group markup
*/
@@ -39,13 +38,11 @@
return xsprintf('xsprintf_javascript', null, $argv);
}
-
/**
- * xsprintf() callback for javascript encoding.
+ * @{function:xsprintf} callback for JavaScript encoding.
* @group markup
*/
function xsprintf_javascript($userdata, &$pattern, &$pos, &$value, &$length) {
-
$type = $pattern[$pos];
switch ($type) {
@@ -84,7 +81,7 @@
case 'd':
if ($value > 0x1FFFFFFFFFFFFF) {
- throw new Exception(
+ throw new RangeException(
"You are passing an integer to jsprintf() which is so large it can ".
"not be represented without loss of precision by Javascript's ".
"native Number class. Use %# instead.");
@@ -92,5 +89,5 @@
break;
}
- $pattern[$pos] = $type;
+ $pattern[$pos] = $type;
}
diff --git a/src/xsprintf/ldapsprintf.php b/src/xsprintf/ldapsprintf.php
--- a/src/xsprintf/ldapsprintf.php
+++ b/src/xsprintf/ldapsprintf.php
@@ -1,8 +1,8 @@
<?php
/**
- * Format an LDAP string. This function behaves like sprintf(), except that all
- * the normal conversions (like %s) will be properly escaped, and additional
+ * Format an LDAP string. This function behaves like `sprintf`, except that all
+ * the normal conversions (like "%s") will be properly escaped, and additional
* conversions are supported:
*
* %S (Search Filter)
@@ -18,11 +18,10 @@
}
/**
- * ldap_sprintf() callback for LDAP encoding.
+ * @{function:ldap_sprintf} callback for LDAP encoding.
* @group markup
*/
function xsprintf_ldap($userdata, &$pattern, &$pos, &$value, &$length) {
-
$type = $pattern[$pos];
// https://www.owasp.org/index.php/Preventing_LDAP_Injection_in_Java
@@ -46,5 +45,5 @@
break;
}
- $pattern[$pos] = $type;
+ $pattern[$pos] = $type;
}
diff --git a/src/xsprintf/qsprintf.php b/src/xsprintf/qsprintf.php
--- a/src/xsprintf/qsprintf.php
+++ b/src/xsprintf/qsprintf.php
@@ -1,9 +1,9 @@
<?php
/**
- * Format an SQL query. This function behaves like sprintf(), except that
- * all the normal conversions (like %s) will be properly escaped, and
- * additional conversions are supported:
+ * Format an SQL query. This function behaves like `sprintf`, except that all
+ * the normal conversions (like "%s") will be properly escaped, and additional
+ * conversions are supported:
*
* %nd, %ns, %nf, %nB
* "Nullable" versions of %d, %s, %f and %B. Will produce 'NULL' if the
@@ -74,9 +74,10 @@
return xsprintf('xsprintf_query', $escaper, $argv);
}
-
/**
- * xsprintf() callback for encoding SQL queries. See qsprintf().
+ * @{function:xsprintf} callback for encoding SQL queries. See
+ * @{function:qsprintf}.
+ *
* @group storage
*/
function xsprintf_query($userdata, &$pattern, &$pos, &$value, &$length) {
@@ -90,7 +91,7 @@
$prefix = '';
if (!($escaper instanceof PhutilQsprintfInterface)) {
- throw new Exception('Invalid database escaper!');
+ throw new InvalidArgumentException('Invalid database escaper.');
}
switch ($type) {
@@ -101,7 +102,7 @@
case 's':
$pattern = substr_replace($pattern, '', $pos, 1);
$length = strlen($pattern);
- $type = 's';
+ $type = 's';
if ($value === null) {
$value = 'IS NULL';
$done = true;
@@ -127,7 +128,7 @@
$nullable = true;
break;
default:
- throw new Exception('Unknown conversion, try %nd or %ns.');
+ throw new XsprintfUnknownConversionException("%n{$next}");
}
break;
@@ -161,7 +162,7 @@
$value = implode(', ', $value);
break;
default:
- throw new Exception("Unknown conversion %L{$next}.");
+ throw new XsprintfUnknownConversionException("%L{$next}");
}
break;
}
@@ -233,8 +234,7 @@
break;
default:
- throw new Exception("Unknown conversion '%{$type}'.");
-
+ throw new XsprintfUnknownConversionException($type);
}
}
@@ -244,13 +244,15 @@
$pattern[$pos] = $type;
}
-
/**
* @group storage
*/
function _qsprintf_check_type($value, $type, $query) {
switch ($type) {
- case 'Ld': case 'Ls': case 'LC': case 'LB':
+ case 'Ld':
+ case 'Ls':
+ case 'LC':
+ case 'LB':
if (!is_array($value)) {
throw new AphrontQueryParameterException(
$query,
@@ -272,13 +274,15 @@
}
}
-
/**
* @group storage
*/
function _qsprintf_check_scalar_type($value, $type, $query) {
switch ($type) {
- case 'Q': case 'LC': case 'T': case 'C':
+ case 'Q':
+ case 'LC':
+ case 'T':
+ case 'C':
if (!is_string($value)) {
throw new AphrontQueryParameterException(
$query,
@@ -286,7 +290,9 @@
}
break;
- case 'Ld': case 'd': case 'f':
+ case 'Ld':
+ case 'd':
+ case 'f':
if (!is_null($value) && !is_numeric($value)) {
throw new AphrontQueryParameterException(
$query,
@@ -294,8 +300,14 @@
}
break;
- case 'Ls': case 's': case 'LB': case 'B':
- case '~': case '>': case '<': case 'K':
+ case 'Ls':
+ case 's':
+ case 'LB':
+ case 'B':
+ case '~':
+ case '>':
+ case '<':
+ case 'K':
if (!is_null($value) && !is_scalar($value)) {
throw new AphrontQueryParameterException(
$query,
@@ -304,6 +316,6 @@
break;
default:
- throw new Exception("Unknown conversion '{$type}'.");
+ throw new XsprintfUnknownConversionException($type);
}
}
diff --git a/src/xsprintf/queryfx.php b/src/xsprintf/queryfx.php
--- a/src/xsprintf/queryfx.php
+++ b/src/xsprintf/queryfx.php
@@ -33,8 +33,7 @@
$argv = func_get_args();
$ret = call_user_func_array('queryfx_all', $argv);
if (count($ret) > 1) {
- throw new AphrontQueryCountException(
- 'Query returned more than one row.');
+ throw new AphrontQueryCountException('Query returned more than one row.');
} else if (count($ret)) {
return reset($ret);
}
diff --git a/src/xsprintf/urisprintf.php b/src/xsprintf/urisprintf.php
--- a/src/xsprintf/urisprintf.php
+++ b/src/xsprintf/urisprintf.php
@@ -1,8 +1,8 @@
<?php
/**
- * Format a URI. This function behaves like sprintf(), except that all the
- * normal conversions (like %s) will be properly escaped, and additional
+ * 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)
@@ -25,11 +25,10 @@
}
/**
- * uri_sprintf() callback for URI encoding.
+ * @{function:urisprintf} callback for URI encoding.
* @group markup
*/
function xsprintf_uri($userdata, &$pattern, &$pos, &$value, &$length) {
-
$type = $pattern[$pos];
switch ($type) {
@@ -48,5 +47,5 @@
break;
}
- $pattern[$pos] = $type;
+ $pattern[$pos] = $type;
}
diff --git a/src/xsprintf/xsprintf.php b/src/xsprintf/xsprintf.php
--- a/src/xsprintf/xsprintf.php
+++ b/src/xsprintf/xsprintf.php
@@ -31,9 +31,8 @@
* @{function:qsprintf}, this is the database connection.
* @param list List of arguments, with the sprintf() pattern in position 0.
* @return string Formatted string.
- *
*/
-function xsprintf($callback, $userdata, $argv) {
+function xsprintf($callback, $userdata, array $argv) {
$argc = count($argv);
$arg = 0;
$pos = 0;
@@ -45,58 +44,56 @@
$c = $pattern[$pos];
if ($conv) {
- // We could make a greater effort to support formatting modifiers,
- // but they really have no place in semantic string formatting.
+ // We could make a greater effort to support formatting modifiers,
+ // but they really have no place in semantic string formatting.
if (strpos("'-0123456789.\$+", $c) !== false) {
- throw new Exception(
+ throw new InvalidArgumentException(
"xsprintf() does not support the `%{$c}' modifier.");
}
- if ($c != '%') {
+ if ($c !== '%') {
$conv = false;
$arg++;
if ($arg >= $argc) {
- throw new Exception('Too few arguments to xsprintf().');
+ throw new BadFunctionCallException(
+ 'Too few arguments to xsprintf().');
}
$callback($userdata, $pattern, $pos, $argv[$arg], $len);
}
}
- if ($c == '%') {
- // If we have "%%", this encodes a literal percentage symbol, so we are
- // no longer inside a conversion.
+ if ($c === '%') {
+ // If we have "%%", this encodes a literal percentage symbol, so we are
+ // no longer inside a conversion.
$conv = !$conv;
}
}
- if ($arg != ($argc - 1)) {
- throw new Exception('Too many arguments to xsprintf().');
+ if ($arg !== ($argc - 1)) {
+ throw new BadFunctionCallException('Too many arguments to xsprintf().');
}
$argv[0] = $pattern;
-
return call_user_func_array('sprintf', $argv);
}
-
/**
- * Example @{function:xsprintf} callback. When you call xsprintf(), you
- * must pass a callback like this one. xsprintf() will invoke the callback when
- * it encounters a conversion (like "%Z") in the pattern string.
+ * Example @{function:xsprintf} callback. When you call `xsprintf`, you must
+ * pass a callback like this one. `xsprintf` will invoke the callback when it
+ * encounters a conversion (like "%Z") in the pattern string.
*
- * Generally, this callback should examine ##$pattern[$pos]## (which will
- * contain the conversion character, like 'Z'), escape ##$value## appropriately,
- * and then replace ##$pattern[$pos]## with an 's' so sprintf() prints the
- * escaped value as a string. However, more sophisticated behaviors are possible
- * -- particularly, consuming multiple characters to allow for conversions like
- * "%Ld". In this case, the callback needs to substr_replace() the entire
- * conversion with 's' and then update ##$length##.
+ * Generally, this callback should examine `$pattern[$pos]` (which will contain
+ * the conversion character, like 'Z'), escape `$value` appropriately, and then
+ * replace `$pattern[$pos]` with an 's' so `sprintf` prints the escaped value
+ * as a string. However, more sophisticated behaviors are possible --
+ * particularly, consuming multiple characters to allow for conversions like
+ * "%Ld". In this case, the callback needs to `substr_replace` the entire
+ * conversion with 's' and then update `$length`.
*
* For example implementations, see @{function:xsprintf_command},
- * @{function:xsprintf_javascript},
- * and @{function:xsprintf_query}.
+ * @{function:xsprintf_javascript} and @{function:xsprintf_query}.
*
* @param wild Arbitrary, optional userdata. This is whatever userdata
* was passed to @{function:xsprintf}.
@@ -111,7 +108,7 @@
&$pos,
&$value,
&$length) {
- throw new Exception(
+ throw new RuntimeException(
'This function exists only to document the call signature for xsprintf() '.
'callbacks.');
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Oct 17 2024, 10:58 PM (4 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6723805
Default Alt Text
D9788.id23495.diff (24 KB)
Attached To
Mode
D9788: Various minor improvements to the `xsprintf` functions
Attached
Detach File
Event Timeline
Log In to Comment