get byte size from string

@param string $size_string       string expression of byte size. ex) 2MB, 100KB, 3.5GB

@return float     size in bytes

@throws InvalidArgumentException
436483
public static function getByteSizeFromString( string $size_string ) : float
    {
        if ( ($pos=strpos($size_string,'TB')) > 0 ){
            // TB
            $number = substr($size_string,0,$pos);
            if ( is_numeric($number) ){
                return intval(self::BYTES_TB * $number);
            }
        }
        else if ( ($pos=strpos($size_string,'GB')) > 0 ){
            // GB
            $number = substr($size_string,0,$pos);
            if ( is_numeric($number) ){
                return intval(self::BYTES_GB * $number);
            }
        }
        else if ( ($pos=strpos($size_string,'MB')) > 0 ){
            // MB
            $number = substr($size_string,0,$pos);
            if ( is_numeric($number) ){
                return intval(self::BYTES_MB * $number);
            }
        }
        else if ( ($pos=strpos($size_string,'KB')) > 0 ){
            // KB
            $number = substr($size_string,0,$pos);
            if ( is_numeric($number) ){
                return intval(self::BYTES_KB * $number);
            }
        }
        else if ( ($pos=strpos($size_string,'B')) > 0 ){
            // B
            $number = substr($size_string,0,$pos);
            if ( is_numeric($number) ){
                return intval($number);
            }
        }
        else if ( is_numeric($size_string) ){
            return intval($size_string);
        }
    
        throw( new InvalidArgumentException(1,$size_string) );
    }
1283744
def memory_usage(self, string=False):
        
        if string:
            n = getsizeof(self)
            return ' '.join((str(s) for s in convert_bytes(n)))
        return self.info()['size']
388960
function stringSize(string) {
      if (!(string && reHasComplexSymbol.test(string))) {
        return string.length;
      }
      var result = reComplexSymbol.lastIndex = 0;
      while (reComplexSymbol.test(string)) {
        result++;
      }
      return result;
    }
696460
protected function getByteSize($size)
    {
        if (! preg_match('/(\d+)\s*([tgkmbi]{1,3})/i', $size, $result)) {
            return $size;
        }

        $mapping = $this->mapping[strtolower($result[2])];
        return pow($mapping[0], $mapping[1]) * $result[1];
    }
1369790
def sizeof(s):
    
    if hasattr(s, '_size_'):
        return s._size_

    elif isinstance(s, bytes):
        return len(s)

    raise ValueError(s)
1287873
private static int getBytesSize(ArrayList<String> arr) {
        int count=0;
        
        for (String str : arr) {
            count = count + str.length();
        }
        return count;
    }
821300
public function getImageDataUri($string, $size = 64, $color = null, $backgroundColor = null)
    {
        return sprintf('data:%s;base64,%s', $this->generator->getMimeType(), base64_encode($this->getImageData($string, $size, $color, $backgroundColor)));
    }
426431
function convertMemorySize($strval, string $to_unit = 'b'){
        $strval    = strtolower(str_replace(' ', '', $strval));
        $val       = floatval($strval);
        $to_unit   = strtolower(trim($to_unit))[0];
        $from_unit = str_replace($val, '', $strval);
        $from_unit = empty($from_unit) ? 'b' : trim($from_unit)[0];
        $units     = 'kmgtph';  // (k)ilobyte, (m)egabyte, (g)igabyte and so on...

        // Convert to bytes
        if ($from_unit !== 'b'){
            $val *= 1024 ** (strpos($units, $from_unit) + 1);
        }

        // Convert to unit
        if ($to_unit !== 'b'){
            $val /= 1024 ** (strpos($units, $to_unit) + 1);
        }

        $val = $val.' '.($to_unit!='b'?strtoupper($to_unit).'B':'B');

        return $val;
    }
561826
public function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT)
    {
        $diff = strlen($input) - mb_strlen($input);
        return str_pad($input, $pad_length + $diff, $pad_string, $pad_type);
    }
1225104
function _GetSize($ftype, $ty, $fsize, $fprec)
	{
		if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
			$ftype .= "(".$fsize;
			if (strlen($fprec)) $ftype .= ",".$fprec;
			$ftype .= ')';
		}
		return $ftype;
	}
590760
public function getStringByByte($bytes, $decimals = 2, $decimalPoint = '.', $thousandsSeparator = ',')
    {
        $size = [
            'B',
            'kB',
            'MB',
            'GB',
            'TB',
            'PB',
            'EB',
            'ZB',
            'YB',
        ];

        $factor = intval(floor((strlen($bytes) - 1) / 3));
        if (false === isset($size[$factor])) {
            return $bytes;
        }

        return sprintf('%s %s', number_format($bytes / pow(1024, $factor), $decimals, $decimalPoint, $thousandsSeparator), $size[$factor]);
    }
919380
def GetByteSize(self):
    
    if self.size == definitions.SIZE_NATIVE or self.units != 'bytes':
      return None

    return self.size
1117528
def sizes_from_string(string = "0x0")
      string = "0x0" if string.nil? || string.empty?

      raise ArgumentError unless string =~ /(\d*x\d*)/

      width, height = string.scan(/(\d*)x(\d*)/)[0].map(&:to_i)

      width = 0 if width.nil?
      height = 0 if height.nil?
      {
        width: width,
        height: height
      }
    end
809188
def strsize (b):
    """"""
    if b < 0:
        raise ValueError("Invalid negative byte number")
    if b < 1024:
        return "%dB" % b
    if b < 1024 * 10:
        return "%dKB" % (b // 1024)
    if b < 1024 * 1024:
        return "%.2fKB" % (float(b) / 1024)
    if b < 1024 * 1024 * 10:
        return "%.2fMB" % (float(b) / (1024*1024))
    if b < 1024 * 1024 * 1024:
        return "%.1fMB" % (float(b) / (1024*1024))
    if b < 1024 * 1024 * 1024 * 10:
        return "%.2fGB" % (float(b) / (1024*1024*1024))
    return "%.1fGB" % (float(b) / (1024*1024*1024))
1065489
public static function len($string): int
    {
        if (self::isMBString()) {
            return mb_strlen($string, self::$encoding);
        }

        return strlen($string); // @codeCoverageIgnore
    }
768438
public static String byte2FitMemorySize(final long byteNum) {
        if (byteNum < 0) {
            return "shouldn't be less than zero!";
        } else if (byteNum < MemoryConst.KB) {
            return String.format("%.3fB", (double) byteNum);
        } else if (byteNum < MemoryConst.MB) {
            return String.format("%.3fKB", (double) byteNum / MemoryConst.KB);
        } else if (byteNum < MemoryConst.GB) {
            return String.format("%.3fMB", (double) byteNum / MemoryConst.MB);
        } else {
            return String.format("%.3fGB", (double) byteNum / MemoryConst.GB);
        }
    }
1159708
public function read($count_bytes) {
    // If we have data in the read_buffer then use it.
    $readBuffer_size = strlen($this->read_buffer);
    $bytes_available = $readBuffer_size - $this->buffer_read_position;

    // If there are no more bytes available then get some.
    if ($bytes_available === 0 && !$this->eof) {
      // If we know the object size, check it first.
      $object_bytes_read = $this->object_block_start_position +
                           $this->buffer_read_position;
      if ($object_bytes_read === $this->object_total_length ||
          !isset($this->next_read_position)) {
        $this->eof = true;
        return false;
      }
      if (!$this->fillReadBuffer($this->next_read_position)) {
        return false;
      }
      // Re-calculate the number of bytes we can serve.
      $readBuffer_size = strlen($this->read_buffer);
      $bytes_available = $readBuffer_size - $this->buffer_read_position;
    }

    if ($bytes_available > 0) {
      $bytes_to_read = min($bytes_available, $count_bytes);
      $current_buffer_position = $this->buffer_read_position;
      $this->buffer_read_position += $bytes_to_read;

      return substr($this->read_buffer,
                    $current_buffer_position,
                    $bytes_to_read);
    }

    return false;
  }