91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 5KB
    文件類型: .c
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2021-05-09
  • 語(yǔ)言: 其他
  • 標(biāo)簽: strchr??

資源簡(jiǎn)介

strchr的源碼閱讀,更加了解字符串的操作

資源截圖

代碼片段和文件信息

/*?Find?the?first?occurrence?of?C?in?S.??*/
char?*
strchr?(s?c_in)
?????const?char?*s;
?????int?c_in;
{
??const?unsigned?char?*char_ptr;
??const?unsigned?long?int?*longword_ptr;
??unsigned?long?int?longword?magic_bits?charmask;
??unsigned?reg_char?c;

??c?=?(unsigned?char)?c_in;

??/*?Handle?the?first?few?characters?by?reading?one?character?at?a?time.
?????Do?this?until?CHAR_PTR?is?aligned?on?a?longword?boundary.??*/
??for?(char_ptr?=?(const?unsigned?char?*)?s;
???????((unsigned?long?int)?char_ptr?&?(sizeof?(longword)?-?1))?!=?0;
???????++char_ptr)
????if?(*char_ptr?==?c)
??????return?(void?*)?char_ptr;
????else?if?(*char_ptr?==?‘/0‘)
??????return?NULL;

??/*?All?these?elucidatory?comments?refer?to?4-byte?longwords
?????but?the?theory?applies?equally?well?to?8-byte?longwords.??*/

??longword_ptr?=?(unsigned?long?int?*)?char_ptr;

??/*?Bits?31?24?16?and?8?of?this?number?are?zero.??Call?these?bits
?????the?“holes.“??Note?that?there?is?a?hole?just?to?the?left?of
?????each?byte?with?an?extra?at?the?end:

?????bits:??01111110?11111110?11111110?11111111
?????bytes:?AAAAAAAA?BBBBBBBB?CCCCCCCC?DDDDDDDD

?????The?1-bits?make?sure?that?carries?propagate?to?the?next?0-bit.
?????The?0-bits?provide?holes?for?carries?to?fall?into.??*/
??switch?(sizeof?(longword))
????{
????case?4:?magic_bits?=?0x7efefeffL;?break;
????case?8:?magic_bits?=?((0x7efefefeL?<????default:
??????abort?();
????}

??/*?Set?up?a?longword?each?of?whose?bytes?is?C.??*/
??charmask?=?c?|?(c?<??charmask?|=?charmask?<??if?(sizeof?(longword)?>?4)
????/*?Do?the?shift?in?two?steps?to?avoid?a?warning?if?long?has?32?bits.??*/
????charmask?|=?(charmask?<??if?(sizeof?(longword)?>?8)
????abort?();

??/*?Instead?of?the?traditional?loop?which?tests?each?character
?????we?will?test?a?longword?at?a?time.??The?tricky?part?is?testing
?????if?*any?of?the?four*?bytes?in?the?longword?in?question?are?zero.??*/
??for?(;;)
????{
??????/*?We?tentatively?exit?the?loop?if?adding?MAGIC_BITS?to
?LONGWORD?fails?to?change?any?of?the?hole?bits?of?LONGWORD.

?1)?Is?this?safe???Will?it?catch?all?the?zero?bytes?
?Suppose?there?is?a?byte?with?all?zeros.??Any?carry?bits
?propagating?from?its?left?will?fall?into?the?hole?at?its
?least?significant?bit?and?stop.??Since?there?will?be?no
?carry?from?its?most?significant?bit?the?LSB?of?the
?byte?to?the?left?will?be?unchanged?and?the?zero?will?be
?detected.

?2)?

評(píng)論

共有 條評(píng)論

相關(guān)資源