Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F99289455
jsprintf.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jan 23, 03:14
Size
2 KB
Mime Type
text/x-php
Expires
Sat, Jan 25, 03:14 (2 d)
Engine
blob
Format
Raw Data
Handle
23768311
Attached To
rPHU libphutil
jsprintf.php
View Options
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 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
* supported:
*
* %O (Object)
* Converts the argument to a JSON object.
*
* %A (Array)
* Converts the argument to a JSON array.
*
* %b (Boolean)
* Converts a value to either `true' or `false'.
*
* %# (FBID)
* JavaScript has issues with 64-bit numbers so we use this as our
* transport mechanism from PHP to JavaScript
*
* %J (Javascript)
* Inserts raw, unescaped Javascript. This is DANGEROUS because it WILL
* NOT BE ESCAPED.
*/
function
jsprintf
(
$pattern
/*, ... */
)
{
$args
=
func_get_args
();
return
xsprintf
(
'xsprintf_javascript'
,
null
,
$args
);
}
function
vjsprintf
(
$pattern
,
array
$argv
)
{
array_unshift
(
$argv
,
$pattern
);
return
xsprintf
(
'xsprintf_javascript'
,
null
,
$argv
);
}
/**
* xsprintf() callback for javascript encoding.
*/
function
xsprintf_javascript
(
$userdata
,
&
$pattern
,
&
$pos
,
&
$value
,
&
$length
)
{
$type
=
$pattern
[
$pos
];
switch
(
$type
)
{
case
'#'
:
case
's'
:
$value
=
json_encode
((
string
)
$value
);
$type
=
's'
;
break
;
case
'A'
:
if
(!
is_array
(
$value
))
{
$value
=
array
();
}
$value
=
json_encode
(
array_values
(
$value
));
$type
=
's'
;
break
;
case
'O'
:
if
(
$value
!==
null
)
{
$value
=
(
object
)
$value
;
}
$value
=
json_encode
(
$value
);
$type
=
's'
;
break
;
case
'J'
:
$type
=
's'
;
break
;
case
'b'
:
$value
=
$value
?
'true'
:
'false'
;
$type
=
's'
;
break
;
case
'd'
:
if
(
$value
>
0x1FFFFFFFFFFFFF
)
{
throw
new
Exception
(
"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."
);
}
break
;
}
$pattern
[
$pos
]
=
$type
;
}
Event Timeline
Log In to Comment