1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117:
<?php
namespace Dropbox;
class Util
{
const SPECIAL_ESCAPE_IN = "\r\n\t\\\"";
const SPECIAL_ESCAPE_OUT = "rnt\\\"";
/**
* Return a double-quoted version of the given string, using PHP-escape sequences
* for all non-printable and non-ASCII characters.
*
* @param string $string
*
* @return string
*/
public static function q($string)
{
# HACK: "self::SPECIAL_ESCAPE_OUT[...]" is not valid syntax in PHP 5.3, so put
# it in a local variable first.
$special_escape_out = self::SPECIAL_ESCAPE_OUT;
$r = "\"";
$len = \strlen($string);
for ($i = 0; $i < $len; $i++) {
$c = $string[$i];
$escape_i = \strpos(self::SPECIAL_ESCAPE_IN, $c);
if ($escape_i !== false) {
// Characters with a special escape code.
$r .= "\\";
$r .= $special_escape_out[$escape_i];
}
else if ($c >= "\x20" and $c <= "\x7e") {
// Printable characters.
$r .= $c;
}
else {
// Generic hex escape code.
$r .= "\\x";
$r .= \bin2hex($c);
}
}
$r .= "\"";
return $r;
}
/**
* If the given string begins with the UTF-8 BOM (byte order mark), remove it and
* return whatever is left. Otherwise, return the original string untouched.
*
* Though it's not recommended for UTF-8 to have a BOM, the standard allows it to
* support software that isn't Unicode-aware.
*
* @param string $string
* A UTF-8 encoded string.
*
* @return string
*/
public static function stripUtf8Bom($string)
{
if (strlen($string) == 0) return $string;
if (\substr_compare($string, "\xEF\xBB\xBF", 0, 3) === 0) {
$string = \substr($string, 3);
}
return $string;
}
/**
* Return whether `$s` starts with `$prefix`.
*
* @param string $s
* @param string $prefix
* @param bool $caseInsensitive
*
* @return bool
*/
public static function startsWith($s, $prefix, $caseInsensitive = false)
{
// substr_compare errors if $main_str is zero-length, so handle that
// case specially here.
if (\strlen($s) == 0) {
return strlen($prefix) == 0;
}
return \substr_compare($s, $prefix, 0, strlen($prefix), $caseInsensitive) == 0;
}
/**
* If `$s` starts with `$prefix`, return `$s` with `$prefix` removed. Otherwise,
* return `null`.
*
* @param string $s
* @param string $prefix
* @param bool $caseInsensitive
*
* @return string|null
*/
public static function stripPrefix($s, $prefix, $caseInsensitive = false)
{
// substr_compare errors if $main_str is zero-length, so handle that
// case specially here.
if (strlen($s) == 0) {
if (strlen($prefix) == 0) {
return $s;
} else {
return null;
}
}
$prefix_length = strlen($prefix);
if (\substr_compare($s, $prefix, 0, strlen($prefix), $caseInsensitive) == 0) {
return substr($s, $prefix_length);
}
}
}