亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

一些常見函數的內部實現(原)

系統 1701 0

一些常見函數的內部實現(原)

Wentao Sun

1. 子串查找:

/* ?strtok_s? */
/*
?*?strtok_s,?wcstok_s?;
?*?uses?_Context?to?keep?track?of?the?position?in?the?string.
?
*/
_SAFECRT__EXTERN_C
char ? * ?__cdecl?strtok_s( char ? * _String,? const ? char ? * _Control,? char ? ** _Context);

#if ?_SAFECRT_USE_INLINES

__inline
char ? * ?__cdecl?strtok_s( char ? * _String,? const ? char ? * _Control,? char ? ** _Context)
{
????unsigned?
char ? * str;
????
const ?unsigned? char ? * ctl? = ?( const ?unsigned? char ? * )_Control;
????unsigned?
char ?map[ 32 ];
????
int ?count;

????
/* ?validation?section? */
????_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Context,?EINVAL,?NULL);
????_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Control,?EINVAL,?NULL);
????_SAFECRT__VALIDATE_CONDITION_ERROR_RETURN(_String?!=?NULL?||?*_Context?!=?NULL,?EINVAL,?NULL);

???? /* ?Clear?control?map? */
????
for ?(count? = ? 0 ;?count? < ? 32 ;?count ++ )
????{
????????map[count]?
= ? 0 ;
????}

????
/* ?Set?bits?in?delimiter?table? */
????
do ?{
????????map[
* ctl? >> ? 3 ]? |= ?( 1 ? << ?( * ctl? & ? 7 ));
????}?
while ?( * ctl ++ );

????
/* ?If?string?is?NULL,?set?str?to?the?saved
????*?pointer?(i.e.,?continue?breaking?tokens?out?of?the?string
????*?from?the?last?strtok?call)?
*/
????
if ?(_String? != ?NULL)
????{
????????str?
= ?(unsigned? char ? * )_String;
????}
????
else
????{
????????str?
= ?(unsigned? char ? * ) * _Context;
????}

????
/* ?Find?beginning?of?token?(skip?over?leading?delimiters).?Note?that
????*?there?is?no?token?iff?this?loop?sets?str?to?point?to?the?terminal
????*?null?(*str?==?0)?
*/
????
while ?((map[ * str? >> ? 3 ]? & ?( 1 ? << ?( * str? & ? 7 )))? && ? * str? != ? 0 )
????{
????????str
++ ;
????}

????_String?
= ?( char ? * )str;

????
/* ?Find?the?end?of?the?token.?If?it?is?not?the?end?of?the?string,
????*?put?a?null?there.?
*/
????
for ?(?;? * str? != ? 0 ?;?str ++ ?)
????{
????????
if ?(map[ * str? >> ? 3 ]? & ?( 1 ? << ?( * str? & ? 7 )))
????????{
????????????
* str ++ ? = ? 0 ;
????????????
break ;
????????}
????}

????
/* ?Update?context? */
????
* _Context? = ?( char ? * )str;

????
/* ?Determine?if?a?token?has?been?found.? */
????
if ?(_String? == ?( char ? * )str)
????{
????????
return ?NULL;
????}
????
else
????{
????????
return ?_String;
????}
}
#endif

/* ?wcstok_s? */
_SAFECRT__EXTERN_C
wchar_t?
* ?__cdecl?wcstok_s(wchar_t? * _String,? const ?wchar_t? * _Control,?wchar_t? ** _Context);

#if ?_SAFECRT_USE_INLINES

__inline
wchar_t?
* ?__cdecl?wcstok_s(wchar_t? * _String,? const ?wchar_t? * _Control,?wchar_t? ** _Context)
{
????wchar_t?
* token;
????
const ?wchar_t? * ctl;

????
/* ?validation?section? */
????_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Context,?EINVAL,?NULL);
????_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Control,?EINVAL,?NULL);
????_SAFECRT__VALIDATE_CONDITION_ERROR_RETURN(_String?!=?NULL?||?*_Context?!=?NULL,?EINVAL,?NULL);


????
/* ?If?string==NULL,?continue?with?previous?string? */
????
if ?( ! _String)
????{
????????_String?
= ? * _Context;
????}

????
/* ?Find?beginning?of?token?(skip?over?leading?delimiters).?Note?that
????*?there?is?no?token?iff?this?loop?sets?string?to?point?to?the?terminal?null.?
*/
????
for ?(?;? * _String? != ? 0 ?;?_String ++ )
????{
????????
for ?(ctl? = ?_Control;? * ctl? != ? 0 ? && ? * ctl? != ? * _String;?ctl ++ )
????????????;
????????
if ?( * ctl? == ? 0 )
????????{
????????????
break ;
????????}
????}

????token?
= ?_String;

????
/* ?Find?the?end?of?the?token.?If?it?is?not?the?end?of?the?string,
????*?put?a?null?there.?
*/
????
for ?(?;? * _String? != ? 0 ?;?_String ++ )
????{
????????
for ?(ctl? = ?_Control;? * ctl? != ? 0 ? && ? * ctl? != ? * _String;?ctl ++ )
????????????;
????????
if ?( * ctl? != ? 0 )
????????{
????????????
* _String ++ ? = ? 0 ;
????????????
break ;
????????}
????}

????
/* ?Update?the?context? */
????
* _Context? = ?_String;

????
/* ?Determine?if?a?token?has?been?found.? */
????
if ?(token? == ?_String)
????{
????????
return ?NULL;
????}
????
else
????{
????????
return ?token;
????}
}
#endif

?

?2. 使用strsafe.h時需要注意將其放到其他string操作頭文件的后面,以免不必要的編譯錯誤。

可以參考: http://www.programfan.com/club/showtxt.asp?id=235904
我今天在編譯的適合也碰到過這個問題。

再次強調,使用strsafe.h最好是放在cpp文件中,而非頭文件中。

在微軟的tchar.h中明確的有個#error put strsafe.h behind thar.h。 strsafe.h被要求放在tchar.h的后面。如果不這樣,會得到一堆很奇怪的錯誤。

?

?

一些常見函數的內部實現(原)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 五月久久亚洲七七综合中文网 | 欧美国产精品亚洲精品第一区 | 久久黄色网 | 久久午夜青青草原影院 | 亚洲麻豆国产精品 | 久久国产精品免费一区二区三区 | 久久精品国产免费看久久精品 | 日本自己的私人影院 | 综合玖玖| 九九视频免费精品视频免费 | 久久精品国产免费观看99 | 四虎伦理| 99久久这里只有精品 | 免费一看一级毛片人 | 成人一级片在线观看 | 国产精品久久久久久久免费大片 | 欧美xvideosexo另类 | 羞羞视频免费网站在线 | 免费毛片看 | 和日本免费不卡在线v | 中文字幕精品一区二区三区在线 | 日本一级特黄大一片免 | 欧美精品日日鲁夜夜 | 成年人免费在线视频 | 狠狠综合久久久久尤物丿 | 精品无人区乱码一区二区 | 日韩欧美二区在线观看 | 奇米成人网 | 亚洲综合春色另类久久 | 欧美色大成网站www永久男同 | 香蕉久久夜色精品国产2020 | 国产成人亚综合91精品首页 | 色爱区综合 | 免费又黄又爽视频 | 亚洲高清国产一区二区三区 | 夜精品a一区二区三区 | 亚洲欧美色综合精品 | 久久久不卡国产精品一区二区 | 亚洲性色视频 | 夜夜操操 | 五月网婷婷|