https://github.com/yanminhui/misc
Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script
https://github.com/yanminhui/misc
c cplusplus cplusplus11 cplusplus14 cplusplus17 cplusplus20 cpp javascript python python2 python3
Last synced: 8 months ago
JSON representation
Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script
- Host: GitHub
- URL: https://github.com/yanminhui/misc
- Owner: yanminhui
- License: mit
- Created: 2019-03-15T16:38:44.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-08T04:29:16.000Z (over 2 years ago)
- Last Synced: 2025-03-25T07:41:41.932Z (about 1 year ago)
- Topics: c, cplusplus, cplusplus11, cplusplus14, cplusplus17, cplusplus20, cpp, javascript, python, python2, python3
- Language: C++
- Homepage:
- Size: 95.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Miscellaneous Skills
Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script.
## C/C++/11/14/17/20
### [format_bytes](https://github.com/yanminhui/misc/blob/master/cpp/format_bytes.hpp)
Given a byte count, converts it to human-readable format
and returns a string consisting of a value and a units indicator.
Depending on the size of the value, the units part is bytes,
KB (kibibytes), MB (mebibytes), GB (gibibytes), TB (tebibytes),
or PB (pebibytes)...
**Function Prototype**
```.cpp
template
CharT const* format_bytes(std::basic_string& repr // (1)
, ByteT const bytes
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
template::value>::type>
CharT const* format_bytes(std::basic_string& repr // (2)
, ByteT const bytes
, IndicatorT&& indicator
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
template
CharT const* format_bytes(std::basic_string& repr // (3)
, ByteT const bytes
, InputIt first, InputIt last
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
template
CharT const* format_bytes(std::basic_string& repr // (4)
, ByteT const bytes
, InputIt first, InputIt last
, IndicatorT&& indicator
, std::size_t const decimal=2u
, std::size_t const reduced_unit=1024u);
```
**Usage**
```.cpp
using namespace ymh::misc;
std::string s;
std::cout << format_bytes(s, 18446640) << std::endl;
```
equal to:
```.cpp
std::wstring wcs; // unicode
auto indicators = { "Bytes", "KB", "MB", "GB" };
format_bytes(s, 18446640
, std::begin(indicators), std::end(indicators)
, "MB", 2u, 1024u);
```
**Output**
```.sh
17.60 MB
```
### [error_t](https://github.com/yanminhui/misc/blob/master/cpp/error.hpp)
Save std::error_code, boost.system, GetLastError(), user custom error code
and error message to class [w]error_t.
Query error information by dump() or dump_backtrace() from class [w]error_t,
it can query error domain, value, message by the numbers of [w]error_t.
**Function Prototype**
(1) User Custom Error
```.cpp
SET_ERROR_CUSTOM[W](error_t&, domain, value, format_string, ...);
SET_ERROR_MESSAGE[W](error_t&, value, format_string, ...);
SET_ERROR_STRING[W](error_t&, format_string, ...);
```
(2) std::error_code or boost.system
```.cpp
SET_ERROR_CODE[W](error_t&, error_code);
MAKE_ERROR_CODE[W](error_t&, errc_t);
```
(3) System Error
```.cpp
SET_SYSTEM_ERROR[W](error_t&, ::GetLastError()); // errno
```
(4) Catch Exception
```.cpp
ERROR_TRY[W] {
// throw exception
} ERROR_CATCH[W](error_t&)
```
(5) Print Error Message to Stream
```.cpp
void error_t::dump(std::basic_ostream&);
void error_t::dump_backtrace(std::basic_ostream&);
```
**Usage**
```.cpp
using namespace ymh;
error_t err;
SET_ERROR_STRING(err, "Open file %s failed", "error.log");
if (err) // return true if error occur
{
SET_SYSTEM_ERROR(err, ::GetLastError());
}
```
**Output**
```.sh
File "xxx.cpp", line ?, in : Open file error.log failed
File "xxx.cpp", line ?, in : Success
```
### [una](https://github.com/yanminhui/misc/blob/master/cpp/una.hpp)
Unicode and ANSI (byte string) conversion is supported by class `codec`.
Convert wide char to multi bytes by `encode`
and reverse it by `decode`. It can convert one
multi bytes to another multi bytes by `convert<...>`.
Others, it will try convert to local codepage if you use `file_text` to read
file's contents. You can save multi bytes to file by `save_file_text<...>`.
_Attention_ : may throw `std::system_error` exception if failed.
**Function Prototype**
(1) Multi Bytes to Wide Char
```.cpp
std::wstring wtext = decode(u8"utf-8 string");
wtext = UTF8ToUnicode(u8"utf-8 string");"
```
(2) Wide Char to Multi Bytes
```.cpp
std::string text = encode(L"wide string");
text = UnicodeToUTF8(L"wide string");
```
(3) Convert between Multi Bytes
```.cpp
std::string ntext = convert("ansi string");
```
(4) Read/Save File Text
```.cpp
std::wstring wtext = read_file_text(L"demo.txt");
save_file_text("demo.txt", "bingo");
```
**Usage**
```.cpp
using namespace ymh;
try
{
auto text = file_text("demo.txt");
auto wtext = decode(text);
}
catch (std::system_error const& e)
{
}
```
## Python2/3
### [mcr](https://github.com/yanminhui/misc/blob/master/py/mcr.py)
Measure compression ratio. `zlib`, `gzip`, `bz2`, `lzma` is supported.
**Usage**
```bash
usage: mcr.py [-h] [--verbose] [--chunk-size {4,8,16,32,64,128,256,512}]
[--name {gzip,all,zlib,bz2}]
[--level {-2,-1,0,1,2,3,4,5,6,7,8,9}]
file
Measure compression ratio.
positional arguments:
file file or directory
optional arguments:
-h, --help show this help message and exit
--verbose print progress status (default: None)
--chunk-size {4,8,16,32,64,128,256,512}
data chunk's size (metric: KB) (default: 16)
--name {gzip,all,zlib,bz2}
compression algorithm's name (default: all)
--level {-2,-1,0,1,2,3,4,5,6,7,8,9}
controlling the level of compression, all = -2,
default = -1 (default: -2)
```
**Example**
```bash
$ python3 mcr.py --level=-1 /dev/vda
File: /dev/vda, Length: 40.0 GB, Chunk Size: 16.0 KB
NAME LVL OUTSIZE EXPIRED %SAV IN/ps OUT/ps RATIO %PROG REMAIN
bz2 9 6.61 GB 1.1 h 83.48 10.33 MBps 1.71 MBps 6.05 100.0 0.0 s
zlib -1 8.74 GB 14.93 m 78.15 45.72 MBps 9.99 MBps 4.58 100.0 0.0 s
gzip 9 8.75 GB 50.93 m 78.13 13.4 MBps 2.93 MBps 4.57 100.0 0.0 s
lzma 0 4.43 GB 3.03 h 88.93 3.75 MBps 425.55 KBps 9.03 100.0 0.0 s
```
## ECMAScript5/6
## Shell Script