📄 wy__hstr.h
字号:
/* Copyright is licensed by GNU LGPL. (I.J. Wang, 2004)*/#ifndef WY__HSTR_H__#define WY__HSTR_H__#define WY__HSTR_VERSION 31#include "wyret.h"#include "wycseg.h"#include <cstdlib>/* Wy__HStr is a class of fixed-size character array. Constructor is hidden, use member alloc(..)/free(Wy__HStr*) to construct and destruct Wy__HStr objects. Wym_EFBIG is returned if length of the resultant string exceeds _capacity() (flexible featured class 'WyStr' will reallocate Wy__HStr) Implementation uses Wy__HStr as the data header in front of the internally allocated character array, the trailing area of Wy__HStr is for storing the characters. None of class member is the cancellation point*/class Wy__HStr { public: typedef size_t size_type; typedef char value_type; static const value_type EOS=0; static const char class_name[]; WY_THROW_REPLY; private: static const size_type BufGranBits=7; // .at leat 2, see ref1 (cpp) // .for number conversions at // least 7 required static const size_type DefaultObjSize=1<<BufGranBits; static const size_type BufGranBitsMask=DefaultObjSize-1; // [Syn] Get the memory block size enough for Wy__HStr object which can // hold dlen elements // // [Ret] Memory size // inline static const size_type _memsize(size_type dlen) WY__NOTHROW__ { return ((~BufGranBitsMask)& (dlen+sizeof(Wy__HStr)+sizeof(value_type)-1)) +DefaultObjSize; }; // [Syn] Get the capacity of the memory block size for Wy__HStr object // // Note: msize must be >= DefaultObjSize // // [Ret] number of characters that msize size of memory for object can hold // inline static const size_type _capmem(size_type msize) WY__NOTHROW__ { return msize-sizeof(value_type)-sizeof(Wy__HStr); }; inline static value_type* _i_alloc(size_type size) WY__NOTHROW__ #ifdef WY_DEBUG try { return reinterpret_cast<value_type*>(std::malloc(size)); } catch(...) { WY_TERMINATE("std::malloc(size_t) threw something"); }; #else { return reinterpret_cast<value_type*>(std::malloc(size)); }; #endif inline static void _i_free(void* p) WY__NOTHROW__ #ifdef WY_DEBUG try { std::free(p); } catch(...) { WY_TERMINATE("std::free(void*) threw something"); }; #else { std::free(p); }; #endif // [Syn] Get the data buffer of *hptr // inline static const value_type* _data_buf(const Wy__HStr* hptr) WY__NOTHROW__ { return reinterpret_cast<const value_type*>(hptr+1); }; inline static value_type* _data_buf(Wy__HStr* hptr) WY__NOTHROW__ { return reinterpret_cast<value_type*>(hptr+1); }; // Hidden Wy__HStr(); Wy__HStr(const Wy__HStr&); ~Wy__HStr(); const Wy__HStr& operator=(const Wy__HStr&); bool operator==(const Wy__HStr&) const; bool operator!=(const Wy__HStr&) const; WyRet append(int); void* operator new(size_t); // use alloc(..) public: // [Syn] Allocate object in the dynamic store capacible of holding // MinCapacity number of characters // // data()= "" // size()= 0 // _capacity()= MinCapacity // // Note: Use member free(..) to destruct and free the object // // [Throw] Reply // Wym_ENOMEM // // [Ret] Pointer to allocated Wy__HStr object // static Wy__HStr* alloc(void) WY__TSPC(Reply); static Wy__HStr* alloc(const Wy__HStr&) WY__TSPC(Reply); // [Syn] Allocate object in the dynamic store capacible of holding // cap number of characters and initialize object to represent // an empty string. // // data()= "" // size()= 0 // _capacity()= enough to hold cap characters // // Note: Use member free(..) to destruct and free the object // // [Throw] Reply // Wym_EFBIG cap too long, cap>max_capacity() // Wym_ENOMEM // // [Ret] Pointer to allocated Wy__HStr object, or // static Wy__HStr* alloc(size_type cap) WY__TSPC(Reply); // [Syn] Allocate object in the dynamic store holding // cnum number of characters ch // // data()= cnum character ch's // size()= cnum // _capacity()= enough to hold cnum characters // // Note: Use member free(..) to destruct and free the object // // [Throw] Reply // Wym_EFBIG cnum>max_capacity() // Wym_ENOMEM // // [Ret] Pointer to allocated Wy__HStr object, or // static Wy__HStr* alloc(size_type cnum, value_type ch) WY__TSPC(Reply); // [Syn] Allocate object in the dynamic store // and initialize object to contaion string indicated by cs // // data()= characters copied from sbuf // size()= cs.size() // capacity()= enough to hold string indicated by cs // // Note: Use member free(..) to destruct and free the object // // [Throw] Reply // Wym_EFBIG cs (or cs1,cs2,cs3) >max_capacity() // Wym_ENOMEM // // [Ret] Pointer to allocated Wy__HStr object, or // static Wy__HStr* alloc(const WyCSeg& cs) WY__TSPC(Reply); static Wy__HStr* alloc(const WyCSeg& cs, value_type ch) WY__TSPC(Reply); static Wy__HStr* alloc(const WyCSeg& cs, size_type cnum, value_type ch) WY__TSPC(Reply); static Wy__HStr* alloc(const WyCSeg& cs1,size_type cnum, value_type ch, const WyCSeg& cs2) WY__TSPC(Reply); static Wy__HStr* alloc(const WyCSeg& cs1,const WyCSeg& cs2) WY__TSPC(Reply); static Wy__HStr* alloc(const WyCSeg& cs1,const WyCSeg& cs2, const WyCSeg& cs3) WY__TSPC(Reply); static Wy__HStr* alloc(value_type ch,const WyCSeg& cs) WY__TSPC(Reply); // [Syn] Free the resource allocated by alloc(..) // inline static void free(Wy__HStr* hptr) WY__NOTHROW__ { _i_free(hptr); }; inline const value_type* data(void) const WY__NOTHROW__ { return _data_buf(this); }; inline const value_type* c_str(void) const WY__NOTHROW__ { const_cast<value_type*>(_data_buf(this))[_m_dlen]=EOS; return _data_buf(this); }; // Note: For object is mostly refered by pointer, operator[]'s not provided. // These members do not check for invalid pos. // inline const value_type& _at(size_type pos) const WY__NOTHROW__ { return _data_buf(this)[pos]; }; inline value_type& _at(size_type pos) WY__NOTHROW__ { return _data_buf(this)[pos]; }; inline const value_type* _begin(void) const WY__NOTHROW__ { return _data_buf(this); }; inline const value_type* _end(void) const WY__NOTHROW__ { return &_data_buf(this)[_m_dlen]; }; inline size_type size(void) const WY__NOTHROW__ { return _m_dlen; }; inline size_type _capacity(void) const WY__NOTHROW__ { return _m_dcap; }; // [Syn] Get sub-array // inline WyCSeg cseg(void) const WY__NOTHROW__ { return WyCSeg(_data_buf(this),_m_dlen); } // [Syn] Get sub-array // // [Throw] Reply // Wym_EINVAL idx>size() // WyCSeg cseg(size_type idx) const WY__TSPC(Reply) { if(idx>_m_dlen) { WY_THROW( Reply(Wym_EINVAL) ); } return WyCSeg(_data_buf(this)+idx,_m_dlen-idx); }; WyCSeg cseg(size_type idx, size_type num) const WY__TSPC(Reply) { if(idx>_m_dlen) { WY_THROW( Reply(Wym_EINVAL) ); } size_type rs=_m_dlen-idx; if(rs>num) { rs=num; } return WyCSeg(_data_buf(this)+idx,rs); }; // [Syn] Set data as an empty string // void set_string(void) WY__NOTHROW__ { _data_buf(this)[0]=EOS; _m_dlen=0; }; // [Syn] Set data from cs indicated characters // // [Ret] Ok // Wym_ELOOP *this and argument indicated string would overlap // WyRet set_string(const Wy__HStr& hstr) WY__TSPC(); // [Syn] Set data from cs indicated characters // // [Ret] Ok // Wym_EFBIG Data size would exceed _capacity() // Wym_ELOOP *this and argument indicated string would overlap // WyRet set_string(const WyCSeg& cs) WY__TSPC();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -