{"id":22220291,"url":"https://github.com/yanminhui/misc","last_synced_at":"2026-04-08T16:31:47.770Z","repository":{"id":153957874,"uuid":"175857075","full_name":"yanminhui/misc","owner":"yanminhui","description":"Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script","archived":false,"fork":false,"pushed_at":"2023-08-08T04:29:16.000Z","size":98,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-01T05:37:23.639Z","etag":null,"topics":["c","cplusplus","cplusplus11","cplusplus14","cplusplus17","cplusplus20","cpp","javascript","python","python2","python3"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yanminhui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-15T16:38:44.000Z","updated_at":"2023-04-20T15:01:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"333a69d8-5f30-438d-889e-27d3421f3a8d","html_url":"https://github.com/yanminhui/misc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yanminhui/misc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanminhui%2Fmisc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanminhui%2Fmisc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanminhui%2Fmisc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanminhui%2Fmisc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yanminhui","download_url":"https://codeload.github.com/yanminhui/misc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanminhui%2Fmisc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31564868,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"ssl_error","status_checked_at":"2026-04-08T14:31:17.202Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cplusplus","cplusplus11","cplusplus14","cplusplus17","cplusplus20","cpp","javascript","python","python2","python3"],"created_at":"2024-12-02T23:07:58.092Z","updated_at":"2026-04-08T16:31:47.754Z","avatar_url":"https://github.com/yanminhui.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Miscellaneous Skills\n\nMiscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script.\n\n## C/C++/11/14/17/20\n\n### [format_bytes](https://github.com/yanminhui/misc/blob/master/cpp/format_bytes.hpp)\n\nGiven a byte count, converts it to human-readable format \nand returns a string consisting of a value and a units indicator.\n\nDepending on the size of the value, the units part is bytes, \nKB (kibibytes), MB (mebibytes), GB (gibibytes), TB (tebibytes), \nor PB (pebibytes)...\n\n**Function Prototype**\n\n```.cpp\ntemplate\u003ctypename CharT, typename ByteT\u003e\nCharT const* format_bytes(std::basic_string\u003cCharT\u003e\u0026 repr             // (1)\n                        , ByteT const bytes\n                        , std::size_t const decimal=2u\n                        , std::size_t const reduced_unit=1024u);\n\ntemplate\u003ctypename CharT, typename ByteT, typename IndicatorT\n       , typename = typename std::enable_if\u003c!std::is_integral\u003cIndicatorT\u003e::value\u003e::type\u003e\nCharT const* format_bytes(std::basic_string\u003cCharT\u003e\u0026 repr             // (2)\n                        , ByteT const bytes\n                        , IndicatorT\u0026\u0026 indicator\n                        , std::size_t const decimal=2u\n                        , std::size_t const reduced_unit=1024u);\n\ntemplate\u003ctypename CharT, typename ByteT, typename InputIt\u003e\nCharT const* format_bytes(std::basic_string\u003cCharT\u003e\u0026 repr             // (3)\n                        , ByteT const bytes\n                        , InputIt first, InputIt last\n                        , std::size_t const decimal=2u\n                        , std::size_t const reduced_unit=1024u);\n\ntemplate\u003ctypename CharT, typename ByteT\n       , typename InputIt, typename IndicatorT\u003e\nCharT const* format_bytes(std::basic_string\u003cCharT\u003e\u0026 repr             // (4)\n                        , ByteT const bytes\n                        , InputIt first, InputIt last\n                        , IndicatorT\u0026\u0026 indicator\n                        , std::size_t const decimal=2u\n                        , std::size_t const reduced_unit=1024u);\n```\n\n**Usage**\n\n```.cpp\nusing namespace ymh::misc;\n\nstd::string s;\nstd::cout \u003c\u003c format_bytes(s, 18446640) \u003c\u003c std::endl;\n```\n\nequal to:\n\n```.cpp\nstd::wstring wcs;  // unicode\nauto indicators = { \"Bytes\", \"KB\", \"MB\", \"GB\" };\nformat_bytes(s, 18446640\n           , std::begin(indicators), std::end(indicators)\n           , \"MB\", 2u, 1024u);\n```\n\n**Output**\n\n```.sh\n17.60 MB\n```\n\n### [error_t](https://github.com/yanminhui/misc/blob/master/cpp/error.hpp)\n\nSave std::error_code, boost.system, GetLastError(), user custom error code \nand error message to class [w]error_t.\n \nQuery error information by dump() or dump_backtrace() from class [w]error_t,\nit can query error domain, value, message by the numbers of [w]error_t.\n \n**Function Prototype**\n \n(1) User Custom Error\n\n```.cpp\nSET_ERROR_CUSTOM[W](error_t\u0026, domain, value, format_string, ...);\nSET_ERROR_MESSAGE[W](error_t\u0026, value, format_string, ...);\nSET_ERROR_STRING[W](error_t\u0026, format_string, ...);\n```\n\n(2) std::error_code or boost.system\n\n```.cpp\nSET_ERROR_CODE[W](error_t\u0026, error_code);\nMAKE_ERROR_CODE[W](error_t\u0026, errc_t);\n```\n\n(3) System Error\n\n```.cpp\nSET_SYSTEM_ERROR[W](error_t\u0026, ::GetLastError());  // errno\n```\n\n(4) Catch Exception\n\n```.cpp\nERROR_TRY[W] {\n  // throw exception\n} ERROR_CATCH[W](error_t\u0026)\n```\n\n(5) Print Error Message to Stream\n\n```.cpp\nvoid error_t::dump(std::basic_ostream\u003ccharT\u003e\u0026);\nvoid error_t::dump_backtrace(std::basic_ostream\u003ccharT\u003e\u0026);\n```\n\n**Usage**\n\n```.cpp\nusing namespace ymh;\n\nerror_t err;\nSET_ERROR_STRING(err, \"Open file %s failed\", \"error.log\");\n\nif (err)  // return true if error occur\n{\n  SET_SYSTEM_ERROR(err, ::GetLastError());\n}\n```\n\n**Output**\n\n```.sh\nFile \"xxx.cpp\", line ?, in \u003cfunction\u003e: Open file error.log failed\nFile \"xxx.cpp\", line ?, in \u003cfunction\u003e: Success\n```\n\n### [una](https://github.com/yanminhui/misc/blob/master/cpp/una.hpp)\n\nUnicode and ANSI (byte string) conversion is supported by class `codec`.\n\nConvert wide char to multi bytes by `encode\u003ccodepage::cp_xxx, bom::nobomb\u003e` \nand reverse it by `decode\u003ccodepage::cp_xxx, bom::nobomb\u003e`. It can convert one \nmulti bytes to another multi bytes by `convert\u003c...\u003e`.\n\nOthers, it will try convert to local codepage if you use `file_text` to read \nfile's contents. You can save multi bytes to file by `save_file_text\u003c...\u003e`.\n\n_Attention_ : may throw `std::system_error` exception if failed.\n\n**Function Prototype**\n\n(1) Multi Bytes to Wide Char\n\n```.cpp\nstd::wstring wtext = decode\u003ccodepage::cp_utf8\u003e(u8\"utf-8 string\");\nwtext = UTF8ToUnicode\u003cbom::nobomb\u003e(u8\"utf-8 string\");\"\n```\n\n(2) Wide Char to Multi Bytes\n\n```.cpp\nstd::string text = encode\u003ccodepage::cp_utf8\u003e(L\"wide string\");\ntext = UnicodeToUTF8\u003cbom::bomb\u003e(L\"wide string\");\n```\n\n(3) Convert between Multi Bytes\n\n```.cpp\nstd::string ntext = convert\u003ccodepage::cp_utf8\u003e(\"ansi string\");\n```\n\n(4) Read/Save File Text\n\n```.cpp\nstd::wstring wtext = read_file_text(L\"demo.txt\");\nsave_file_text\u003ccodepage::cp_utf8, bom::bomb\u003e(\"demo.txt\", \"bingo\");\n```\n\n**Usage**\n\n```.cpp\nusing namespace ymh;\n\ntry\n{\n    auto text = file_text(\"demo.txt\");\n    auto wtext = decode(text);\n} \ncatch (std::system_error const\u0026 e)\n{\n}\n```\n\n## Python2/3\n\n### [mcr](https://github.com/yanminhui/misc/blob/master/py/mcr.py)\n\nMeasure compression ratio. `zlib`, `gzip`, `bz2`, `lzma` is supported.\n\n**Usage**\n\n```bash\nusage: mcr.py [-h] [--verbose] [--chunk-size {4,8,16,32,64,128,256,512}]\n              [--name {gzip,all,zlib,bz2}]\n              [--level {-2,-1,0,1,2,3,4,5,6,7,8,9}]\n              file\n\nMeasure compression ratio.\n\npositional arguments:\n  file                  file or directory\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --verbose             print progress status (default: None)\n  --chunk-size {4,8,16,32,64,128,256,512}\n                        data chunk's size (metric: KB) (default: 16)\n  --name {gzip,all,zlib,bz2}\n                        compression algorithm's name (default: all)\n  --level {-2,-1,0,1,2,3,4,5,6,7,8,9}\n                        controlling the level of compression, all = -2,\n                        default = -1 (default: -2)\n```\n\n**Example**\n\n```bash\n$ python3 mcr.py --level=-1 /dev/vda\nFile: /dev/vda, Length: 40.0 GB, Chunk Size: 16.0 KB\nNAME LVL OUTSIZE   EXPIRED %SAV  IN/ps       OUT/ps      RATIO %PROG REMAIN \nbz2    9 6.61 GB   1.1 h   83.48 10.33 MBps  1.71 MBps    6.05 100.0 0.0 s\nzlib  -1 8.74 GB   14.93 m 78.15 45.72 MBps  9.99 MBps    4.58 100.0 0.0 s\ngzip   9 8.75 GB   50.93 m 78.13 13.4 MBps   2.93 MBps    4.57 100.0 0.0 s\nlzma   0 4.43 GB   3.03 h  88.93 3.75 MBps   425.55 KBps  9.03 100.0 0.0 s\n```\n\n## ECMAScript5/6\n\n## Shell Script\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanminhui%2Fmisc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyanminhui%2Fmisc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanminhui%2Fmisc/lists"}