字符串处理函数

本文探讨了Android C库在版本9.0.0_r3中的字符串处理函数实现,链接指向了开源代码。同时,还介绍了Linux内核v5.7.2中的相关函数,通过链接提供了详细源码供读者查阅。
摘要由CSDN通过智能技术生成

Android C库:
http://androidxref.com/9.0.0_r3/xref/bionic/libc/upstream-openbsd/lib/libc/string/

/*
37 * sizeof(word) MUST BE A POWER OF TWO
38 * SO THAT wmask BELOW IS ALL ONES
39 */
40typedef	long word;		/* "word" used for optimal copy speed */
41
42#define	wsize	sizeof(word)
43#define	wmask	(wsize - 1)
44
45/*
46 * Copy a block of memory, handling overlap.拷贝一块内存
47 */
48void *
49memmove(void *dst0, const void *src0, size_t length)
50{
   
51	char *dst = dst0;
52	const char *src = src0;
53	size_t t;
54
55	if (length == 0 || dst == src)		/* nothing to do */
56		goto done;
57
58	/*
59	 * Macros: loop-t-times; and loop-t-times, t>0
60	 */
61#define	TLOOP(s) if (t) TLOOP1(s)
62#define	TLOOP1(s) do {
    s; } while (--t)
63
64	if ((unsigned long)dst < (unsigned long)src) {
   
65		/*
66		 * Copy forward.
67		 */
68		t = (long)src;	/* only need low bits */
69		if ((t | (long)dst) & wmask) {
   
70			/*
71			 * Try to align operands.  This cannot be done
72			 * unless the low bits match.
73			 */
74			if ((t ^ (long)dst) & wmask || length < wsize)
75				t = length;
76			else
77				t = wsize - (t & wmask);
78			length -= t;
79			TLOOP1(*dst++ = *src++);
80		}
81		/*
82		 * Copy whole words, then mop up any trailing bytes.
83		 */
84		t = length / wsize;
85		TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
86		t = length & wmask;
87		TLOOP(*dst++ = *src++);
88	} else {
   
89		/*
90		 * Copy backwards.  Otherwise essentially the same.
91		 * Alignment works as before, except that it takes
92		 * (t&wmask) bytes to align, not wsize-(t&wmask).
93		 */
94		src += length;
95		dst += length;
96		t = (long)src;
97		if ((t | (long)dst) & wmask) {
   
98			if ((t ^ (long)dst) & wmask || length <= wsize)
99				t = length;
100			else
101				t &= wmask;
102			length -= t;
103			TLOOP1(*--dst = *--src);
104		}
105		t = length / wsize;
106		TLOOP(src -= wsize; dst 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值