Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script

License

NotificationsYou must be signed in to change notification settings

yanminhui/misc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script.

C/C++/11/14/17/20

Given a byte count, converts it to human-readable formatand 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

template<typename CharT,typename ByteT>CharTconst*format_bytes(std::basic_string<CharT>& repr// (1)                        , ByteTconst bytes                        , std::size_tconst decimal=2u                        , std::size_tconst reduced_unit=1024u);template<typename CharT,typename ByteT,typename IndicatorT       ,typename =typename std::enable_if<!std::is_integral<IndicatorT>::value>::type>CharTconst*format_bytes(std::basic_string<CharT>& repr// (2)                        , ByteTconst bytes                        , IndicatorT&& indicator                        , std::size_tconst decimal=2u                        , std::size_tconst reduced_unit=1024u);template<typename CharT,typename ByteT,typename InputIt>CharTconst*format_bytes(std::basic_string<CharT>& repr// (3)                        , ByteTconst bytes                        , InputIt first, InputIt last                        , std::size_tconst decimal=2u                        , std::size_tconst reduced_unit=1024u);template<typename CharT,typename ByteT       ,typename InputIt,typename IndicatorT>CharTconst*format_bytes(std::basic_string<CharT>& repr// (4)                        , ByteTconst bytes                        , InputIt first, InputIt last                        , IndicatorT&& indicator                        , std::size_tconst decimal=2u                        , std::size_tconst reduced_unit=1024u);

Usage

usingnamespaceymh::misc;std::string s;std::cout << format_bytes(s,18446640) << std::endl;

equal to:

std::wstring wcs;// unicodeauto indicators = {"Bytes","KB","MB","GB" };format_bytes(s,18446640           , std::begin(indicators), std::end(indicators)           , "MB", 2u, 1024u);

Output

17.60 MB

Save std::error_code, boost.system, GetLastError(), user custom error codeand 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

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

SET_ERROR_CODE[W](error_t&, error_code);MAKE_ERROR_CODE[W](error_t&,errc_t);

(3) System Error

SET_SYSTEM_ERROR[W](error_t&, ::GetLastError());// errno

(4) Catch Exception

ERROR_TRY[W] {// throw exception} ERROR_CATCH[W](error_t&)

(5) Print Error Message to Stream

voiderror_t::dump(std::basic_ostream<charT>&);voiderror_t::dump_backtrace(std::basic_ostream<charT>&);

Usage

usingnamespaceymh;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

File"xxx.cpp", line?,in<function>: Open file error.log failedFile"xxx.cpp", line?,in<function>: Success

Unicode and ANSI (byte string) conversion is supported by classcodec.

Convert wide char to multi bytes byencode<codepage::cp_xxx, bom::nobomb>and reverse it bydecode<codepage::cp_xxx, bom::nobomb>. It can convert onemulti bytes to another multi bytes byconvert<...>.

Others, it will try convert to local codepage if you usefile_text to readfile's contents. You can save multi bytes to file bysave_file_text<...>.

Attention : may throwstd::system_error exception if failed.

Function Prototype

(1) Multi Bytes to Wide Char

std::wstring wtext = decode<codepage::cp_utf8>(u8"utf-8 string");wtext = UTF8ToUnicode<bom::nobomb>(u8"utf-8 string");"

(2) Wide Char to Multi Bytes

std::string text = encode<codepage::cp_utf8>(L"wide string");text = UnicodeToUTF8<bom::bomb>(L"wide string");

(3) Convert between Multi Bytes

std::string ntext = convert<codepage::cp_utf8>("ansi string");

(4) Read/Save File Text

std::wstring wtext = read_file_text(L"demo.txt");save_file_text<codepage::cp_utf8, bom::bomb>("demo.txt","bingo");

Usage

usingnamespaceymh;try{auto text =file_text("demo.txt");auto wtext =decode(text);}catch (std::system_errorconst& e){}

Python2/3

Measure compression ratio.zlib,gzip,bz2,lzma is supported.

Usage

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}]              fileMeasure compression ratio.positional arguments:  file                  file or directoryoptional arguments:  -h, --help            show thishelp message andexit  --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

$ python3 mcr.py --level=-1 /dev/vdaFile: /dev/vda, Length: 40.0 GB, Chunk Size: 16.0 KBNAME 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 szlib  -1 8.74 GB   14.93 m 78.15 45.72 MBps  9.99 MBps    4.58 100.0 0.0 sgzip   9 8.75 GB   50.93 m 78.13 13.4 MBps   2.93 MBps    4.57 100.0 0.0 slzma   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

About

Miscellaneous skills for C, C++11/14/17/20, Python2/3, ECMAScript5/6 and Shell Script

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp