mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-30 19:00:10 +02:00
Remove trailing whitespace.
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
This commit is contained in:
@@ -6,11 +6,11 @@
|
||||
*/
|
||||
class HTMLPurifier_UnitConverter
|
||||
{
|
||||
|
||||
|
||||
const ENGLISH = 1;
|
||||
const METRIC = 2;
|
||||
const DIGITAL = 3;
|
||||
|
||||
|
||||
/**
|
||||
* Units information array. Units are grouped into measuring systems
|
||||
* (English, Metric), and are assigned an integer representing
|
||||
@@ -34,28 +34,28 @@ class HTMLPurifier_UnitConverter
|
||||
self::ENGLISH => array('mm', '2.83464567', 'pt'),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Minimum bcmath precision for output.
|
||||
*/
|
||||
protected $outputPrecision;
|
||||
|
||||
|
||||
/**
|
||||
* Bcmath precision for internal calculations.
|
||||
*/
|
||||
protected $internalPrecision;
|
||||
|
||||
|
||||
/**
|
||||
* Whether or not BCMath is available
|
||||
*/
|
||||
private $bcmath;
|
||||
|
||||
|
||||
public function __construct($output_precision = 4, $internal_precision = 10, $force_no_bcmath = false) {
|
||||
$this->outputPrecision = $output_precision;
|
||||
$this->internalPrecision = $internal_precision;
|
||||
$this->bcmath = !$force_no_bcmath && function_exists('bcmul');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a length object of one unit into another unit.
|
||||
* @param HTMLPurifier_Length $length
|
||||
@@ -75,37 +75,37 @@ class HTMLPurifier_UnitConverter
|
||||
* decimals will be added on.
|
||||
*/
|
||||
public function convert($length, $to_unit) {
|
||||
|
||||
|
||||
if (!$length->isValid()) return false;
|
||||
|
||||
|
||||
$n = $length->getN();
|
||||
$unit = $length->getUnit();
|
||||
|
||||
|
||||
if ($n === '0' || $unit === false) {
|
||||
return new HTMLPurifier_Length('0', false);
|
||||
}
|
||||
|
||||
|
||||
$state = $dest_state = false;
|
||||
foreach (self::$units as $k => $x) {
|
||||
if (isset($x[$unit])) $state = $k;
|
||||
if (isset($x[$to_unit])) $dest_state = $k;
|
||||
}
|
||||
if (!$state || !$dest_state) return false;
|
||||
|
||||
|
||||
// Some calculations about the initial precision of the number;
|
||||
// this will be useful when we need to do final rounding.
|
||||
$sigfigs = $this->getSigFigs($n);
|
||||
if ($sigfigs < $this->outputPrecision) $sigfigs = $this->outputPrecision;
|
||||
|
||||
|
||||
// BCMath's internal precision deals only with decimals. Use
|
||||
// our default if the initial number has no decimals, or increase
|
||||
// it by how ever many decimals, thus, the number of guard digits
|
||||
// will always be greater than or equal to internalPrecision.
|
||||
$log = (int) floor(log(abs($n), 10));
|
||||
$cp = ($log < 0) ? $this->internalPrecision - $log : $this->internalPrecision; // internal precision
|
||||
|
||||
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
|
||||
|
||||
// Determine what unit IN THIS SYSTEM we need to convert to
|
||||
if ($dest_state === $state) {
|
||||
// Simple conversion
|
||||
@@ -114,57 +114,57 @@ class HTMLPurifier_UnitConverter
|
||||
// Convert to the smallest unit, pending a system shift
|
||||
$dest_unit = self::$units[$state][$dest_state][0];
|
||||
}
|
||||
|
||||
|
||||
// Do the conversion if necessary
|
||||
if ($dest_unit !== $unit) {
|
||||
$factor = $this->div(self::$units[$state][$unit], self::$units[$state][$dest_unit], $cp);
|
||||
$n = $this->mul($n, $factor, $cp);
|
||||
$unit = $dest_unit;
|
||||
}
|
||||
|
||||
|
||||
// Output was zero, so bail out early. Shouldn't ever happen.
|
||||
if ($n === '') {
|
||||
$n = '0';
|
||||
$unit = $to_unit;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// It was a simple conversion, so bail out
|
||||
if ($dest_state === $state) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if ($i !== 0) {
|
||||
// Conversion failed! Apparently, the system we forwarded
|
||||
// to didn't have this unit. This should never happen!
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Pre-condition: $i == 0
|
||||
|
||||
|
||||
// Perform conversion to next system of units
|
||||
$n = $this->mul($n, self::$units[$state][$dest_state][1], $cp);
|
||||
$unit = self::$units[$state][$dest_state][2];
|
||||
$state = $dest_state;
|
||||
|
||||
|
||||
// One more loop around to convert the unit in the new system.
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Post-condition: $unit == $to_unit
|
||||
if ($unit !== $to_unit) return false;
|
||||
|
||||
|
||||
// Useful for debugging:
|
||||
//echo "<pre>n";
|
||||
//echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n";
|
||||
|
||||
|
||||
$n = $this->round($n, $sigfigs);
|
||||
if (strpos($n, '.') !== false) $n = rtrim($n, '0');
|
||||
$n = rtrim($n, '.');
|
||||
|
||||
|
||||
return new HTMLPurifier_Length($n, $unit);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of significant figures in a string number.
|
||||
* @param string $n Decimal number
|
||||
@@ -181,7 +181,7 @@ class HTMLPurifier_UnitConverter
|
||||
}
|
||||
return $sigfigs;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds two numbers, using arbitrary precision when available.
|
||||
*/
|
||||
@@ -189,7 +189,7 @@ class HTMLPurifier_UnitConverter
|
||||
if ($this->bcmath) return bcadd($s1, $s2, $scale);
|
||||
else return $this->scale($s1 + $s2, $scale);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Multiples two numbers, using arbitrary precision when available.
|
||||
*/
|
||||
@@ -197,7 +197,7 @@ class HTMLPurifier_UnitConverter
|
||||
if ($this->bcmath) return bcmul($s1, $s2, $scale);
|
||||
else return $this->scale($s1 * $s2, $scale);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Divides two numbers, using arbitrary precision when available.
|
||||
*/
|
||||
@@ -205,7 +205,7 @@ class HTMLPurifier_UnitConverter
|
||||
if ($this->bcmath) return bcdiv($s1, $s2, $scale);
|
||||
else return $this->scale($s1 / $s2, $scale);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rounds a number according to the number of sigfigs it should have,
|
||||
* using arbitrary precision when available.
|
||||
@@ -229,7 +229,7 @@ class HTMLPurifier_UnitConverter
|
||||
return $this->scale(round($n, $sigfigs - $new_log - 1), $rp + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scales a float to $scale digits right of decimal point, like BCMath.
|
||||
*/
|
||||
@@ -248,5 +248,5 @@ class HTMLPurifier_UnitConverter
|
||||
}
|
||||
return sprintf('%.' . $scale . 'f', (float) $r);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user