From e25b171e6b64ffe233bc5e83680242da6f538340 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Mon, 4 Mar 2024 15:33:59 -0500 Subject: [PATCH 1/2] Simple number-printing utility function. 4271 -> "Four thousand two hundred seventy one" Why? Well, for one it permits the generation of larger strings from simple 32-bit seed numbers. This permits sort of a "decompression" of string data from simple inputs to do some fuzz testing or other things. --- Utility/CMakeLists.txt | 1 + Utility/print_number.h | 115 +++++++++++++++++++++++ Utility/print_number.test/.0.cc.swp | Bin 0 -> 12288 bytes Utility/print_number.test/0.cc | 39 ++++++++ Utility/print_number.test/CMakeLists.txt | 1 + 5 files changed, 156 insertions(+) create mode 100644 Utility/print_number.h create mode 100644 Utility/print_number.test/.0.cc.swp create mode 100644 Utility/print_number.test/0.cc create mode 100644 Utility/print_number.test/CMakeLists.txt diff --git a/Utility/CMakeLists.txt b/Utility/CMakeLists.txt index 68244fa..e722143 100644 --- a/Utility/CMakeLists.txt +++ b/Utility/CMakeLists.txt @@ -2,3 +2,4 @@ target_sources( alepha PRIVATE fancyTypeName.cc ) +add_subdirectory( print_number.test ) diff --git a/Utility/print_number.h b/Utility/print_number.h new file mode 100644 index 0000000..bebb7e2 --- /dev/null +++ b/Utility/print_number.h @@ -0,0 +1,115 @@ +static_assert( __cplusplus > 2020'99 ); + +#pragma once + +#include + +#include + +#include +#include +#include +#include + +namespace Alepha::Hydrogen::Utility ::detail:: print_number_m +{ + inline namespace exports + { + std::string formatPrint( std::uint64_t ); + } + + inline std::string + exports::formatPrint( std::uint64_t value ) + { + const std::map< int, std::string > names + { + { 1, "one" }, + { 2, "two" }, + { 3, "three" }, + { 4, "four" }, + { 5, "five" }, + { 6, "six" }, + { 7, "seven" }, + { 8, "eight" }, + { 9, "nine" }, + { 10, "ten" }, + { 11, "eleven" }, + { 12, "twelve" }, + { 13, "thirteen" }, + { 14, "fourteen" }, + { 15, "fifteen" }, + { 16, "sixteen" }, + { 17, "seventeen" }, + { 18, "eighteen" }, + { 19, "nineteen" }, + }; + + const std::map< int, std::string > tens + { + { 20, "twenty" }, + { 30, "thirty" }, + { 40, "forty" }, + { 50, "fifty" }, + { 60, "sixty" }, + { 70, "seventy" }, + { 80, "eighty" }, + { 90, "ninety" }, + }; + + // One Thousand to the N + const std::map< std::uint64_t, std::string > powers + { + { 0, "" }, + { 1, "thousand" }, + { 2, "million" }, + { 3, "billion" }, + { 4, "trillion" }, + { 5, "quadrillion" }, + { 6, "quintillion" }, + }; + + std::vector< std::string > parts; + + if( value == 0 ) return "zero"; + + for( int millennium= 0; value; ++millennium ) + { + auto remainder= value % 1000; + value/= 1000; + if( not remainder ) continue; + std::string next; + if( remainder >= 100 ) next+= names.at( remainder / 100 ) + " hundred "; + remainder%= 100; + if( remainder >= 20 ) + { + next+= tens.at( remainder - remainder % 10 ); + if( remainder % 10 ) next+= " " + names.at( remainder % 10 ); + } + else if( remainder ) + { + next+= names.at( remainder ); + } + next+= " " + powers.at( millennium ); + parts.push_back( std::move( next ) ); + } + + std::reverse( begin( parts ), end( parts ) ); + std::string rv; + bool first= true; + for( auto part: parts ) + { + while( part.back() == ' ' ) part.pop_back(); + while( part.front() == ' ' ) part= part.substr( 1 ); + if( not first ) rv+= ' '; + rv+= part; + first= false; + } + + return rv; + } +} + +namespace Alepha::Hydrogen::Utility::inline exports::inline print_number_m +{ + using namespace detail::print_number_m::exports; +} diff --git a/Utility/print_number.test/.0.cc.swp b/Utility/print_number.test/.0.cc.swp new file mode 100644 index 0000000000000000000000000000000000000000..c8103e9875ae672b8582fde128289801aecdbb38 GIT binary patch literal 12288 zcmeI2!Ef9|6vii9&_dHzMcg0GVH2s{^=>QLIGe3Xxo`l1)F1(Z)x`T!8+pgZGwzZ^ zq4)O44T&RHE}TG|`4>QfEB^rpxbcnG@ivh{!m-s`>07VIZ{B?G=NU`Z&TjbOJMYqF zZ=mqGs?>L@KeKs#T^;P`SMucUINS;UM(!Yrz}z+O?evb(J6?AOwVf z5D)@FKnMr{As_@^3<9oLQIAo|OHDc5=D2d|*qq8i2nYcoAOwVf5D)@FKnMr{As_^V zfDm{N3FxU(-@KyKd#}Pg{{KII|NrfZQolk!K|exYLnZVD^cHjvx&~c^{=BTz_s{_} zhdzexLYJXmR?!D~8T$Q_Qcs~L&}UE=T7v@U3Umqj8$SMm{(ydfzJ)%5cAyWS+UIk~ zSu6_yAs_^VfDjM@LO=+-hy;$dRL84x1a+*NMp2wsjz%L|?0Ai$X3J|7?d9c6yZ8Oh z*L8ZNvx;fM2iCpVOf#RS$&nfwj~7ADv^JT8)D)GAC}?}{WtB~qffa^$^VY;I6C5J- zJV`TNUkn#xfDP3_^Npb7A`2#d6E2OmKlxNO6?Hm6ZkSw|W?7mWa`T*$%1le15~mLn zw@)r9nQ68N24u= z*}w*DUVLy^S{j9;rA2RVah2-Io4U>}+$DKswV7VPm4oXx{8^Hi6mEaPC>-AMTqrmv zxV2y3o?{OUi@m%m+jModGo#u(C!p4yZ`XH-f|FO;4jy=YTaO1=;y>UrudgjisZZ;E zm};Z#2YNT-2bgL9y*y|fMfbI3t2)8kIoWhpWK7sC&Se_wjQm`+6z4N0za+G!In!Jl zcXu(5Rj0D4c}Paj*cLkGb}{;~aV3iQ%L3%>mO50{X_v;NE0G;M(-awyl70ra2R$sH)&%_{R<>(t;hfX literal 0 HcmV?d00001 diff --git a/Utility/print_number.test/0.cc b/Utility/print_number.test/0.cc new file mode 100644 index 0000000..b7c9f96 --- /dev/null +++ b/Utility/print_number.test/0.cc @@ -0,0 +1,39 @@ +static_assert( __cplusplus > 2020'99 ); + +#include "../print_number.h" + +#include +#include + +#include + +static auto init= Alepha::Utility::enroll <=[] +{ + using namespace Alepha::Testing::exports; + "Do simple numerical tests come out as we want?"_test + <=TableTest< Alepha::Utility::formatPrint >::Cases + { + { "zero", { 0 }, "zero" }, + { "one", { 1 }, "one" }, + { "seven hundred", { 700 }, "seven hundred" }, + { "one thousand twenty four", { 1024 }, "one thousand twenty four" }, + { "ten thousand two hundred forty", { 10240 }, "ten thousand two hundred forty" }, + { "1283721", { 1'283'721 }, "one million two hundred eighty three thousand seven hundred twenty one" }, + { "1000283000721", { 1'000'283'000'721 }, + "one trillion two hundred eighty three million seven hundred twenty one" }, + + { "1000283000721", { 1'000'283'000'000 }, + "one trillion two hundred eighty three million" }, + }; + + -"repl"_test <=[] + { + while( std::cin ) + { + std::uint64_t value; + std::cout << "input: " << std::flush; + std::cin >> value; + std::cout << Alepha::Utility::formatPrint( value ) << std::endl; + } + }; +}; diff --git a/Utility/print_number.test/CMakeLists.txt b/Utility/print_number.test/CMakeLists.txt new file mode 100644 index 0000000..b099603 --- /dev/null +++ b/Utility/print_number.test/CMakeLists.txt @@ -0,0 +1 @@ +unit_test( 0 ) From f9553de81fe4b8082fb9cd4532d3ce0682363b9f Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Thu, 21 Mar 2024 17:18:46 -0400 Subject: [PATCH 2/2] Fix some issues with the initial pull request for number printing. --- Utility/print_number.test/.0.cc.swp | Bin 12288 -> 0 bytes Utility/print_number.test/0.cc | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 Utility/print_number.test/.0.cc.swp diff --git a/Utility/print_number.test/.0.cc.swp b/Utility/print_number.test/.0.cc.swp deleted file mode 100644 index c8103e9875ae672b8582fde128289801aecdbb38..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2!Ef9|6vii9&_dHzMcg0GVH2s{^=>QLIGe3Xxo`l1)F1(Z)x`T!8+pgZGwzZ^ zq4)O44T&RHE}TG|`4>QfEB^rpxbcnG@ivh{!m-s`>07VIZ{B?G=NU`Z&TjbOJMYqF zZ=mqGs?>L@KeKs#T^;P`SMucUINS;UM(!Yrz}z+O?evb(J6?AOwVf z5D)@FKnMr{As_@^3<9oLQIAo|OHDc5=D2d|*qq8i2nYcoAOwVf5D)@FKnMr{As_^V zfDm{N3FxU(-@KyKd#}Pg{{KII|NrfZQolk!K|exYLnZVD^cHjvx&~c^{=BTz_s{_} zhdzexLYJXmR?!D~8T$Q_Qcs~L&}UE=T7v@U3Umqj8$SMm{(ydfzJ)%5cAyWS+UIk~ zSu6_yAs_^VfDjM@LO=+-hy;$dRL84x1a+*NMp2wsjz%L|?0Ai$X3J|7?d9c6yZ8Oh z*L8ZNvx;fM2iCpVOf#RS$&nfwj~7ADv^JT8)D)GAC}?}{WtB~qffa^$^VY;I6C5J- zJV`TNUkn#xfDP3_^Npb7A`2#d6E2OmKlxNO6?Hm6ZkSw|W?7mWa`T*$%1le15~mLn zw@)r9nQ68N24u= z*}w*DUVLy^S{j9;rA2RVah2-Io4U>}+$DKswV7VPm4oXx{8^Hi6mEaPC>-AMTqrmv zxV2y3o?{OUi@m%m+jModGo#u(C!p4yZ`XH-f|FO;4jy=YTaO1=;y>UrudgjisZZ;E zm};Z#2YNT-2bgL9y*y|fMfbI3t2)8kIoWhpWK7sC&Se_wjQm`+6z4N0za+G!In!Jl zcXu(5Rj0D4c}Paj*cLkGb}{;~aV3iQ%L3%>mO50{X_v;NE0G;M(-awyl70ra2R$sH)&%_{R<>(t;hfX diff --git a/Utility/print_number.test/0.cc b/Utility/print_number.test/0.cc index b7c9f96..2549748 100644 --- a/Utility/print_number.test/0.cc +++ b/Utility/print_number.test/0.cc @@ -22,7 +22,7 @@ static auto init= Alepha::Utility::enroll <=[] { "1000283000721", { 1'000'283'000'721 }, "one trillion two hundred eighty three million seven hundred twenty one" }, - { "1000283000721", { 1'000'283'000'000 }, + { "1000283000000", { 1'000'283'000'000 }, "one trillion two hundred eighty three million" }, };