/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Data/Models/ByteSize.cs
71 строка
3 KB
Yair
Code Quality: Fixed crash when creating an archive
22 апр 2025, 18:22
22 апр 2025, 18:22
9fe3a93
Код
Авторство
О чём код?
// Copyright (c) Files Community // Licensed under the MIT License. using System.Collections.Frozen; namespace Files.App.Data.Models { public struct ByteSize : IEquatable<ByteSize>, IComparable<ByteSize> { private static readonly FrozenDictionary<string, string> units = new Dictionary<string, string> { [ByteSizeLib.ByteSize.BitSymbol] = Strings.ByteSymbol.ToLocalized(), [ByteSizeLib.ByteSize.ByteSymbol] = Strings.ByteSymbol.ToLocalized(), [ByteSizeLib.ByteSize.KiloByteSymbol] = Strings.KiloByteSymbol.ToLocalized(), [ByteSizeLib.ByteSize.MegaByteSymbol] = Strings.MegaByteSymbol.ToLocalized(), [ByteSizeLib.ByteSize.GigaByteSymbol] = Strings.GigaByteSymbol.ToLocalized(), [ByteSizeLib.ByteSize.TeraByteSymbol] = Strings.TeraByteSymbol.ToLocalized(), [ByteSizeLib.ByteSize.PetaByteSymbol] = Strings.PetaByteSymbol.ToLocalized(), }.ToFrozenDictionary(); private readonly ByteSizeLib.ByteSize size; public static readonly ByteSize Zero = new(0L); public static readonly ByteSize MaxValue = new(long.MaxValue); public ulong Bytes => (ulong)size.Bytes; public string ShortString => $"{size.LargestWholeNumberDecimalValue:0.##} {units[size.LargestWholeNumberDecimalSymbol]}"; public string LongString => $"{ShortString} ({size.Bytes:#,##0} {units[ByteSizeLib.ByteSize.ByteSymbol]})"; public static ByteSize FromBytes(ulong bytes) => new(bytes); public static ByteSize FromKibiBytes(ulong kibiBytes) => new(kibiBytes * ByteSizeLib.ByteSize.BytesInKibiByte); public static ByteSize FromMebiBytes(ulong mebiBytes) => new(mebiBytes * ByteSizeLib.ByteSize.BytesInMebiByte); public static ByteSize FromGibiBytes(ulong gibiBytes) => new(gibiBytes * ByteSizeLib.ByteSize.BytesInGibiByte); public static ByteSize FromTebiBytes(ulong tebiBytes) => new(tebiBytes * ByteSizeLib.ByteSize.BytesInTebiByte); public static ByteSize FromPebiBytes(ulong pebiBytes) => new(pebiBytes * ByteSizeLib.ByteSize.BytesInPebiByte); public static explicit operator ulong(ByteSize size) => size.Bytes; public static implicit operator ByteSize(ulong size) => new(size); public static ByteSize operator +(ByteSize a, ByteSize b) => new(a.Bytes + b.Bytes); public static ByteSize operator -(ByteSize a, ByteSize b) => new(a.Bytes - b.Bytes); public static bool operator ==(ByteSize a, ByteSize b) => a.size == b.size; public static bool operator !=(ByteSize a, ByteSize b) => a.size != b.size; public static bool operator <(ByteSize a, ByteSize b) => a.size < b.size; public static bool operator >(ByteSize a, ByteSize b) => a.size > b.size; public static bool operator <=(ByteSize a, ByteSize b) => a.size <= b.size; public static bool operator >=(ByteSize a, ByteSize b) => a.size >= b.size; public override string ToString() => ShortString; public override int GetHashCode() => size.GetHashCode(); public override bool Equals(object? other) => other is ByteSize size && Equals(size); public bool Equals(ByteSize other) => other.size.Equals(size); public int CompareTo(ByteSize other) => other.size.CompareTo(size); public ByteSize(ulong bytes) { if (bytes > long.MaxValue) { throw new ArgumentException($"The maximum size is {long.MaxValue}."); } size = ByteSizeLib.ByteSize.FromBytes((long)bytes); } } }