NumberFormat.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/NumberFormat.h>
  9. #include <AK/NumericLimits.h>
  10. #include <AK/StringView.h>
  11. namespace AK {
  12. // FIXME: Remove this hackery once printf() supports floats.
  13. static DeprecatedString number_string_with_one_decimal(u64 number, u64 unit, StringView suffix)
  14. {
  15. constexpr auto max_unit_size = NumericLimits<u64>::max() / 10;
  16. VERIFY(unit < max_unit_size);
  17. auto integer_part = number / unit;
  18. auto decimal_part = (number % unit) * 10 / unit;
  19. return DeprecatedString::formatted("{}.{} {}", integer_part, decimal_part, suffix);
  20. }
  21. DeprecatedString human_readable_quantity(u64 quantity, StringView unit)
  22. {
  23. constexpr u64 size_of_unit = 1024;
  24. constexpr auto unit_prefixes = AK::Array { "", "K", "M", "G", "T", "P", "E" };
  25. auto full_unit_suffix = [&](int index) {
  26. auto binary_infix = (size_of_unit == 1024 && index != 0) ? "i"sv : ""sv;
  27. return DeprecatedString::formatted("{}{}{}",
  28. unit_prefixes[index], binary_infix, unit);
  29. };
  30. auto size_of_current_unit = size_of_unit;
  31. if (quantity < size_of_unit)
  32. return DeprecatedString::formatted("{} {}", quantity, full_unit_suffix(0));
  33. for (size_t i = 1; i < unit_prefixes.size() - 1; i++) {
  34. auto suffix = full_unit_suffix(i);
  35. if (quantity < size_of_unit * size_of_current_unit) {
  36. return number_string_with_one_decimal(quantity, size_of_current_unit, suffix);
  37. }
  38. size_of_current_unit *= size_of_unit;
  39. }
  40. return number_string_with_one_decimal(quantity,
  41. size_of_current_unit, full_unit_suffix(unit_prefixes.size() - 1));
  42. }
  43. DeprecatedString human_readable_size(u64 size)
  44. {
  45. return human_readable_quantity(size, "B"sv);
  46. }
  47. DeprecatedString human_readable_size_long(u64 size)
  48. {
  49. if (size < 1 * KiB)
  50. return DeprecatedString::formatted("{} bytes", size);
  51. else
  52. return DeprecatedString::formatted("{} ({} bytes)", human_readable_size(size), size);
  53. }
  54. DeprecatedString human_readable_time(i64 time_in_seconds)
  55. {
  56. auto days = time_in_seconds / 86400;
  57. time_in_seconds = time_in_seconds % 86400;
  58. auto hours = time_in_seconds / 3600;
  59. time_in_seconds = time_in_seconds % 3600;
  60. auto minutes = time_in_seconds / 60;
  61. time_in_seconds = time_in_seconds % 60;
  62. StringBuilder builder;
  63. if (days > 0)
  64. builder.appendff("{} day{} ", days, days == 1 ? "" : "s");
  65. if (hours > 0)
  66. builder.appendff("{} hour{} ", hours, hours == 1 ? "" : "s");
  67. if (minutes > 0)
  68. builder.appendff("{} minute{} ", minutes, minutes == 1 ? "" : "s");
  69. builder.appendff("{} second{}", time_in_seconds, time_in_seconds == 1 ? "" : "s");
  70. return builder.to_deprecated_string();
  71. }
  72. DeprecatedString human_readable_digital_time(i64 time_in_seconds)
  73. {
  74. auto hours = time_in_seconds / 3600;
  75. time_in_seconds = time_in_seconds % 3600;
  76. auto minutes = time_in_seconds / 60;
  77. time_in_seconds = time_in_seconds % 60;
  78. StringBuilder builder;
  79. if (hours > 0)
  80. builder.appendff("{:02}:", hours);
  81. builder.appendff("{:02}:", minutes);
  82. builder.appendff("{:02}", time_in_seconds);
  83. return builder.to_deprecated_string();
  84. }
  85. }