strlen.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:5k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  2.    Written by Torbjorn Granlund (tege@sics.se),
  3.    with help from Dan Sahlin (dan@sics.se);
  4.    commentary by Jim Blandy (jimb@ai.mit.edu).
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB.  If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA.  */
  17. #include <misc.h>
  18. /* Include misc.h instead of ansidecl.h and string.h.   Remove use
  19.    of DEFUN and CONST.  mgd@santafe.edu 1998-07-08 */
  20. /* Return the length of the null-terminated string STR.  Scan for
  21.    the null terminator quickly by testing four bytes at a time.  */
  22. size_t
  23. strlen (const char *str)
  24. {
  25.   const char *char_ptr;
  26.   const unsigned long int *longword_ptr;
  27.   unsigned long int longword, magic_bits, himagic, lomagic;
  28.   /* Handle the first few characters by reading one character at a time.
  29.      Do this until CHAR_PTR is aligned on a longword boundary.  */
  30.   for (char_ptr = str; ((unsigned long int) char_ptr
  31. & (sizeof (longword) - 1)) != 0;
  32.        ++char_ptr)
  33.     if (*char_ptr == '')
  34.       return char_ptr - str;
  35.   /* All these elucidatory comments refer to 4-byte longwords,
  36.      but the theory applies equally well to 8-byte longwords.  */
  37.   longword_ptr = (unsigned long int *) char_ptr;
  38.   /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
  39.      the "holes."  Note that there is a hole just to the left of
  40.      each byte, with an extra at the end:
  41.      
  42.      bits:  01111110 11111110 11111110 11111111
  43.      bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD 
  44.      The 1-bits make sure that carries propagate to the next 0-bit.
  45.      The 0-bits provide holes for carries to fall into.  */
  46.   magic_bits = 0x7efefeffL;
  47.   himagic = 0x80808080L;
  48.   lomagic = 0x01010101L;
  49.   if (sizeof (longword) > 4)
  50.     {
  51.       /* 64-bit version of the magic.  */
  52.       magic_bits = (0x7efefefeL << 32) | 0xfefefeffL;
  53.       himagic = (himagic << 32) | himagic;
  54.       lomagic = (lomagic << 32) | lomagic;
  55.     }
  56.   if (sizeof (longword) > 8)
  57.     abort ();
  58.   /* Instead of the traditional loop which tests each character,
  59.      we will test a longword at a time.  The tricky part is testing
  60.      if *any of the four* bytes in the longword in question are zero.  */
  61.   for (;;)
  62.     {
  63.       /* We tentatively exit the loop if adding MAGIC_BITS to
  64.  LONGWORD fails to change any of the hole bits of LONGWORD.
  65.  1) Is this safe?  Will it catch all the zero bytes?
  66.  Suppose there is a byte with all zeros.  Any carry bits
  67.  propagating from its left will fall into the hole at its
  68.  least significant bit and stop.  Since there will be no
  69.  carry from its most significant bit, the LSB of the
  70.  byte to the left will be unchanged, and the zero will be
  71.  detected.
  72.  2) Is this worthwhile?  Will it ignore everything except
  73.  zero bytes?  Suppose every byte of LONGWORD has a bit set
  74.  somewhere.  There will be a carry into bit 8.  If bit 8
  75.  is set, this will carry into bit 16.  If bit 8 is clear,
  76.  one of bits 9-15 must be set, so there will be a carry
  77.  into bit 16.  Similarly, there will be a carry into bit
  78.  24.  If one of bits 24-30 is set, there will be a carry
  79.  into bit 31, so all of the hole bits will be changed.
  80.  The one misfire occurs when bits 24-30 are clear and bit
  81.  31 is set; in this case, the hole at bit 31 is not
  82.  changed.  If we had access to the processor carry flag,
  83.  we could close this loophole by putting the fourth hole
  84.  at bit 32!
  85.  So it ignores everything except 128's, when they're aligned
  86.  properly.  */
  87.       longword = *longword_ptr++;
  88.       if (
  89. #if 0
  90.   /* Add MAGIC_BITS to LONGWORD.  */
  91.   (((longword + magic_bits)
  92.     /* Set those bits that were unchanged by the addition.  */
  93.     ^ ~longword)
  94.    /* Look at only the hole bits.  If any of the hole bits
  95.       are unchanged, most likely one of the bytes was a
  96.       zero.  */
  97.    & ~magic_bits)
  98. #else
  99.   ((longword - lomagic) & himagic)
  100. #endif
  101.   != 0)
  102. {
  103.   /* Which of the bytes was the zero?  If none of them were, it was
  104.      a misfire; continue the search.  */
  105.   const char *cp = (const char *) (longword_ptr - 1);
  106.   if (cp[0] == 0)
  107.     return cp - str;
  108.   if (cp[1] == 0)
  109.     return cp - str + 1;
  110.   if (cp[2] == 0)
  111.     return cp - str + 2;
  112.   if (cp[3] == 0)
  113.     return cp - str + 3;
  114.   if (sizeof (longword) > 4)
  115.     {
  116.       if (cp[4] == 0)
  117. return cp - str + 4;
  118.       if (cp[5] == 0)
  119. return cp - str + 5;
  120.       if (cp[6] == 0)
  121. return cp - str + 6;
  122.       if (cp[7] == 0)
  123. return cp - str + 7;
  124.     }
  125. }
  126.     }
  127. }