]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Strings.php
Merge pull request #12390 from annando/fixes
[friendica.git] / src / Util / Strings.php
index 565c9bbb03b5cb026ab6e5317163c4df05a854ca..379f2a25213dda4ed89fc8d688eb4dffb4c038f6 100644 (file)
@@ -23,6 +23,7 @@ namespace Friendica\Util;
 
 use Friendica\Content\ContactSelector;
 use Friendica\Core\Logger;
+use Friendica\Core\System;
 use ParagonIE\ConstantTime\Base64;
 
 /**
@@ -220,7 +221,12 @@ class Strings
         */
        public static function formatBytes(int $bytes, int $precision = 2): string
        {
-               $units = ['B', 'KB', 'MB', 'GB', 'TB'];
+               // If this method is called for an infinite (== unlimited) amount of bytes:
+               if ($bytes == INF) {
+                       return INF;
+               }
+
+               $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
                $bytes = max($bytes, 0);
                $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
                $pow = min($pow, count($units) - 1);
@@ -475,7 +481,7 @@ class Strings
 
                $blocks = [];
 
-               $text = preg_replace_callback($regex,
+               $return = preg_replace_callback($regex,
                        function ($matches) use ($executionId, &$blocks) {
                                $return = '«block-' . $executionId . '-' . count($blocks) . '»';
 
@@ -486,7 +492,11 @@ class Strings
                        $text
                );
 
-               $text = $callback($text) ?? '';
+               if (is_null($return)) {
+                       Logger::warning('Received null value from preg_replace_callback', ['text' => $text, 'regex' => $regex, 'blocks' => $blocks, 'executionId' => $executionId, 'callstack' => System::callstack(10)]);
+               }
+
+               $text = $callback($return ?? $text) ?? '';
 
                // Restore code blocks
                $text = preg_replace_callback('/«block-' . $executionId . '-([0-9]+)»/iU',
@@ -502,4 +512,35 @@ class Strings
 
                return $text;
        }
-}
\ No newline at end of file
+
+       /**
+        * This function converts a PHP's shorhand notation string for file sizes in to an integer number of total bytes.
+        * For example: The string for shorthand notation of '2M' (which is 2,097,152 Bytes) is converted to 2097152
+        * @see https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
+        * @param string $shorthand
+        * @return int
+        */
+       public static function getBytesFromShorthand(string $shorthand): int
+       {
+               $shorthand = trim($shorthand);
+
+               if (is_numeric($shorthand)) {
+                       return $shorthand;
+               }
+
+               $last      = strtolower($shorthand[strlen($shorthand)-1]);
+               $shorthand = substr($shorthand, 0, -1);
+
+               switch($last) {
+                       case 'g':
+                               $shorthand *= 1024;
+                       case 'm':
+                               $shorthand *= 1024;
+                       case 'k':
+                               $shorthand *= 1024;
+               }
+
+               return $shorthand;
+       }
+
+}