{"id":4058,"date":"2017-06-21T16:06:06","date_gmt":"2017-06-21T21:06:06","guid":{"rendered":"http:\/\/neosmart.net\/blog\/?p=4058"},"modified":"2018-01-13T14:54:23","modified_gmt":"2018-01-13T20:54:23","slug":"prettysize-a-net-library-for-printing-human-readable-file-sizes","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/prettysize-a-net-library-for-printing-human-readable-file-sizes\/","title":{"rendered":"PrettySize: a .NET library for printing human-readable file sizes"},"content":{"rendered":"<p><a href=\"http:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size.png\" rel=\"follow\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-4074 colorbox-4058\" src=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-600x600.png\" alt=\"\" width=\"161\" height=\"161\" srcset=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-600x600.png 600w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-150x150.png 150w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-1024x1024.png 1024w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-300x300.png 300w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size.png 1200w\" sizes=\"auto, (max-width: 161px) 100vw, 161px\" \/><\/a>Continuing our promise to open source parts of our libraries and applications where possible, we&#8217;ve just released <code>PrettySize<\/code>, a C# and .NET library for representing file sizes in a human-readable (pretty) format. <code>PrettySize<\/code>\u00a0is available for free (MIT-licensed) <a href=\"https:\/\/github.com\/neosmart\/PrettySize.net\" rel=\"nofollow\">on GitHub<\/a> and <a href=\"https:\/\/www.nuget.org\/packages\/NeoSmart.PrettySize\/\" rel=\"follow\">via NuGet<\/a>\u00a0for those that are interested, and forks, contributions, and pull-requests are actively encouraged.<sup id=\"rf1-4058\"><a href=\"#fn1-4058\" title=\"If anyone wants to try their hand at implementing IFormattable, consider this an open invitation. It&rsquo;s not a functionality we ever needed, but some might find it useful.\" rel=\"footnote\">1<\/a><\/sup><\/p>\n<p>One of the best benefits of open-sourcing code is that it requires you to take a critical eye to what your code does and how it&#8217;s structured. Haphazard code interspersed throughout a dozen different files is cleaned up and re-organized in a way that can only bring benefits all around, from performance to ease-of-use, security, and future maintenance.<\/p>\n<p><!--more--><\/p>\n<p>In this example, <code>PrettySize<\/code>\u00a0went from supporting just a single notation format (<code>2.34 KiB<\/code>, for example), to supporting output using both base 10 (kilobyte, megabyte, KB, MB) and base 2 (kibibyte, mebibyte, KiB, MiB) units, with support for both abbreviated and unabridged units in the output.<\/p>\n<p>Additionally, open-sourcing <code>PrettySize<\/code>\u00a0also brought some performance improvements to bear (although any honest appraisal of the situation would start and end with pointing out that any sort of human-readable formatting is almost always going to be part of a much slower path in the code, namely printing to screen or similar, to an extent that actually forming the human-readable strings in question is going to be of negligible overhead), as the code went from a sequence of <code>if (...) else ...<\/code>\u00a0statements like this:<\/p>\n<pre><code class=\"language-csharp\">if (size &lt; 1 * (1L &lt;&lt; 10))\r\n{\r\n    return size + \" bytes\";\r\n}\r\nelse if (size &lt; 10 * (1L &lt;&lt; 10))\r\n{\r\n    return (size \/ (1.0 * (1L &lt;&lt; 10))).ToString(\"N2\") + \" KiB\";\r\n}\r\nelse if (size &lt; 100 * (1L &lt;&lt; 10))\r\n{\r\n    return (size \/ (1.0 * (1L &lt;&lt; 10))).ToString(\"N1\") + \" KiB\";\r\n}\r\n...\r\n<\/code><\/pre>\n<p>where conversions of larger file sizes would have to go through each successive test until the matching clause was found, to a binary search performed against a sorted array of match ranges.<\/p>\n<p>While the .NET Framework has included a binary tree-based <code>SortedDictionary<\/code>\u00a0since .NET 2.0, it unfortunately does not support &#8220;fuzzy&#8221; lookups (\u00e0 la C++&#8217;s <code>std::map<\/code>) where the nearest match can be returned, and so manipulations would be required to incoming sizes before being able to match them against a pre-existing lookup table. On the other hand, <code>Array.BinarySearch<\/code>\u00a0will return the (binary, two&#8217;s complement) of the closest (higher) match if no exact match was found, letting us create a sorted list of formatting rules for each range, and then find the nearest match to service incoming requests.<\/p>\n<p>In addition to converting numeric file sizes to formatted strings for human-readable representation, <code>PrettySize<\/code>\u00a0also includes constants that can be used to make expressing your own size-related calculations easier, with both base 2 and base 10 units (and their abbreviations) exposed.<\/p>\n<p>Some random facts learned in the process:<\/p>\n<ul>\n<li><code>Exabyte<\/code>\u00a0is the only base 10 unit that isn&#8217;t 8 characters long, throwing a wrench in our code formatting for no good reason;<\/li>\n<li>The corresponding base 2 unit, <code>exbibyte<\/code>, is a bit of an oddly-named unit; although perhaps understandably so since the remainder of the base 2 units all have <code>e<\/code>\u00a0as the second letter, and <code>eebibyte<\/code>\u00a0is a bit.. difficult to pronounce.<\/li>\n<\/ul>\n<p>Anyway, that&#8217;s all for now. Go ahead and grab <code>PrettySize<\/code>\u00a0<a href=\"https:\/\/www.nuget.org\/packages\/NeoSmart.PrettySize\" rel=\"follow\">from NuGet<\/a> or <a href=\"https:\/\/github.com\/neosmart\/PrettySize.net\" rel=\"nofollow\">off of GitHub<\/a>\u00a0and make life easier on yourself and your end users&#8217; eyes!<\/p>\n<p class=\"save\">Download: <a href=\"https:\/\/www.nuget.org\/packages\/NeoSmart.PrettySize\" rel=\"follow\">PrettySize 2.1.0<\/a><\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-4058\"><p>If anyone wants to try their hand at implementing <code>IFormattable<\/code>, consider this an open invitation. It&#8217;s not a functionality we ever needed, but some might find it useful.&nbsp;<a href=\"#rf1-4058\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Continuing our promise to open source parts of our libraries and applications where possible, we&#8217;ve just released PrettySize, a C# and .NET library for representing file sizes in a human-readable (pretty) format. PrettySize\u00a0is available for free (MIT-licensed) on GitHub and &hellip; <a href=\"https:\/\/neosmart.net\/blog\/prettysize-a-net-library-for-printing-human-readable-file-sizes\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[91,212,963,52,964],"class_list":["post-4058","post","type-post","status-publish","format-standard","hentry","category-software","tag-net","tag-development","tag-nuget","tag-open-source","tag-prettysize"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-13s","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=4058"}],"version-history":[{"count":10,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4058\/revisions"}],"predecessor-version":[{"id":4375,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4058\/revisions\/4375"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=4058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=4058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=4058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}