Este es un puerto de bytesize a Delphi/Freepascal.
TByteSize es un "registro" de utilidad que facilita la representación del tamaño del byte en el código al eliminar la ambigüedad del valor que se representa.
TByteSize es para bytes qué System.TimeSpan.TTimeSpan es para el tiempo.
Este proyecto se creó utilizando la actualización 1 de Delphi 10 Seattle, pero debería compilar en cualquier versión de Delphi desde 2009 y Freepascal 3.0.0 hacia arriba.
Sin TByteSize :
const
MaxFileSizeMBs = 1.5 ;
// I need it in KBs!
var
kilobytes: Double;
begin
kilobytes := MaxFileSizeMBs * 1024 ;
end ; Con ByteSize :
var
MaxFileSize: TByteSize;
begin
MaxFileSize := TByteSize.FromMegaBytes( 1.5 );
end ;
// I have it in KBs!
MaxFileSize.KiloBytes; TByteSize se comporta como cualquier otro record respaldado por un valor numérico.
// Add
var
monthlyUsage, currentUsage, total, delta, : TByteSize;
begin
monthlyUsage := TByteSize.FromGigaBytes( 10 );
currentUsage := TByteSize.FromMegaBytes( 512 );
total := monthlyUsage + currentUsage;
total.Add(ByteSize.FromKiloBytes( 10 ));
total.AddGigaBytes( 10 );
// Subtract
delta := total.Subtract(TByteSize.FromKiloBytes( 10 ));
delta := delta - TByteSize.FromGigaBytes( 100 );
delta := delta.AddMegaBytes(- 100 );
end ; Puede crear un "objeto" ByteSize a partir de bits , bytes , kilobytes , megabytes , gigabytes y terabytes .
TByteSize.Create( 1.5 ); // Constructor takes in bytes
// Static Constructors
TByteSize.FromBits( 10 ); // Bits are whole numbers only
TByteSize.FromBytes( 1.5 ); // Same as constructor
TByteSize.FromKiloBytes( 1.5 );
TByteSize.FromMegaBytes( 1.5 );
TByteSize.FromGigaBytes( 1.5 );
TByteSize.FromTeraBytes( 1.5 );
TByteSize.FromPetaBytes( 1.5 );
Un "objeto" TByteSize contiene representaciones en bits , bytes , kilobytes , megabytes , gigabytes , terabytes y petabytes .
var
maxFileSize: TByteSize;
begin
maxFileSize := TByteSize.FromKiloBytes( 10 );
maxFileSize.Bits; // 81920
maxFileSize.Bytes; // 10240
maxFileSize.KiloBytes; // 10
maxFileSize.MegaBytes; // 0.009765625
maxFileSize.GigaBytes; // 9.53674316e-6
maxFileSize.TeraBytes; // 9.31322575e-9
end ; Un "objeto" TByteSize también contiene dos propiedades que representan el símbolo y el valor de prefijo métrico más grandes.
var
maxFileSize: TByteSize;
begin
maxFileSize := TByteSize.FromKiloBytes( 10 );
maxFileSize.LargestWholeNumberSymbol; // "KB"
maxFileSize.LargestWholeNumberValue; // 10
end ; Todas las operaciones de análisis de cadena ( String to TByteSize ) se localizan para usar el separador decimal número de la configuración regional actualmente establecida.
TByteSize viene con un práctico método ToString que utiliza el prefijo métrico más grande cuyo valor es mayor o igual a 1.
TByteSize.FromBits( 7 ).ToString; // 7 b
TByteSize.FromBits( 8 ).ToString; // 1 B
TByteSize.FromKiloBytes(. 5 ).ToString; // 512 B
TByteSize.FromKiloBytes( 1000 ).ToString; // 1000 KB
TByteSize.FromKiloBytes( 1024 ).ToString; // 1 MB
TByteSize.FromGigabytes(. 5 ).ToString; // 512 MB
TByteSize.FromGigabytes( 1024 ).ToString; // 1 TB El método ToString acepta un solo parámetro string para formatear la salida. El formato puede contener el símbolo del valor para mostrar: b , B , KB , MB , GB , TB , PB . El formateador utiliza el método FormatFloat incorporado. El formato de número predeterminado es #.## que redondea el número a dos decimales.
Puede incluir formatos de símbolos y números.
var
b: TByteSize;
begin
b := TByteSize.FromKiloBytes( 10.505 );
// Default number format is #.##
b.ToString( ' KB ' ); // 10.52 KB
b.ToString( ' MB ' ); // .01 MB
b.ToString( ' b ' ); // 86057 b
// Default symbol is the largest metric prefix value >= 1
b.ToString( ' #.# ' ); // 10.5 KB
// All valid values of double.ToString(string format) are acceptable
b.ToString( ' 0.0000 ' ); // 10.5050 KB
b.ToString( ' 000.00 ' ); // 010.51 KB
// You can include number format and symbols
b.ToString( ' #.#### MB ' ); // .0103 MB
b.ToString( ' 0.00 GB ' ); // 0 GB
b.ToString( ' #.## B ' ); // 10757.12 B
end ; ByteSize tiene un método Parse e TryParse .
Al igual que otros métodos TryParse , ByteSize.TryParse Devuelve el valor boolean que indica si el análisis fue exitoso o no. Si se analiza el valor, se emite al parámetro out suministrado.
var
output: TByteSize;
begin
TByteSize.TryParse( ' 1.5mb ' , output);
// Invalid
TByteSize.Parse( ' 1.5 b ' ); // Can't have partial bits
// Valid
TByteSize.Parse( ' 5b ' );
TByteSize.Parse( ' 1.55B ' );
TByteSize.Parse( ' 1.55KB ' );
TByteSize.Parse( ' 1.55 kB ' ); // Spaces are trimmed
TByteSize.Parse( ' 1.55 kb ' );
TByteSize.Parse( ' 1.55 MB ' );
TByteSize.Parse( ' 1.55 mB ' );
TByteSize.Parse( ' 1.55 mb ' );
TByteSize.Parse( ' 1.55 GB ' );
TByteSize.Parse( ' 1.55 gB ' );
TByteSize.Parse( ' 1.55 gb ' );
TByteSize.Parse( ' 1.55 TB ' );
TByteSize.Parse( ' 1.55 tB ' );
TByteSize.Parse( ' 1.55 tb ' );
TByteSize.Parse( ' 1,55 kb ' ); // de-DE culture
end ;### Pruebas unitarias
Unit Tests can be found in ByteSizeLib.Tests Folder.
The unit tests makes use of DUnitX and TestInsight.
###Licencia
Este "software" tiene licencia bajo MIT License (MIT) .
1MhFfW7tDuEHQSgie65uJcAfJgCNchGeKf0x6c1DC21aeC49A822A4f1E3bf07c623C2C1978a98345367-40###Conclusión
Un agradecimiento especial a Omar Khudeira por esta increíble biblioteca. (Gracias a los desarrolladores del marco de prueba Dunitx y TestInsight por hacer herramientas que simplifican las pruebas unitarias.