[Pkg-owncloud-commits] [owncloud] 129/199: Add helper method for turning int|float into base-10 unsigned integer string.

David Prévot taffit at moszumanska.debian.org
Sun Jun 1 18:53:17 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 2c36a4b07a088bca4f20c2f6386765dfc8ad07b7
Author: Andreas Fischer <bantu at owncloud.com>
Date:   Sat Feb 15 23:21:23 2014 +0100

    Add helper method for turning int|float into base-10 unsigned integer string.
---
 lib/private/largefilehelper.php | 24 +++++++++++++++++++++
 tests/lib/largefilehelper.php   | 46 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/lib/private/largefilehelper.php b/lib/private/largefilehelper.php
index 5f5e14a..66626b4 100644
--- a/lib/private/largefilehelper.php
+++ b/lib/private/largefilehelper.php
@@ -13,6 +13,30 @@ namespace OC;
  */
 class LargeFileHelper {
 	/**
+	* @brief Formats a signed integer or float as an unsigned integer base-10
+	*        string. Passed strings will be checked for being base-10.
+	*
+	* @param int|float|string $number Number containing unsigned integer data
+	*
+	* @return string Unsigned integer base-10 string
+	*/
+	public function formatUnsignedInteger($number) {
+		if (is_float($number)) {
+			// Undo the effect of the php.ini setting 'precision'.
+			return number_format($number, 0, '', '');
+		} else if (is_string($number) && ctype_digit($number)) {
+			return $number;
+		} else if (is_int($number)) {
+			// Interpret signed integer as unsigned integer.
+			return sprintf('%u', $number);
+		} else {
+			throw new \UnexpectedValueException(
+				'Expected int, float or base-10 string'
+			);
+		}
+	}
+
+	/**
 	* @brief Tries to get the filesize of a file via various workarounds that
 	*        even work for large files on 32-bit platforms.
 	*
diff --git a/tests/lib/largefilehelper.php b/tests/lib/largefilehelper.php
new file mode 100644
index 0000000..5db1f9c
--- /dev/null
+++ b/tests/lib/largefilehelper.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Copyright (c) 2014 Andreas Fischer <bantu at owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test;
+
+class LargeFileHelper extends \PHPUnit_Framework_TestCase {
+	protected $helper;
+
+	public function setUp() {
+		parent::setUp();
+		$this->helper = new \OC\LargeFileHelper;
+	}
+
+	public function testFormatUnsignedIntegerFloat() {
+		$this->assertSame(
+			'9007199254740992',
+			$this->helper->formatUnsignedInteger((float) 9007199254740992)
+		);
+	}
+
+	public function testFormatUnsignedIntegerInt() {
+		$this->assertSame(
+			PHP_INT_SIZE === 4 ? '4294967295' : '18446744073709551615',
+			$this->helper->formatUnsignedInteger(-1)
+		);
+	}
+
+	public function testFormatUnsignedIntegerString() {
+		$this->assertSame(
+			'9007199254740993',
+			$this->helper->formatUnsignedInteger('9007199254740993')
+		);
+	}
+
+	/**
+	* @expectedException \UnexpectedValueException
+	*/
+	public function testFormatUnsignedIntegerStringException() {
+		$this->helper->formatUnsignedInteger('900ABCD254740993');
+	}
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list