glibc2.9源码分析 -- string.h(strncpy - 将一个字符串的前n个字符复制到另一个字符数组中)

这个函数的实现就是从头扫描源串,然后将前n个字符放入目标字符数组中。

总体给人感觉是经过了深度优化的代码,主要有以下几点值得我们去学习:
1.使用寄存器中间变量,提高运行效率。
2.使用位移运算代替2的倍数的乘除运算,进一步提高运行效率。
3.复制时循环体以4个字符为单位操作一次,减少小循环次数,又进一步提高运行效率。

从源码的分析结果来看,这个函数在复制字符串的时候会做出以下几种行为:
(设n代表要复制的字符数量。)
1.源串的定义不是整个原始输入字符序列,而是原始输入字符序列中出现的第一个’\0’以及其之前的字符构成的字符序列。这里将这个有可能作为子串的串称为源串。
2.如果n小于等于源串字符数量,则复制完成之后不会在目标字符数组后面加’\0’(如果源串最后一个字符就是’\0’的话看起来是加上了,但其实这个’\0’还是从源串复制过去的)。
3.如果n大于源串长度,则复制完成之后会在目标字符数组后面插入 [n-源串字符数量] 个’\0’,例如某个源串的字符个数为3(2个普通字符加1个’\0’),n为5,则将源串复制到目标字符数组时会添加2个’\0’,最后得到的字符数组后面就是3个’\0’。

小提示:
使用strncpy()的时候一定要仔细考虑’\0’的问题,因为这个函数有些情况下是不操作’\0’的。

下面是加了我理解的代码。

/* Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <string.h>
#include <memcopy.h>

#undef strncpy

char *
strncpy (s1, s2, n)
     char *s1;
     const char *s2;
     size_t n;
{
  reg_char c;
  char *s = s1;

  //此时s1指向原始字符序列的首字符的地址的前一个地址
  --s1;

  //如果n>4,则按4个字符为单位进行循环复制
  if (n >= 4)
    {
	  //移位运算代替除法运算,提高计算效率
      size_t n4 = n >> 2;

      for (;;)
	{
	  c = *s2++;
	  *++s1 = c;
	  if (c == '\0')
	    break;
	  c = *s2++;
	  *++s1 = c;
	  if (c == '\0')
	    break;
	  c = *s2++;
	  *++s1 = c;
	  if (c == '\0')
	    break;
	  c = *s2++;
	  *++s1 = c;
	  if (c == '\0')
	    break;
	  if (--n4 == 0)
		//前往填充'\0'
	    goto last_chars;
	}
	//原字符序列中间存在'\0'时才会执行下面这段代码
      n = n - (s1 - s) - 1;
      if (n == 0)
	return s;
      goto zero_fill;
    }

 last_chars:
  //这个n &= 3相当于n %= 4,只不过运算效率更高一些
  n &= 3;
  if (n == 0)
    return s;
  //复制剩余字符
  do
    {
      c = *s2++;
      *++s1 = c;
      if (--n == 0)
	return s;
    }
  while (c != '\0');
 //填充'\0'
 zero_fill:
  do
    *++s1 = '\0';
  while (--n > 0);

  return s;
}
libc_hidden_builtin_def (strncpy)

注:glibc版本为2.9。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Kirito

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值